From 236ec53a6900595112142ee7daa33eaa87967312 Mon Sep 17 00:00:00 2001 From: MarkusRost <37275477+Markus-Rost@users.noreply.github.com> Date: Wed, 3 Apr 2024 18:53:00 +0200 Subject: [PATCH 1/4] add integrationType to generateAuthUrl --- index.d.ts | 1 + lib/oauth.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 9de71e4..da0b518 100644 --- a/index.d.ts +++ b/index.d.ts @@ -197,6 +197,7 @@ declare class OAuth extends EventEmitter { redirectUri?: string; responseType?: "code" | "token"; permissions?: string; + integrationType?: 1 | 0; guildId?: string; disableGuildSelect?: boolean; }): string; diff --git a/lib/oauth.js b/lib/oauth.js index c408f1c..d9fe47f 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -263,7 +263,8 @@ class OAuth extends RequestHandler { * @arg {String?} options.responseType The response type, either code or token (token is for client-side web applications only). Defaults to code * @arg {String | Array} options.scope The scopes for your URL * @arg {String?} options.state A unique cryptographically secure string (https://discord.com/developers/docs/topics/oauth2#state-and-security) - * @arg {Number?} options.permissions The permissions number for the bot invite (only with bot scope) (https://discord.com/developers/docs/topics/permissions) + * @arg {String?} options.permissions The permissions number for the bot invite (only with bot scope) (https://discord.com/developers/docs/topics/permissions) + * @arg {Number?} options.integrationType The installation context for the authorization, either 0 for guild or 1 for user install (only with applications.commands scope) (https://discord.com/developers/docs/resources/application#installation-context) * @arg {String?} options.guildId The guild id to pre-fill the bot invite (only with bot scope) * @arg {Boolean?} options.disableGuildSelect Disallows the user from changing the guild for the bot invite, either true or false (only with bot scope) * @returns {String} @@ -279,6 +280,7 @@ class OAuth extends RequestHandler { ? options.scope.join(" ") : options.scope, permissions: options.permissions, + integration_type: options.integrationType, guild_id: options.guildId, disable_guild_select: options.disableGuildSelect, state: options.state, From 7c3259b164f90b4e5684c8982de9e27eec3ecf11 Mon Sep 17 00:00:00 2001 From: MarkusRost <37275477+Markus-Rost@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:48:16 +0200 Subject: [PATCH 2/4] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6aff62b..7703863 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,8 @@ Takes an object with the following properties: `permissions`: The permissions number for the bot invite (only with bot scope) (https://discord.com/developers/docs/topics/permissions). +`integrationType`: The installation context for the authorization, either 0 for guild or 1 for user install (only with applications.commands scope) (https://discord.com/developers/docs/resources/application#installation-context). + `guildId`: The guild id to pre-fill the bot invite (only with bot scope). `disableGuildSelect`: Disallows the user from changing the guild for the bot invite, either true or false (only with bot scope). From f0acc88771475a14c4184cd21d204270ca262611 Mon Sep 17 00:00:00 2001 From: MarkusRost <37275477+Markus-Rost@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:21:33 +0200 Subject: [PATCH 3/4] Add getCurrentAuthorizationInformation() https://discord.com/developers/docs/topics/oauth2#get-current-authorization-information --- index.d.ts | 24 +++++++++++++++++++++--- lib/oauth.js | 14 ++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) 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 From b4bf86289f231ef09a8080078483eb4d12f4aacd Mon Sep 17 00:00:00 2001 From: MarkusRost <37275477+Markus-Rost@users.noreply.github.com> Date: Fri, 19 Jul 2024 12:30:25 +0200 Subject: [PATCH 4/4] Add getCurrentAuthorizationInformation() to README --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) 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.