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(OAuth2API): add revokeToken method #10440

Merged
merged 8 commits into from
Aug 20, 2024
Merged
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions packages/core/src/api/oauth2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable jsdoc/check-param-names */

import { Buffer } from 'node:buffer';
import { type RequestData, type REST, makeURLSearchParams } from '@discordjs/rest';
import {
Routes,
Expand All @@ -13,6 +14,8 @@ import {
type RESTGetAPIOAuth2CurrentApplicationResult,
type RESTPostOAuth2AccessTokenURLEncodedData,
type RESTPostOAuth2AccessTokenResult,
type RESTPostOAuth2TokenRevocation,
type Snowflake,
} from 'discord-api-types/v10';

export class OAuth2API {
Expand Down Expand Up @@ -121,4 +124,24 @@ export class OAuth2API {
signal,
}) as Promise<RESTGetAPIOAuth2CurrentAuthorizationResult>;
}

/**
* Revokes an OAuth2 token
*
* @see {@link https://discord.com/developers/docs/topics/oauth2#authorization-code-grant-token-revocation-example}
* @param body - The body of the token revocation request
* @param options - The options for the token revocation request
*/
public async revokeToken(applicationId: Snowflake, applicationSecret: string, body: RESTPostOAuth2TokenRevocation, { signal }: Pick<RequestData, 'signal'> = {}) {
return this.rest.post(Routes.oauth2TokenRevocation(), {
Larsundso marked this conversation as resolved.
Show resolved Hide resolved
body: makeURLSearchParams(body),
passThroughBody: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(`${applicationId}:${applicationSecret}`).toString('base64')}`,
Larsundso marked this conversation as resolved.
Show resolved Hide resolved
},
auth: false,
signal,
}) as Promise<{}>;
Larsundso marked this conversation as resolved.
Show resolved Hide resolved
}
}