From c5e69880436a4c3307c1221ce3dc85dfff0f46c2 Mon Sep 17 00:00:00 2001 From: Ankit Tiwari Date: Mon, 15 Jul 2024 12:36:00 +0530 Subject: [PATCH] fix: PR changes --- lib/build/recipe/oauth2/recipe.js | 2 +- .../recipe/oauth2client/api/implementation.js | 37 +++++++++-------- lib/build/recipe/oauth2client/index.js | 24 +++++++---- .../oauth2client/recipeImplementation.js | 11 ++--- lib/build/recipe/oauth2client/types.d.ts | 40 +++++++++++-------- lib/ts/recipe/oauth2/recipe.ts | 2 +- .../recipe/oauth2client/api/implementation.ts | 39 ++++++++++-------- lib/ts/recipe/oauth2client/index.ts | 26 ++++++++---- .../oauth2client/recipeImplementation.ts | 40 +++++-------------- lib/ts/recipe/oauth2client/types.ts | 40 +++++++++++-------- test/test-server/src/index.ts | 18 --------- 11 files changed, 143 insertions(+), 136 deletions(-) diff --git a/lib/build/recipe/oauth2/recipe.js b/lib/build/recipe/oauth2/recipe.js index 9a7a01277..4e6d0d78c 100644 --- a/lib/build/recipe/oauth2/recipe.js +++ b/lib/build/recipe/oauth2/recipe.js @@ -75,7 +75,7 @@ class Recipe extends recipeModule_1.default { querier_1.Querier.getNewInstanceOrThrowError(recipeId), this.config, appInfo, - this.getDefaultIdTokenPayload + this.getDefaultIdTokenPayload.bind(this) ) ); this.recipeInterfaceImpl = builder.override(this.config.override.functions).build(); diff --git a/lib/build/recipe/oauth2client/api/implementation.js b/lib/build/recipe/oauth2client/api/implementation.js index 7ac101e0c..5b642629a 100644 --- a/lib/build/recipe/oauth2client/api/implementation.js +++ b/lib/build/recipe/oauth2client/api/implementation.js @@ -9,36 +9,41 @@ const session_1 = __importDefault(require("../../session")); function getAPIInterface() { return { authorisationUrlGET: async function ({ options, redirectURIOnProviderDashboard, userContext }) { - const authUrl = await options.recipeImplementation.getAuthorisationRedirectURL( + const providerConfig = await options.recipeImplementation.getProviderConfig({ userContext }); + const authUrl = await options.recipeImplementation.getAuthorisationRedirectURL({ + providerConfig, redirectURIOnProviderDashboard, - userContext - ); + userContext, + }); return Object.assign({ status: "OK" }, authUrl); }, signInPOST: async function (input) { const { options, tenantId, userContext } = input; + const providerConfig = await options.recipeImplementation.getProviderConfig({ userContext }); let oAuthTokensToUse = {}; if ("redirectURIInfo" in input && input.redirectURIInfo !== undefined) { - oAuthTokensToUse = await options.recipeImplementation.exchangeAuthCodeForOAuthTokens( - input.redirectURIInfo, - userContext - ); + oAuthTokensToUse = await options.recipeImplementation.exchangeAuthCodeForOAuthTokens({ + providerConfig, + redirectURIInfo: input.redirectURIInfo, + userContext, + }); } else if ("oAuthTokens" in input && input.oAuthTokens !== undefined) { oAuthTokensToUse = input.oAuthTokens; } else { throw Error("should never come here"); } - const { userId, rawUserInfoFromProvider } = await options.recipeImplementation.getUserInfo( - oAuthTokensToUse, - userContext - ); - const { user, recipeUserId } = await options.recipeImplementation.signIn( + const { userId, rawUserInfoFromProvider } = await options.recipeImplementation.getUserInfo({ + providerConfig, + oAuthTokens: oAuthTokensToUse, + userContext, + }); + const { user, recipeUserId } = await options.recipeImplementation.signIn({ userId, - oAuthTokensToUse, - rawUserInfoFromProvider, tenantId, - userContext - ); + rawUserInfoFromProvider, + oAuthTokens: oAuthTokensToUse, + userContext, + }); const session = await session_1.default.createNewSession( options.req, options.res, diff --git a/lib/build/recipe/oauth2client/index.js b/lib/build/recipe/oauth2client/index.js index 45d0ec291..285af7656 100644 --- a/lib/build/recipe/oauth2client/index.js +++ b/lib/build/recipe/oauth2client/index.js @@ -23,19 +23,29 @@ exports.getUserInfo = exports.exchangeAuthCodeForOAuthTokens = exports.getAuthor const recipe_1 = __importDefault(require("./recipe")); class Wrapper { static async getAuthorisationRedirectURL(redirectURIOnProviderDashboard, userContext) { - return await recipe_1.default - .getInstanceOrThrowError() - .recipeInterfaceImpl.getAuthorisationRedirectURL(redirectURIOnProviderDashboard, userContext); + const recipeInterfaceImpl = recipe_1.default.getInstanceOrThrowError().recipeInterfaceImpl; + const providerConfig = await recipeInterfaceImpl.getProviderConfig({ userContext }); + return await recipeInterfaceImpl.getAuthorisationRedirectURL({ + providerConfig, + redirectURIOnProviderDashboard, + userContext, + }); } static async exchangeAuthCodeForOAuthTokens(redirectURIInfo, userContext) { - return await recipe_1.default - .getInstanceOrThrowError() - .recipeInterfaceImpl.exchangeAuthCodeForOAuthTokens(redirectURIInfo, userContext); + const recipeInterfaceImpl = recipe_1.default.getInstanceOrThrowError().recipeInterfaceImpl; + const providerConfig = await recipeInterfaceImpl.getProviderConfig({ userContext }); + return await recipeInterfaceImpl.exchangeAuthCodeForOAuthTokens({ + providerConfig, + redirectURIInfo, + userContext, + }); } static async getUserInfo(oAuthTokens, userContext) { + const recipeInterfaceImpl = recipe_1.default.getInstanceOrThrowError().recipeInterfaceImpl; + const providerConfig = await recipeInterfaceImpl.getProviderConfig({ userContext }); return await recipe_1.default .getInstanceOrThrowError() - .recipeInterfaceImpl.getUserInfo(oAuthTokens, userContext); + .recipeInterfaceImpl.getUserInfo({ providerConfig, oAuthTokens, userContext }); } } exports.default = Wrapper; diff --git a/lib/build/recipe/oauth2client/recipeImplementation.js b/lib/build/recipe/oauth2client/recipeImplementation.js index e1d7f6205..8343d5d61 100644 --- a/lib/build/recipe/oauth2client/recipeImplementation.js +++ b/lib/build/recipe/oauth2client/recipeImplementation.js @@ -14,8 +14,7 @@ const jose_1 = require("jose"); function getRecipeImplementation(_querier, config) { let providerConfigWithOIDCInfo = null; return { - getAuthorisationRedirectURL: async function (redirectURIOnProviderDashboard, userContext) { - const providerConfig = await this.getProviderConfig(userContext); + getAuthorisationRedirectURL: async function ({ providerConfig, redirectURIOnProviderDashboard }) { const queryParams = { client_id: providerConfig.clientId, redirect_uri: redirectURIOnProviderDashboard, @@ -40,7 +39,7 @@ function getRecipeImplementation(_querier, config) { pkceCodeVerifier: pkceCodeVerifier, }; }, - signIn: async function (userId, oAuthTokens, rawUserInfoFromProvider, tenantId, userContext) { + signIn: async function ({ userId, tenantId, userContext, oAuthTokens, rawUserInfoFromProvider }) { const user = await __1.getUser(userId, userContext); if (user === undefined) { throw new Error(`Failed to getUser from the userId ${userId} in the ${tenantId} tenant`); @@ -79,8 +78,7 @@ function getRecipeImplementation(_querier, config) { }); return providerConfigWithOIDCInfo; }, - exchangeAuthCodeForOAuthTokens: async function (redirectURIInfo, userContext) { - const providerConfig = await this.getProviderConfig(userContext); + exchangeAuthCodeForOAuthTokens: async function ({ providerConfig, redirectURIInfo }) { if (providerConfig.tokenEndpoint === undefined) { throw new Error("OAuth2Client provider's tokenEndpoint is not configured."); } @@ -108,8 +106,7 @@ function getRecipeImplementation(_querier, config) { } return tokenResponse.jsonResponse; }, - getUserInfo: async function (oAuthTokens, userContext) { - const providerConfig = await this.getProviderConfig(userContext); + getUserInfo: async function ({ providerConfig, oAuthTokens }) { let jwks; const accessToken = oAuthTokens["access_token"]; const idToken = oAuthTokens["id_token"]; diff --git a/lib/build/recipe/oauth2client/types.d.ts b/lib/build/recipe/oauth2client/types.d.ts index 2a4b896fa..d00a4a069 100644 --- a/lib/build/recipe/oauth2client/types.d.ts +++ b/lib/build/recipe/oauth2client/types.d.ts @@ -65,17 +65,18 @@ export declare type TypeNormalisedInput = { }; }; export declare type RecipeInterface = { - getAuthorisationRedirectURL( - redirectURIOnProviderDashboard: string, - userContext: UserContext - ): Promise<{ + getAuthorisationRedirectURL(input: { + providerConfig: ProviderConfigWithOIDCInfo; + redirectURIOnProviderDashboard: string; + userContext: UserContext; + }): Promise<{ urlWithQueryParams: string; pkceCodeVerifier?: string; }>; - getProviderConfig(userContext: UserContext): Promise; - signIn( - userId: string, - oAuthTokens: OAuthTokens, + getProviderConfig(input: { userContext: UserContext }): Promise; + signIn(input: { + userId: string; + oAuthTokens: OAuthTokens; rawUserInfoFromProvider: { fromIdTokenPayload?: { [key: string]: any; @@ -83,10 +84,10 @@ export declare type RecipeInterface = { fromUserInfoAPI?: { [key: string]: any; }; - }, - tenantId: string, - userContext: UserContext - ): Promise<{ + }; + tenantId: string; + userContext: UserContext; + }): Promise<{ status: "OK"; recipeUserId: RecipeUserId; user: User; @@ -100,15 +101,20 @@ export declare type RecipeInterface = { }; }; }>; - exchangeAuthCodeForOAuthTokens( + exchangeAuthCodeForOAuthTokens(input: { + providerConfig: ProviderConfigWithOIDCInfo; redirectURIInfo: { redirectURIOnProviderDashboard: string; redirectURIQueryParams: any; pkceCodeVerifier?: string | undefined; - }, - userContext: UserContext - ): Promise; - getUserInfo(oAuthTokens: OAuthTokens, userContext: UserContext): Promise; + }; + userContext: UserContext; + }): Promise; + getUserInfo(input: { + providerConfig: ProviderConfigWithOIDCInfo; + oAuthTokens: OAuthTokens; + userContext: UserContext; + }): Promise; }; export declare type APIOptions = { recipeImplementation: RecipeInterface; diff --git a/lib/ts/recipe/oauth2/recipe.ts b/lib/ts/recipe/oauth2/recipe.ts index 5aa8a3888..a209cff1c 100644 --- a/lib/ts/recipe/oauth2/recipe.ts +++ b/lib/ts/recipe/oauth2/recipe.ts @@ -55,7 +55,7 @@ export default class Recipe extends RecipeModule { Querier.getNewInstanceOrThrowError(recipeId), this.config, appInfo, - this.getDefaultIdTokenPayload + this.getDefaultIdTokenPayload.bind(this) ) ); this.recipeInterfaceImpl = builder.override(this.config.override.functions).build(); diff --git a/lib/ts/recipe/oauth2client/api/implementation.ts b/lib/ts/recipe/oauth2client/api/implementation.ts index 8e7c6d287..a6f14e0cb 100644 --- a/lib/ts/recipe/oauth2client/api/implementation.ts +++ b/lib/ts/recipe/oauth2client/api/implementation.ts @@ -5,10 +5,13 @@ import { OAuthTokens } from "../types"; export default function getAPIInterface(): APIInterface { return { authorisationUrlGET: async function ({ options, redirectURIOnProviderDashboard, userContext }) { - const authUrl = await options.recipeImplementation.getAuthorisationRedirectURL( + const providerConfig = await options.recipeImplementation.getProviderConfig({ userContext }); + + const authUrl = await options.recipeImplementation.getAuthorisationRedirectURL({ + providerConfig, redirectURIOnProviderDashboard, - userContext - ); + userContext, + }); return { status: "OK", ...authUrl, @@ -17,31 +20,35 @@ export default function getAPIInterface(): APIInterface { signInPOST: async function (input) { const { options, tenantId, userContext } = input; + const providerConfig = await options.recipeImplementation.getProviderConfig({ userContext }); + let oAuthTokensToUse: OAuthTokens = {}; if ("redirectURIInfo" in input && input.redirectURIInfo !== undefined) { - oAuthTokensToUse = await options.recipeImplementation.exchangeAuthCodeForOAuthTokens( - input.redirectURIInfo, - userContext - ); + oAuthTokensToUse = await options.recipeImplementation.exchangeAuthCodeForOAuthTokens({ + providerConfig, + redirectURIInfo: input.redirectURIInfo, + userContext, + }); } else if ("oAuthTokens" in input && input.oAuthTokens !== undefined) { oAuthTokensToUse = input.oAuthTokens; } else { throw Error("should never come here"); } - const { userId, rawUserInfoFromProvider } = await options.recipeImplementation.getUserInfo( - oAuthTokensToUse, - userContext - ); + const { userId, rawUserInfoFromProvider } = await options.recipeImplementation.getUserInfo({ + providerConfig, + oAuthTokens: oAuthTokensToUse, + userContext, + }); - const { user, recipeUserId } = await options.recipeImplementation.signIn( + const { user, recipeUserId } = await options.recipeImplementation.signIn({ userId, - oAuthTokensToUse, - rawUserInfoFromProvider, tenantId, - userContext - ); + rawUserInfoFromProvider, + oAuthTokens: oAuthTokensToUse, + userContext, + }); const session = await Session.createNewSession( options.req, diff --git a/lib/ts/recipe/oauth2client/index.ts b/lib/ts/recipe/oauth2client/index.ts index 6668f312e..4ae33ab34 100644 --- a/lib/ts/recipe/oauth2client/index.ts +++ b/lib/ts/recipe/oauth2client/index.ts @@ -21,10 +21,13 @@ export default class Wrapper { static init = Recipe.init; static async getAuthorisationRedirectURL(redirectURIOnProviderDashboard: string, userContext: UserContext) { - return await Recipe.getInstanceOrThrowError().recipeInterfaceImpl.getAuthorisationRedirectURL( + const recipeInterfaceImpl = Recipe.getInstanceOrThrowError().recipeInterfaceImpl; + const providerConfig = await recipeInterfaceImpl.getProviderConfig({ userContext }); + return await recipeInterfaceImpl.getAuthorisationRedirectURL({ + providerConfig, redirectURIOnProviderDashboard, - userContext - ); + userContext, + }); } static async exchangeAuthCodeForOAuthTokens( @@ -35,14 +38,23 @@ export default class Wrapper { }, userContext: UserContext ) { - return await Recipe.getInstanceOrThrowError().recipeInterfaceImpl.exchangeAuthCodeForOAuthTokens( + const recipeInterfaceImpl = Recipe.getInstanceOrThrowError().recipeInterfaceImpl; + const providerConfig = await recipeInterfaceImpl.getProviderConfig({ userContext }); + return await recipeInterfaceImpl.exchangeAuthCodeForOAuthTokens({ + providerConfig, redirectURIInfo, - userContext - ); + userContext, + }); } static async getUserInfo(oAuthTokens: OAuthTokens, userContext: UserContext) { - return await Recipe.getInstanceOrThrowError().recipeInterfaceImpl.getUserInfo(oAuthTokens, userContext); + const recipeInterfaceImpl = Recipe.getInstanceOrThrowError().recipeInterfaceImpl; + const providerConfig = await recipeInterfaceImpl.getProviderConfig({ userContext }); + return await Recipe.getInstanceOrThrowError().recipeInterfaceImpl.getUserInfo({ + providerConfig, + oAuthTokens, + userContext, + }); } } diff --git a/lib/ts/recipe/oauth2client/recipeImplementation.ts b/lib/ts/recipe/oauth2client/recipeImplementation.ts index eb8fce5dc..ef374b8f6 100644 --- a/lib/ts/recipe/oauth2client/recipeImplementation.ts +++ b/lib/ts/recipe/oauth2client/recipeImplementation.ts @@ -8,7 +8,7 @@ import { } from "./types"; import { Querier } from "../../querier"; import RecipeUserId from "../../recipeUserId"; -import { UserContext, User as UserType } from "../../types"; +import { User as UserType } from "../../types"; import { doGetRequest, doPostRequest, @@ -26,11 +26,8 @@ export default function getRecipeImplementation(_querier: Querier, config: TypeN return { getAuthorisationRedirectURL: async function ( this: RecipeInterface, - redirectURIOnProviderDashboard, - userContext + { providerConfig, redirectURIOnProviderDashboard } ) { - const providerConfig = await this.getProviderConfig(userContext); - const queryParams: { [key: string]: string } = { client_id: providerConfig.clientId, redirect_uri: redirectURIOnProviderDashboard, @@ -61,16 +58,13 @@ export default function getRecipeImplementation(_querier: Querier, config: TypeN pkceCodeVerifier: pkceCodeVerifier, }; }, - signIn: async function ( - userId: string, - oAuthTokens: OAuthTokens, - rawUserInfoFromProvider: { - fromIdTokenPayload?: { [key: string]: any }; - fromUserInfoAPI?: { [key: string]: any }; - }, - tenantId: string, - userContext: UserContext - ): Promise<{ + signIn: async function ({ + userId, + tenantId, + userContext, + oAuthTokens, + rawUserInfoFromProvider, + }): Promise<{ status: "OK"; user: UserType; recipeUserId: RecipeUserId; @@ -123,17 +117,7 @@ export default function getRecipeImplementation(_querier: Querier, config: TypeN }; return providerConfigWithOIDCInfo; }, - exchangeAuthCodeForOAuthTokens: async function ( - this: RecipeInterface, - redirectURIInfo: { - redirectURIOnProviderDashboard: string; - redirectURIQueryParams: any; - pkceCodeVerifier?: string | undefined; - }, - userContext: UserContext - ) { - const providerConfig = await this.getProviderConfig(userContext); - + exchangeAuthCodeForOAuthTokens: async function (this: RecipeInterface, { providerConfig, redirectURIInfo }) { if (providerConfig.tokenEndpoint === undefined) { throw new Error("OAuth2Client provider's tokenEndpoint is not configured."); } @@ -164,9 +148,7 @@ export default function getRecipeImplementation(_querier: Querier, config: TypeN return tokenResponse.jsonResponse as OAuthTokenResponse; }, - getUserInfo: async function (oAuthTokens: OAuthTokens, userContext: UserContext): Promise { - const providerConfig = await this.getProviderConfig(userContext); - + getUserInfo: async function ({ providerConfig, oAuthTokens }): Promise { let jwks: JWTVerifyGetKey | undefined; const accessToken = oAuthTokens["access_token"]; diff --git a/lib/ts/recipe/oauth2client/types.ts b/lib/ts/recipe/oauth2client/types.ts index fa85a0cad..75d3a55c2 100644 --- a/lib/ts/recipe/oauth2client/types.ts +++ b/lib/ts/recipe/oauth2client/types.ts @@ -78,25 +78,26 @@ export type TypeNormalisedInput = { }; export type RecipeInterface = { - getAuthorisationRedirectURL( - redirectURIOnProviderDashboard: string, - userContext: UserContext - ): Promise<{ + getAuthorisationRedirectURL(input: { + providerConfig: ProviderConfigWithOIDCInfo; + redirectURIOnProviderDashboard: string; + userContext: UserContext; + }): Promise<{ urlWithQueryParams: string; pkceCodeVerifier?: string; }>; - getProviderConfig(userContext: UserContext): Promise; + getProviderConfig(input: { userContext: UserContext }): Promise; - signIn( - userId: string, - oAuthTokens: OAuthTokens, + signIn(input: { + userId: string; + oAuthTokens: OAuthTokens; rawUserInfoFromProvider: { fromIdTokenPayload?: { [key: string]: any }; fromUserInfoAPI?: { [key: string]: any }; - }, - tenantId: string, - userContext: UserContext - ): Promise<{ + }; + tenantId: string; + userContext: UserContext; + }): Promise<{ status: "OK"; recipeUserId: RecipeUserId; user: User; @@ -106,15 +107,20 @@ export type RecipeInterface = { fromUserInfoAPI?: { [key: string]: any }; }; }>; - exchangeAuthCodeForOAuthTokens( + exchangeAuthCodeForOAuthTokens(input: { + providerConfig: ProviderConfigWithOIDCInfo; redirectURIInfo: { redirectURIOnProviderDashboard: string; redirectURIQueryParams: any; pkceCodeVerifier?: string | undefined; - }, - userContext: UserContext - ): Promise; - getUserInfo(oAuthTokens: OAuthTokens, userContext: UserContext): Promise; + }; + userContext: UserContext; + }): Promise; + getUserInfo(input: { + providerConfig: ProviderConfigWithOIDCInfo; + oAuthTokens: OAuthTokens; + userContext: UserContext; + }): Promise; }; export type APIOptions = { diff --git a/test/test-server/src/index.ts b/test/test-server/src/index.ts index 7257fa1c0..7ae66ec90 100644 --- a/test/test-server/src/index.ts +++ b/test/test-server/src/index.ts @@ -316,24 +316,6 @@ function initST(config: any) { } recipeList.push(OAuth2.init(initConfig)); } - if (recipe.recipeId === "oauth2") { - let initConfig: OAuth2TypeInput = { - ...config, - }; - if (initConfig.override?.functions) { - initConfig.override = { - ...initConfig.override, - functions: getFunc(`${initConfig.override.functions}`), - }; - } - if (initConfig.override?.apis) { - initConfig.override = { - ...initConfig.override, - apis: getFunc(`${initConfig.override.apis}`), - }; - } - recipeList.push(OAuth2.init(initConfig)); - } if (recipe.recipeId === "oauth2client") { let initConfig: OAuth2ClientTypeInput = { ...config,