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 token revocation endpoint #902

Merged
merged 9 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions lib/build/querier.js

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

9 changes: 9 additions & 0 deletions lib/build/recipe/oauth2provider/api/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ function getAPIImplementation() {
userContext,
});
},
revokeTokenPOST: async (input) => {
return input.options.recipeImplementation.revokeToken({
token: input.token,
clientId: input.clientId,
clientSecret: input.clientSecret,
authorizationHeader: input.authorizationHeader,
userContext: input.userContext,
});
},
};
}
exports.default = getAPIImplementation;
8 changes: 8 additions & 0 deletions lib/build/recipe/oauth2provider/api/revokeToken.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 revokeTokenPOST(
apiImplementation: APIInterface,
options: APIOptions,
userContext: UserContext
): Promise<boolean>;
40 changes: 40 additions & 0 deletions lib/build/recipe/oauth2provider/api/revokeToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"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 revokeTokenPOST(apiImplementation, options, userContext) {
if (apiImplementation.revokeTokenPOST === undefined) {
return false;
}
const body = await options.req.getFormData();
if (body.token === undefined) {
utils_1.sendNon200ResponseWithMessage(options.res, "token is required in the request body", 400);
return true;
}
const authorizationHeader =
options.req.getHeaderValue("authorization") || options.req.getHeaderValue("Authorization");
let response = await apiImplementation.revokeTokenPOST({
options,
authorizationHeader,
token: body.token,
clientId: body.client_id,
clientSecret: body.client_secret,
userContext,
});
utils_1.send200Response(options.res, response);
return true;
}
exports.default = revokeTokenPOST;
1 change: 1 addition & 0 deletions lib/build/recipe/oauth2provider/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export declare const AUTH_PATH = "/oauth/auth";
export declare const TOKEN_PATH = "/oauth/token";
export declare const LOGIN_INFO_PATH = "/oauth/login/info";
export declare const USER_INFO_PATH = "/oauth/userinfo";
export declare const REVOKE_TOKEN_PATH = "/oauth/revoke";
3 changes: 2 additions & 1 deletion lib/build/recipe/oauth2provider/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.USER_INFO_PATH = exports.LOGIN_INFO_PATH = exports.TOKEN_PATH = exports.AUTH_PATH = exports.LOGIN_PATH = exports.OAUTH2_BASE_PATH = void 0;
exports.REVOKE_TOKEN_PATH = exports.USER_INFO_PATH = exports.LOGIN_INFO_PATH = exports.TOKEN_PATH = exports.AUTH_PATH = exports.LOGIN_PATH = exports.OAUTH2_BASE_PATH = void 0;
exports.OAUTH2_BASE_PATH = "/oauth/";
exports.LOGIN_PATH = "/oauth/login";
exports.AUTH_PATH = "/oauth/auth";
exports.TOKEN_PATH = "/oauth/token";
exports.LOGIN_INFO_PATH = "/oauth/login/info";
exports.USER_INFO_PATH = "/oauth/userinfo";
exports.REVOKE_TOKEN_PATH = "/oauth/revoke";
10 changes: 10 additions & 0 deletions lib/build/recipe/oauth2provider/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ export default class Wrapper {
audience?: string,
userContext?: Record<string, any>
): Promise<import("./types").ErrorOAuth2 | import("./types").TokenInfo>;
static revokeToken(
token: string,
clientId: string,
clientSecret: string,
useBasicAuth?: boolean,
userContext?: Record<string, any>
): Promise<{
status: "OK";
}>;
}
export declare let init: typeof Recipe.init;
export declare let getOAuth2Clients: typeof Wrapper.getOAuth2Clients;
Expand All @@ -108,4 +117,5 @@ export declare let deleteOAuth2Client: typeof Wrapper.deleteOAuth2Client;
export declare let validateOAuth2AccessToken: typeof Wrapper.validateOAuth2AccessToken;
export declare let validateOAuth2IdToken: typeof Wrapper.validateOAuth2IdToken;
export declare let createTokenForClientCredentials: typeof Wrapper.createTokenForClientCredentials;
export declare let revokeToken: typeof Wrapper.revokeToken;
export type { APIInterface, APIOptions, RecipeInterface };
16 changes: 15 additions & 1 deletion lib/build/recipe/oauth2provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var __importDefault =
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTokenForClientCredentials = exports.validateOAuth2IdToken = exports.validateOAuth2AccessToken = exports.deleteOAuth2Client = exports.updateOAuth2Client = exports.createOAuth2Client = exports.getOAuth2Clients = exports.init = void 0;
exports.revokeToken = exports.createTokenForClientCredentials = exports.validateOAuth2IdToken = exports.validateOAuth2AccessToken = exports.deleteOAuth2Client = exports.updateOAuth2Client = exports.createOAuth2Client = exports.getOAuth2Clients = exports.init = void 0;
const utils_1 = require("../../utils");
const recipe_1 = __importDefault(require("./recipe"));
class Wrapper {
Expand Down Expand Up @@ -71,6 +71,19 @@ class Wrapper {
userContext: utils_1.getUserContext(userContext),
});
}
static revokeToken(token, clientId, clientSecret, useBasicAuth = false, userContext) {
let authorizationHeader = undefined;
if (useBasicAuth) {
authorizationHeader = "Basic " + Buffer.from(clientId + ":" + clientSecret).toString("base64");
}
return recipe_1.default.getInstanceOrThrowError().recipeInterfaceImpl.revokeToken({
token,
clientId,
clientSecret,
authorizationHeader,
userContext: utils_1.getUserContext(userContext),
});
}
}
exports.default = Wrapper;
Wrapper.init = recipe_1.default.init;
Expand All @@ -82,3 +95,4 @@ exports.deleteOAuth2Client = Wrapper.deleteOAuth2Client;
exports.validateOAuth2AccessToken = Wrapper.validateOAuth2AccessToken;
exports.validateOAuth2IdToken = Wrapper.validateOAuth2IdToken;
exports.createTokenForClientCredentials = Wrapper.createTokenForClientCredentials;
exports.revokeToken = Wrapper.revokeToken;
10 changes: 10 additions & 0 deletions lib/build/recipe/oauth2provider/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const utils_1 = require("./utils");
const supertokens_js_override_1 = __importDefault(require("supertokens-js-override"));
const userInfo_1 = __importDefault(require("./api/userInfo"));
const combinedRemoteJWKSet_1 = require("../../combinedRemoteJWKSet");
const revokeToken_1 = __importDefault(require("./api/revokeToken"));
class Recipe extends recipeModule_1.default {
constructor(recipeId, appInfo, isInServerlessEnv, config) {
super(recipeId, appInfo);
Expand Down Expand Up @@ -66,6 +67,9 @@ class Recipe extends recipeModule_1.default {
if (id === constants_1.USER_INFO_PATH) {
return userInfo_1.default(this.apiImpl, tenantId, options, userContext);
}
if (id === constants_1.REVOKE_TOKEN_PATH) {
return revokeToken_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 @@ -147,6 +151,12 @@ class Recipe extends recipeModule_1.default {
id: constants_1.USER_INFO_PATH,
disabled: this.apiImpl.userInfoGET === undefined,
},
{
method: "post",
pathWithoutApiBasePath: new normalisedURLPath_1.default(constants_1.REVOKE_TOKEN_PATH),
id: constants_1.REVOKE_TOKEN_PATH,
disabled: this.apiImpl.revokeTokenPOST === undefined,
},
];
}
handleError(error, _, __, _userContext) {
Expand Down
14 changes: 14 additions & 0 deletions lib/build/recipe/oauth2provider/recipeImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,20 @@ function getRecipeInterface(querier, _config, appInfo, getDefaultIdTokenPayload,
}
return { status: "OK", payload: payload };
},
revokeToken: async function ({ clientId, clientSecret, token, authorizationHeader, userContext }) {
const res = await querier.sendPostRequest(
new normalisedURLPath_1.default(`/recipe/oauth2/pub/revoke`),
Object.assign(
Object.assign(
{ $isFormData: true, authorizationHeader },
authorizationHeader === undefined && { client_id: clientId, client_secret: clientSecret }
),
{ token }
),
userContext
);
return res.data;
},
};
}
exports.default = getRecipeInterface;
24 changes: 24 additions & 0 deletions lib/build/recipe/oauth2provider/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ export declare type RecipeInterface = {
tenantId: string;
userContext: UserContext;
}): Promise<JSONObject>;
revokeToken(input: {
token: string;
clientId?: string;
clientSecret?: string;
authorizationHeader?: string;
userContext: UserContext;
}): Promise<{
status: "OK";
}>;
};
export declare type APIInterface = {
loginGET:
Expand Down Expand Up @@ -306,6 +315,21 @@ export declare type APIInterface = {
options: APIOptions;
userContext: UserContext;
}) => Promise<JSONObject | GeneralErrorResponse>);
revokeTokenPOST:
| undefined
| ((input: {
token: string;
clientId?: string;
clientSecret?: string;
authorizationHeader?: string;
options: APIOptions;
userContext: UserContext;
}) => Promise<
| {
status: "OK";
}
| GeneralErrorResponse
>);
};
export declare type OAuth2ClientOptions = {
clientId: string;
Expand Down
7 changes: 7 additions & 0 deletions lib/ts/querier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ export class Querier {
} else {
headers["content-type"] = "application/json; charset=utf-8";
}

// TODO: Remove this after core changes are done
if (body !== undefined && body["authorizationHeader"]) {
headers["authorization"] = body["authorizationHeader"];
delete body["authorizationHeader"];
}

if (Querier.apiKey !== undefined) {
headers = {
...headers,
Expand Down
9 changes: 9 additions & 0 deletions lib/ts/recipe/oauth2provider/api/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,14 @@ export default function getAPIImplementation(): APIInterface {
userContext,
});
},
revokeTokenPOST: async (input) => {
return input.options.recipeImplementation.revokeToken({
token: input.token,
clientId: input.clientId,
clientSecret: input.clientSecret,
authorizationHeader: input.authorizationHeader,
userContext: input.userContext,
});
},
};
}
50 changes: 50 additions & 0 deletions lib/ts/recipe/oauth2provider/api/revokeToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* 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, sendNon200ResponseWithMessage } from "../../../utils";
import { APIInterface, APIOptions } from "..";
import { UserContext } from "../../../types";

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

