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 OAuth2Client recipe #877

Merged
merged 18 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
test/test-server/node_modules
/examples/**/node_modules
.DS_Store
/.history
Expand All @@ -12,4 +13,4 @@ releasePassword
/test_report
/temp_test_exports
/temp_*
/.nyc_output
/.nyc_output
10 changes: 9 additions & 1 deletion lib/build/querier.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 31 additions & 14 deletions lib/build/querier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/build/recipe/emailpassword/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function getRecipeInterface(querier, getEmailPasswordConfig) {
email: input.email,
password: input.password,
},
{},
input.userContext
);
if (response.status === "OK") {
Expand Down
9 changes: 9 additions & 0 deletions lib/build/recipe/jwt/api/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ function getAPIImplementation() {
if (resp.validityInSeconds !== undefined) {
options.res.setHeader("Cache-Control", `max-age=${resp.validityInSeconds}, must-revalidate`, false);
}
const oauth2 = require("../../oauth2");
// TODO: dirty hack until we get core support
if (oauth2 !== undefined) {
const oauth2JWKSRes = await fetch("http://localhost:4444/.well-known/jwks.json");
if (oauth2JWKSRes.ok) {
const oauth2RespBody = await oauth2JWKSRes.json();
resp.keys = resp.keys.concat(oauth2RespBody.keys);
}
}
return {
keys: resp.keys,
};
Expand Down
1 change: 1 addition & 0 deletions lib/build/recipe/jwt/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function getRecipeInterface(querier, config, appInfo) {
const { body, headers } = await querier.sendGetRequestWithResponseHeaders(
new normalisedURLPath_1.default("/.well-known/jwks.json"),
{},
undefined,
userContext
);
let validityInSeconds = defaultJWKSMaxAge;
Expand Down
2 changes: 2 additions & 0 deletions lib/build/recipe/multitenancy/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function getRecipeInterface(querier) {
let response = await querier.sendPutRequest(
new normalisedURLPath_1.default(`/recipe/multitenancy/tenant`),
Object.assign({ tenantId }, config),
{},
userContext
);
return response;
Expand Down Expand Up @@ -62,6 +63,7 @@ function getRecipeInterface(querier) {
config,
skipValidation,
},
{},
userContext
);
return response;
Expand Down
8 changes: 8 additions & 0 deletions lib/build/recipe/oauth2/api/auth.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 authGET(
apiImplementation: APIInterface,
options: APIOptions,
userContext: UserContext
): Promise<boolean>;
70 changes: 70 additions & 0 deletions lib/build/recipe/oauth2/api/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"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.
*/
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../../../utils");
const set_cookie_parser_1 = __importDefault(require("set-cookie-parser"));
const session_1 = __importDefault(require("../../session"));
async function authGET(apiImplementation, options, userContext) {
if (apiImplementation.authGET === undefined) {
return false;
}
const origURL = options.req.getOriginalURL();
const splitURL = origURL.split("?");
const params = new URLSearchParams(splitURL[1]);
let session;
try {
session = await session_1.default.getSession(options.req, options.res, { sessionRequired: false }, userContext);
} catch (_a) {
// TODO: explain
// ignore
}
let response = await apiImplementation.authGET({
options,
params: Object.fromEntries(params.entries()),
cookie: options.req.getHeaderValue("cookie"),
session,
userContext,
});
if ("redirectTo" in response) {
// TODO:
if (response.setCookie) {
const cookieStr = set_cookie_parser_1.default.splitCookiesString(response.setCookie);
const cookies = set_cookie_parser_1.default.parse(cookieStr);
for (const cookie of cookies) {
options.res.setCookie(
cookie.name,
cookie.value,
cookie.domain,
!!cookie.secure,
!!cookie.httpOnly,
new Date(cookie.expires).getTime(),
cookie.path || "/",
cookie.sameSite
);
}
}
options.res.original.redirect(response.redirectTo);
} else {
utils_1.send200Response(options.res, response);
}
return true;
}
exports.default = authGET;
8 changes: 8 additions & 0 deletions lib/build/recipe/oauth2/api/consent.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 consent(
apiImplementation: APIInterface,
options: APIOptions,
userContext: UserContext
): Promise<boolean>;
63 changes: 63 additions & 0 deletions lib/build/recipe/oauth2/api/consent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"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");
// TODO: separate post and get?
async function consent(apiImplementation, options, userContext) {
var _a;
if (utils_1.normaliseHttpMethod(options.req.getMethod()) === "post") {
if (apiImplementation.consentPOST === undefined) {
return false;
}
const reqBody = await options.req.getJSONBody();
let response = await apiImplementation.consentPOST({
options,
accept: reqBody.accept,
consentChallenge: reqBody.consentChallenge,
grantScope: reqBody.grantScope,
remember: reqBody.remember,
userContext,
});
if ("status" in response) {
utils_1.send200Response(options.res, response);
} else {
options.res.original.redirect(response.redirectTo);
}
} else {
if (apiImplementation.consentGET === undefined) {
return false;
}
const consentChallenge =
(_a = options.req.getKeyValueFromQuery("consentChallenge")) !== null && _a !== void 0
? _a
: options.req.getKeyValueFromQuery("consent_challenge");
if (consentChallenge === undefined) {
throw new Error("TODO");
}
let response = await apiImplementation.consentGET({
options,
consentChallenge,
userContext,
});
if ("status" in response) {
utils_1.send200Response(options.res, response);
} else {
options.res.original.redirect(response.redirectTo);
}
}
return true;
}
exports.default = consent;
Loading
Loading