diff --git a/README.md b/README.md index 7703863..806daa7 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,48 @@ oauth.getGuildMember(access_token, guildId).then(console.log); */ ``` +### `getCurrentAuthorizationInformation(access_token)` + +`access_token`: The user's access token. + +Returns info about the current authorization. Includes the [user](https://discord.com/developers/docs/resources/user#user-object) object of the requester's account if they authorized with the `identify` scope. + +```js +const DiscordOauth2 = require("discord-oauth2"); +const oauth = new DiscordOauth2(); + +const access_token = "6qrZcUqja7812RVdnEKjpzOL4CvHBFG"; + +oauth.getCurrentAuthorizationInformation(access_token).then(console.log); +/* + { + "application": { + "id": "159799960412356608", + "name": "AIRHORN SOLUTIONS", + "icon": "f03590d3eb764081d154a66340ea7d6d", + "description": "", + "hook": true, + "bot_public": true, + "bot_require_code_grant": false, + "verify_key": "c8cde6a3c8c6e49d86af3191287b3ce255872be1fff6dc285bdb420c06a2c3c8" + }, + "scopes": [ + "guilds.join", + "identify" + ], + "expires": "2021-01-23T02:33:17.017000+00:00", + "user": { + "id": "268473310986240001", + "username": "discord", + "avatar": "f749bb0cbeeb26ef21eca719337d20f1", + "discriminator": "0", + "global_name": "Discord", + "public_flags": 131072 + } + } +*/ +``` + ### `generateAuthUrl(object)` Dynamically generate an OAuth2 URL. diff --git a/index.d.ts b/index.d.ts index da0b518..fe95341 100644 --- a/index.d.ts +++ b/index.d.ts @@ -51,7 +51,7 @@ declare namespace OAuth { synced_at: number; subscriber_count: number; revoked: boolean; - application?: Application; + application?: IntegrationApplication; } export interface Connection { @@ -66,15 +66,25 @@ declare namespace OAuth { visibility: 0 | 1; } - export interface Application { + export interface IntegrationApplication { id: string; name: string; icon: string | null | undefined; description: string; - summary: string; bot?: User; } + export interface PartialApplication { + id: string; + name: string; + icon: string | null | undefined; + description: string; + hook?: boolean | null | undefined; + bot_public: boolean; + bot_require_code_grant: boolean; + verify_key: string; + } + export interface TokenRequestResult { access_token: string; token_type: string; @@ -84,6 +94,13 @@ declare namespace OAuth { webhook?: Webhook; } + export interface AuthorizationInformation { + application: PartialApplication; + scopes: string[]; + expires: string; + user?: User; + } + export interface PartialGuild { id: string; name: string; @@ -167,6 +184,7 @@ declare class OAuth extends EventEmitter { clientSecret?: string; }): Promise; revokeToken(access_token: string, credentials?: string): Promise; + getCurrentAuthorizationInformation(access_token: string): Promise; getUser(access_token: string): Promise; getUserGuilds(access_token: string, opts?: { before?: string; diff --git a/lib/oauth.js b/lib/oauth.js index d9fe47f..de5be5b 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -135,6 +135,20 @@ class OAuth extends RequestHandler { ); } + /** + * Request info about the current authorization + * @arg {String} access_token The user access token + * @returns {Promise} + */ + getCurrentAuthorizationInformation(access_token) { + return this.request("GET", "/oauth2/@me", undefined, { + auth: { + type: "Bearer", + creds: access_token, + }, + }); + } + /** * Request basic user data * Requires the `identify` scope