Skip to content
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: Add an api to get login info #867

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/build/recipe/oauth2/api/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ function getAPIImplementation() {
tokenPOST: async (input) => {
return input.options.recipeImplementation.token({ body: input.body, userContext: input.userContext });
},
loginInfoGET: async ({ loginChallenge, options, userContext }) => {
const { client } = await options.recipeImplementation.getLoginRequest({
challenge: loginChallenge,
userContext,
});
return {
clientName: client.clientName,
tosUri: client.tosUri,
policyUri: client.policyUri,
logoUri: client.logoUri,
metadata: client.metadata,
};
},
};
}
exports.default = getAPIImplementation;
8 changes: 8 additions & 0 deletions lib/build/recipe/oauth2/api/loginInfo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-nocheck
import { APIInterface, APIOptions } from "..";
import { UserContext } from "../../../types";
export default function loginInfoGET(
apiImplementation: APIInterface,
options: APIOptions,
userContext: UserContext
): Promise<boolean>;
38 changes: 38 additions & 0 deletions lib/build/recipe/oauth2/api/loginInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
/* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../../../utils");
async function loginInfoGET(apiImplementation, options, userContext) {
var _a;
if (apiImplementation.loginInfoGET === undefined) {
return false;
}
const loginChallenge =
(_a = options.req.getKeyValueFromQuery("login_challenge")) !== null && _a !== void 0
? _a
: options.req.getKeyValueFromQuery("loginChallenge");
if (loginChallenge === undefined) {
throw new Error("TODO");
}
let response = await apiImplementation.loginInfoGET({
options,
loginChallenge,
userContext,
});
utils_1.send200Response(options.res, response);
return true;
}
exports.default = loginInfoGET;
1 change: 1 addition & 0 deletions lib/build/recipe/oauth2/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export declare const LOGOUT_PATH = "/oauth2/logout";
export declare const CONSENT_PATH = "/oauth2/consent";
export declare const AUTH_PATH = "/oauth2/auth";
export declare const TOKEN_PATH = "/oauth2/token";
export declare const LOGIN_INFO_PATH = "/oauth2/login/info";
3 changes: 2 additions & 1 deletion lib/build/recipe/oauth2/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TOKEN_PATH = exports.AUTH_PATH = exports.CONSENT_PATH = exports.LOGOUT_PATH = exports.LOGIN_PATH = exports.OAUTH2_BASE_PATH = void 0;
exports.LOGIN_INFO_PATH = exports.TOKEN_PATH = exports.AUTH_PATH = exports.CONSENT_PATH = exports.LOGOUT_PATH = exports.LOGIN_PATH = exports.OAUTH2_BASE_PATH = void 0;
exports.OAUTH2_BASE_PATH = "/oauth2/";
exports.LOGIN_PATH = "/oauth2/login";
exports.LOGOUT_PATH = "/oauth2/logout";
exports.CONSENT_PATH = "/oauth2/consent";
exports.AUTH_PATH = "/oauth2/auth";
exports.TOKEN_PATH = "/oauth2/token";
exports.LOGIN_INFO_PATH = "/oauth2/login/info";
10 changes: 10 additions & 0 deletions lib/build/recipe/oauth2/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const implementation_1 = __importDefault(require("./api/implementation"));
const login_1 = __importDefault(require("./api/login"));
const logout_1 = __importDefault(require("./api/logout"));
const token_1 = __importDefault(require("./api/token"));
const loginInfo_1 = __importDefault(require("./api/loginInfo"));
const constants_1 = require("./constants");
const recipeImplementation_1 = __importDefault(require("./recipeImplementation"));
const utils_1 = require("./utils");
Expand Down Expand Up @@ -60,6 +61,9 @@ class Recipe extends recipeModule_1.default {
if (id === constants_1.AUTH_PATH) {
return auth_1.default(this.apiImpl, options, userContext);
}
if (id === constants_1.LOGIN_INFO_PATH) {
return loginInfo_1.default(this.apiImpl, options, userContext);
}
throw new Error("Should never come here: handleAPIRequest called with unknown id");
};
this.config = utils_1.validateAndNormaliseUserInput(this, appInfo, config);
Expand Down Expand Up @@ -156,6 +160,12 @@ class Recipe extends recipeModule_1.default {
id: constants_1.AUTH_PATH,
disabled: this.apiImpl.authGET === undefined,
},
{
method: "get",
pathWithoutApiBasePath: new normalisedURLPath_1.default(constants_1.LOGIN_INFO_PATH),
id: constants_1.LOGIN_INFO_PATH,
disabled: this.apiImpl.loginInfoGET === undefined,
},
];
}
handleError(error, _, __, _userContext) {
Expand Down
14 changes: 14 additions & 0 deletions lib/build/recipe/oauth2/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ export declare type TokenInfo = {
scope: string;
tokenType: string;
};
export declare type LoginInfo = {
clientName: string;
tosUri: string;
policyUri: string;
logoUri: string;
metadata?: Record<string, any> | null;
};
export declare type RecipeInterface = {
authorization(input: {
params: any;
Expand Down Expand Up @@ -300,6 +307,13 @@ export declare type APIInterface = {
options: APIOptions;
userContext: UserContext;
}) => Promise<TokenInfo | ErrorOAuth2 | GeneralErrorResponse>);
loginInfoGET:
| undefined
| ((input: {
loginChallenge: string;
options: APIOptions;
userContext: UserContext;
}) => Promise<LoginInfo | GeneralErrorResponse>);
};
export declare type OAuth2ClientOptions = {
clientId: string;
Expand Down
14 changes: 14 additions & 0 deletions lib/ts/recipe/oauth2/api/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,19 @@ export default function getAPIImplementation(): APIInterface {
tokenPOST: async (input) => {
return input.options.recipeImplementation.token({ body: input.body, userContext: input.userContext });
},
loginInfoGET: async ({ loginChallenge, options, userContext }) => {
const { client } = await options.recipeImplementation.getLoginRequest({
challenge: loginChallenge,
userContext,
});

return {
clientName: client.clientName,
tosUri: client.tosUri,
policyUri: client.policyUri,
logoUri: client.logoUri,
metadata: client.metadata,
};
},
};
}
44 changes: 44 additions & 0 deletions lib/ts/recipe/oauth2/api/loginInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (c) 2024, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

import { send200Response } from "../../../utils";
import { APIInterface, APIOptions } from "..";
import { UserContext } from "../../../types";

export default async function loginInfoGET(
apiImplementation: APIInterface,
options: APIOptions,
userContext: UserContext
): Promise<boolean> {
if (apiImplementation.loginInfoGET === undefined) {
return false;
}

const loginChallenge =
options.req.getKeyValueFromQuery("login_challenge") ?? options.req.getKeyValueFromQuery("loginChallenge");

if (loginChallenge === undefined) {
throw new Error("TODO");
}

let response = await apiImplementation.loginInfoGET({
options,
loginChallenge,
userContext,
});

send200Response(options.res, response);
return true;
}
1 change: 1 addition & 0 deletions lib/ts/recipe/oauth2/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export const LOGOUT_PATH = "/oauth2/logout";
export const CONSENT_PATH = "/oauth2/consent";
export const AUTH_PATH = "/oauth2/auth";
export const TOKEN_PATH = "/oauth2/token";
export const LOGIN_INFO_PATH = "/oauth2/login/info";
12 changes: 11 additions & 1 deletion lib/ts/recipe/oauth2/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import APIImplementation from "./api/implementation";
import loginAPI from "./api/login";
import logoutAPI from "./api/logout";
import tokenPOST from "./api/token";
import { AUTH_PATH, CONSENT_PATH, LOGIN_PATH, LOGOUT_PATH, TOKEN_PATH } from "./constants";
import loginInfoGET from "./api/loginInfo";
import { AUTH_PATH, CONSENT_PATH, LOGIN_INFO_PATH, LOGIN_PATH, LOGOUT_PATH, TOKEN_PATH } from "./constants";
import RecipeImplementation from "./recipeImplementation";
import { APIInterface, RecipeInterface, TypeInput, TypeNormalisedInput } from "./types";
import { validateAndNormaliseUserInput } from "./utils";
Expand Down Expand Up @@ -140,6 +141,12 @@ export default class Recipe extends RecipeModule {
id: AUTH_PATH,
disabled: this.apiImpl.authGET === undefined,
},
{
method: "get",
pathWithoutApiBasePath: new NormalisedURLPath(LOGIN_INFO_PATH),
id: LOGIN_INFO_PATH,
disabled: this.apiImpl.loginInfoGET === undefined,
},
];
}

Expand Down Expand Up @@ -176,6 +183,9 @@ export default class Recipe extends RecipeModule {
if (id === AUTH_PATH) {
return authGET(this.apiImpl, options, userContext);
}
if (id === LOGIN_INFO_PATH) {
return loginInfoGET(this.apiImpl, options, userContext);
}
throw new Error("Should never come here: handleAPIRequest called with unknown id");
};

Expand Down
20 changes: 20 additions & 0 deletions lib/ts/recipe/oauth2/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ export type TokenInfo = {
tokenType: string;
};

export type LoginInfo = {
// The name of the client.
clientName: string;
// The URI of the client's terms of service.
tosUri: string;
// The URI of the client's privacy policy.
policyUri: string;
// The URI of the client's logo.
logoUri: string;
// The metadata associated with the client.
metadata?: Record<string, any> | null;
};

export type RecipeInterface = {
authorization(input: {
params: any;
Expand Down Expand Up @@ -392,6 +405,13 @@ export type APIInterface = {
options: APIOptions;
userContext: UserContext;
}) => Promise<TokenInfo | ErrorOAuth2 | GeneralErrorResponse>);
loginInfoGET:
| undefined
| ((input: {
loginChallenge: string;
options: APIOptions;
userContext: UserContext;
}) => Promise<LoginInfo | GeneralErrorResponse>);
};

export type OAuth2ClientOptions = {
Expand Down
Loading