Skip to content

Commit

Permalink
fix: PR changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anku255 committed Jul 15, 2024
1 parent 51f4c9f commit c5e6988
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 136 deletions.
2 changes: 1 addition & 1 deletion lib/build/recipe/oauth2/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
37 changes: 21 additions & 16 deletions lib/build/recipe/oauth2client/api/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 17 additions & 7 deletions lib/build/recipe/oauth2client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 4 additions & 7 deletions lib/build/recipe/oauth2client/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`);
Expand Down Expand Up @@ -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.");
}
Expand Down Expand Up @@ -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"];
Expand Down
40 changes: 23 additions & 17 deletions lib/build/recipe/oauth2client/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,29 @@ 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<ProviderConfigWithOIDCInfo>;
signIn(
userId: string,
oAuthTokens: OAuthTokens,
getProviderConfig(input: { userContext: UserContext }): Promise<ProviderConfigWithOIDCInfo>;
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;
Expand All @@ -100,15 +101,20 @@ export declare type RecipeInterface = {
};
};
}>;
exchangeAuthCodeForOAuthTokens(
exchangeAuthCodeForOAuthTokens(input: {
providerConfig: ProviderConfigWithOIDCInfo;
redirectURIInfo: {
redirectURIOnProviderDashboard: string;
redirectURIQueryParams: any;
pkceCodeVerifier?: string | undefined;
},
userContext: UserContext
): Promise<OAuthTokenResponse>;
getUserInfo(oAuthTokens: OAuthTokens, userContext: UserContext): Promise<UserInfo>;
};
userContext: UserContext;
}): Promise<OAuthTokenResponse>;
getUserInfo(input: {
providerConfig: ProviderConfigWithOIDCInfo;
oAuthTokens: OAuthTokens;
userContext: UserContext;
}): Promise<UserInfo>;
};
export declare type APIOptions = {
recipeImplementation: RecipeInterface;
Expand Down
2 changes: 1 addition & 1 deletion lib/ts/recipe/oauth2/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 23 additions & 16 deletions lib/ts/recipe/oauth2client/api/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
26 changes: 19 additions & 7 deletions lib/ts/recipe/oauth2client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
});
}
}

Expand Down
Loading

0 comments on commit c5e6988

Please sign in to comment.