-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add userInfoGET endpoint (#890)
* feat: add initial oauth2 client apis * feat: Add an api to get login info * fix: merge issues and FE path * fix: WIP fix for CSRF and redirection issues * fix: OAuth2 fixes and test-server updates (#871) * feat: update oauth2 login info endpoint types to match our general patterns * fix: make login flow work * fix: circular dependency * feat: Add OAuth2Client recipe * fix: PR changes * fix: PR changes * fix: PR changes * feat: Add userInfoGET endpoint * fix: PR changes * fix: PR changes * fix: PR changes --------- Co-authored-by: Mihaly Lengyel <[email protected]>
- Loading branch information
Showing
24 changed files
with
534 additions
and
42 deletions.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// @ts-nocheck | ||
import { APIInterface, APIOptions } from ".."; | ||
import { UserContext } from "../../../types"; | ||
export default function userInfoGET( | ||
apiImplementation: APIInterface, | ||
tenantId: string, | ||
options: APIOptions, | ||
userContext: UserContext | ||
): Promise<boolean>; |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"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"); | ||
const __1 = require("../../.."); | ||
// TODO: Replace stub implementation by the actual implementation | ||
async function validateOAuth2AccessToken(accessToken) { | ||
const resp = await fetch(`http://localhost:4445/admin/oauth2/introspect`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
body: new URLSearchParams({ token: accessToken }), | ||
}); | ||
return await resp.json(); | ||
} | ||
async function userInfoGET(apiImplementation, tenantId, options, userContext) { | ||
if (apiImplementation.userInfoGET === undefined) { | ||
return false; | ||
} | ||
const authHeader = options.req.getHeaderValue("authorization") || options.req.getHeaderValue("Authorization"); | ||
if (authHeader === undefined || !authHeader.startsWith("Bearer ")) { | ||
// TODO: Returning a 400 instead of a 401 to prevent a potential refresh loop in the client SDK. | ||
// When addressing this TODO, review other response codes in this function as well. | ||
utils_1.sendNon200ResponseWithMessage(options.res, "Missing or invalid Authorization header", 400); | ||
return true; | ||
} | ||
const accessToken = authHeader.replace(/^Bearer /, "").trim(); | ||
let accessTokenPayload; | ||
try { | ||
accessTokenPayload = await validateOAuth2AccessToken(accessToken); | ||
} catch (error) { | ||
options.res.setHeader("WWW-Authenticate", 'Bearer error="invalid_token"', false); | ||
utils_1.sendNon200ResponseWithMessage(options.res, "Invalid or expired OAuth2 access token", 400); | ||
return true; | ||
} | ||
if ( | ||
accessTokenPayload === null || | ||
typeof accessTokenPayload !== "object" || | ||
typeof accessTokenPayload.sub !== "string" || | ||
typeof accessTokenPayload.scope !== "string" | ||
) { | ||
options.res.setHeader("WWW-Authenticate", 'Bearer error="invalid_token"', false); | ||
utils_1.sendNon200ResponseWithMessage(options.res, "Malformed access token payload", 400); | ||
return true; | ||
} | ||
const userId = accessTokenPayload.sub; | ||
const user = await __1.getUser(userId, userContext); | ||
if (user === undefined) { | ||
options.res.setHeader("WWW-Authenticate", 'Bearer error="invalid_token"', false); | ||
utils_1.sendNon200ResponseWithMessage( | ||
options.res, | ||
"Couldn't find any user associated with the access token", | ||
400 | ||
); | ||
return true; | ||
} | ||
const response = await apiImplementation.userInfoGET({ | ||
accessTokenPayload, | ||
user, | ||
tenantId, | ||
scopes: accessTokenPayload.scope.split(" "), | ||
options, | ||
userContext, | ||
}); | ||
utils_1.send200Response(options.res, response); | ||
return true; | ||
} | ||
exports.default = userInfoGET; |
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
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
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,10 +1,11 @@ | ||
// @ts-nocheck | ||
import { Querier } from "../../querier"; | ||
import { NormalisedAppinfo } from "../../types"; | ||
import { RecipeInterface, TypeNormalisedInput, PayloadBuilderFunction } from "./types"; | ||
import { RecipeInterface, TypeNormalisedInput, PayloadBuilderFunction, UserInfoBuilderFunction } from "./types"; | ||
export default function getRecipeInterface( | ||
querier: Querier, | ||
_config: TypeNormalisedInput, | ||
_appInfo: NormalisedAppinfo, | ||
getDefaultIdTokenPayload: PayloadBuilderFunction | ||
getDefaultIdTokenPayload: PayloadBuilderFunction, | ||
getDefaultUserInfoPayload: UserInfoBuilderFunction | ||
): RecipeInterface; |
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
Oops, something went wrong.