Skip to content

Commit

Permalink
Add validateAccessToken for third party providers that allows validat…
Browse files Browse the repository at this point in the history
…ion of access tokens issues by the provider during sign in or up
  • Loading branch information
nkshah2 committed Sep 7, 2023
1 parent e3f6e01 commit b8f2841
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
7 changes: 0 additions & 7 deletions lib/build/recipe/thirdparty/api/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ function getAPIInterface() {
} else {
throw Error("should never come here");
}
if (provider.config.validateAccessToken !== undefined) {
yield provider.config.validateAccessToken({
accessToken: oAuthTokensToUse.access_token,
clientConfig: provider.config,
userContext,
});
}
const userInfo = yield provider.getUserInfo({ oAuthTokens: oAuthTokensToUse, userContext });
if (userInfo.email === undefined && provider.config.requireEmail === false) {
userInfo.email = {
Expand Down
12 changes: 12 additions & 0 deletions lib/build/recipe/thirdparty/providers/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,18 @@ function NewProvider(input) {
);
rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken;
}
/**
* This is intentionally not part of the above if block. This is because the user may want to validate the access
* token payload even if the user info API has not been provided by the provider. In this case they would get an
* empty object and they can fail if they always expect a non-empty object.
*/
if (impl.config.validateAccessToken !== undefined) {
yield impl.config.validateAccessToken({
accessTokenPayload: rawUserInfoFromProvider.fromUserInfoAPI,
clientConfig: impl.config,
userContext,
});
}
const userInfoResult = getSupertokensUserInfoResultFromRawUserInfo(
impl.config,
rawUserInfoFromProvider
Expand Down
4 changes: 3 additions & 1 deletion lib/build/recipe/thirdparty/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ declare type CommonProviderConfig = {
* @param input.userContext Refer to https://supertokens.com/docs/thirdparty/advanced-customizations/user-context
*/
validateAccessToken?: (input: {
accessToken: string;
accessTokenPayload: {
[key: string]: any;
};
clientConfig: ProviderConfigForClientType;
userContext: any;
}) => Promise<void>;
Expand Down
8 changes: 0 additions & 8 deletions lib/ts/recipe/thirdparty/api/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ export default function getAPIInterface(): APIInterface {
throw Error("should never come here");
}

if (provider.config.validateAccessToken !== undefined) {
await provider.config.validateAccessToken({
accessToken: oAuthTokensToUse.access_token,
clientConfig: provider.config,
userContext,
});
}

const userInfo = await provider.getUserInfo({ oAuthTokens: oAuthTokensToUse, userContext });

if (userInfo.email === undefined && provider.config.requireEmail === false) {
Expand Down
13 changes: 13 additions & 0 deletions lib/ts/recipe/thirdparty/providers/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,19 @@ export default function NewProvider(input: ProviderInput): TypeProvider {
rawUserInfoFromProvider.fromUserInfoAPI = userInfoFromAccessToken;
}

/**
* This is intentionally not part of the above if block. This is because the user may want to validate the access
* token payload even if the user info API has not been provided by the provider. In this case they would get an
* empty object and they can fail if they always expect a non-empty object.
*/
if (impl.config.validateAccessToken !== undefined) {
await impl.config.validateAccessToken({
accessTokenPayload: rawUserInfoFromProvider.fromUserInfoAPI,
clientConfig: impl.config,
userContext,
});
}

const userInfoResult = getSupertokensUserInfoResultFromRawUserInfo(impl.config, rawUserInfoFromProvider);

return {
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/recipe/thirdparty/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type CommonProviderConfig = {
* @param input.userContext Refer to https://supertokens.com/docs/thirdparty/advanced-customizations/user-context
*/
validateAccessToken?: (input: {
accessToken: string;
accessTokenPayload: { [key: string]: any };
clientConfig: ProviderConfigForClientType;
userContext: any;
}) => Promise<void>;
Expand Down

0 comments on commit b8f2841

Please sign in to comment.