const body = await options.req.getFormData();

if (body.token === undefined) {
porcellus marked this conversation as resolved.
Show resolved Hide resolved
sendNon200ResponseWithMessage(options.res, "token is required in the request body", 400);
return true;
}

const authorizationHeader =
options.req.getHeaderValue("authorization") || options.req.getHeaderValue("Authorization");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need both casings, the util should normalize it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


let response = await apiImplementation.revokeTokenPOST({
porcellus marked this conversation as resolved.
Show resolved Hide resolved
options,
authorizationHeader,
token: body.token,
clientId: body.client_id,
clientSecret: body.client_secret,
userContext,
});

send200Response(options.res, response);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to set the WWW-Authorize header in the response? What happens if the auth params are missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided not to set it as couldn't find it anywhere in spec.

return true;
}
1 change: 1 addition & 0 deletions lib/ts/recipe/oauth2provider/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export const AUTH_PATH = "/oauth/auth";
export const TOKEN_PATH = "/oauth/token";
export const LOGIN_INFO_PATH = "/oauth/login/info";
export const USER_INFO_PATH = "/oauth/userinfo";
export const REVOKE_TOKEN_PATH = "/oauth/revoke";
24 changes: 24 additions & 0 deletions lib/ts/recipe/oauth2provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ export default class Wrapper {
userContext: getUserContext(userContext),
});
}

static revokeToken(
token: string,
clientId: string,
clientSecret: string,
useBasicAuth = false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the use passes the wrong value here? How does this interact with clients that do not have clientSecrets?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We fixed it by fetching the clientInfo and passing the values correctly.

userContext?: Record<string, any>
) {
let authorizationHeader: string | undefined = undefined;

if (useBasicAuth) {
authorizationHeader = "Basic " + Buffer.from(clientId + ":" + clientSecret).toString("base64");
}

return Recipe.getInstanceOrThrowError().recipeInterfaceImpl.revokeToken({
token,
clientId,
clientSecret,
authorizationHeader,
userContext: getUserContext(userContext),
});
}
}

export let init = Wrapper.init;
Expand All @@ -125,4 +147,6 @@ export let validateOAuth2IdToken = Wrapper.validateOAuth2IdToken;

export let createTokenForClientCredentials = Wrapper.createTokenForClientCredentials;

export let revokeToken = Wrapper.revokeToken;

export type { APIInterface, APIOptions, RecipeInterface };
Loading
Loading