-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: renewal & better errors #4
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df39ac1
fix: updated dynamic secret types
DanielHougaard de83bee
docs: added renewal docs
DanielHougaard 3f87c89
feat: token renewal
DanielHougaard 05f5f1e
feat: less verbose errors
DanielHougaard 2cee78f
feat: automatic token refreshing
DanielHougaard fd33cf8
update error name
maidul98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,134 @@ | ||
import { InfisicalSDK } from ".."; | ||
import { ApiV1AuthUniversalAuthLoginPostRequest } from "../infisicalapi_client"; | ||
import { DefaultApi as InfisicalApi } from "../infisicalapi_client"; | ||
import { AuthMethod, TAuthCredentials, TTokenDetails } from "../types"; | ||
import { MACHINE_IDENTITY_ID_ENV_NAME } from "./constants"; | ||
import { InfisicalError, newInfisicalError } from "./errors"; | ||
import { getAwsRegion, performAwsIamLogin } from "./util"; | ||
|
||
type AuthenticatorFunction = (accessToken: string) => InfisicalSDK; | ||
type TAuthenticator = { | ||
authenticate: (tokenDetails: Omit<TTokenDetails, "fetchedTime" | "firstFetchTime">) => Promise<InfisicalSDK>; | ||
setCredentials: (credentials: TAuthCredentials) => void; | ||
}; | ||
|
||
type AwsAuthLoginOptions = { | ||
identityId?: string; | ||
}; | ||
|
||
export const renewToken = async (apiClient: InfisicalApi, token?: string) => { | ||
try { | ||
if (!token) { | ||
throw new InfisicalError("Unable to renew access token, no access token set. Are you sure you're authenticated?"); | ||
} | ||
|
||
const res = await apiClient.apiV1AuthTokenRenewPost({ | ||
apiV1AuthTokenRenewPostRequest: { | ||
accessToken: token | ||
} | ||
}); | ||
|
||
return res.data; | ||
} catch (err) { | ||
throw newInfisicalError(err); | ||
} | ||
}; | ||
|
||
export default class AuthClient { | ||
#sdkAuthenticator: AuthenticatorFunction; | ||
#authenticator: TAuthenticator; | ||
#apiClient: InfisicalApi; | ||
#baseUrl: string; | ||
#accessToken: string | undefined; | ||
#credentials: TAuthCredentials | undefined; | ||
|
||
constructor(authenticator: AuthenticatorFunction, apiInstance: InfisicalApi, baseUrl: string) { | ||
this.#sdkAuthenticator = authenticator; | ||
constructor(authenticator: TAuthenticator, apiInstance: InfisicalApi, accessToken?: string) { | ||
this.#authenticator = authenticator; | ||
this.#apiClient = apiInstance; | ||
this.#baseUrl = baseUrl; | ||
this.#accessToken = accessToken; | ||
} | ||
|
||
awsIamAuth = { | ||
login: async (options?: AwsAuthLoginOptions) => { | ||
const identityId = options?.identityId || process.env[MACHINE_IDENTITY_ID_ENV_NAME]; | ||
try { | ||
const identityId = options?.identityId || process.env[MACHINE_IDENTITY_ID_ENV_NAME]; | ||
|
||
if (!identityId) { | ||
throw new Error("Identity ID is required for AWS IAM authentication"); | ||
} | ||
if (!identityId) { | ||
throw new InfisicalError("Identity ID is required for AWS IAM authentication"); | ||
} | ||
|
||
const iamRequest = await performAwsIamLogin(await getAwsRegion()); | ||
const iamRequest = await performAwsIamLogin(await getAwsRegion()); | ||
|
||
const res = await this.#apiClient.apiV1AuthAwsAuthLoginPost({ | ||
apiV1AuthAwsAuthLoginPostRequest: { | ||
iamHttpRequestMethod: iamRequest.iamHttpRequestMethod, | ||
iamRequestBody: Buffer.from(iamRequest.iamRequestBody).toString("base64"), | ||
iamRequestHeaders: Buffer.from(JSON.stringify(iamRequest.iamRequestHeaders)).toString("base64"), | ||
identityId | ||
} | ||
}); | ||
const res = await this.#apiClient.apiV1AuthAwsAuthLoginPost({ | ||
apiV1AuthAwsAuthLoginPostRequest: { | ||
iamHttpRequestMethod: iamRequest.iamHttpRequestMethod, | ||
iamRequestBody: Buffer.from(iamRequest.iamRequestBody).toString("base64"), | ||
iamRequestHeaders: Buffer.from(JSON.stringify(iamRequest.iamRequestHeaders)).toString("base64"), | ||
identityId | ||
} | ||
}); | ||
|
||
this.#authenticator.setCredentials({ | ||
type: AuthMethod.AWSIam, | ||
credentials: { | ||
identityId | ||
} | ||
}); | ||
return this.#authenticator.authenticate(res.data); | ||
} catch (err) { | ||
throw newInfisicalError(err); | ||
} | ||
}, | ||
|
||
return this.#sdkAuthenticator(res.data.accessToken); | ||
renew: async () => { | ||
try { | ||
const refreshedToken = await renewToken(this.#apiClient, this.#accessToken); | ||
return this.#authenticator.authenticate(refreshedToken); | ||
} catch (err) { | ||
throw newInfisicalError(err); | ||
} | ||
} | ||
}; | ||
|
||
universalAuth = { | ||
login: async (options: ApiV1AuthUniversalAuthLoginPostRequest) => { | ||
const res = await this.#apiClient.apiV1AuthUniversalAuthLoginPost({ | ||
apiV1AuthUniversalAuthLoginPostRequest: options | ||
}); | ||
try { | ||
const res = await this.#apiClient.apiV1AuthUniversalAuthLoginPost({ | ||
apiV1AuthUniversalAuthLoginPostRequest: options | ||
}); | ||
|
||
return this.#sdkAuthenticator(res.data.accessToken); | ||
this.#authenticator.setCredentials({ | ||
type: AuthMethod.UniversalAuth, | ||
credentials: { | ||
clientId: options.clientId, | ||
clientSecret: options.clientSecret | ||
} | ||
}); | ||
return this.#authenticator.authenticate(res.data); | ||
} catch (err) { | ||
throw newInfisicalError(err); | ||
} | ||
}, | ||
|
||
renew: async () => { | ||
try { | ||
const refreshedToken = await renewToken(this.#apiClient, this.#accessToken); | ||
return this.#authenticator.authenticate(refreshedToken); | ||
} catch (err) { | ||
throw newInfisicalError(err); | ||
} | ||
} | ||
}; | ||
|
||
accessToken = (token: string) => { | ||
return this.#sdkAuthenticator(token); | ||
accessToken = async (token: string) => { | ||
try { | ||
const tokenData = await renewToken(this.#apiClient, token); | ||
this.#authenticator.setCredentials({ | ||
type: AuthMethod.AccessToken, | ||
credentials: { | ||
accessToken: token | ||
} | ||
}); | ||
return this.#authenticator.authenticate(tokenData); | ||
} catch (err) { | ||
throw newInfisicalError(err); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Add JSDoc comment to explain the purpose, parameters, return type, and possible errors of the function. This is helpful for maintainability and for other developers who might use this function.
'''
/**
*/
'''