mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-02 00:40:07 +00:00
* add OAuth2 routes * Add oauth2 methods to rest * Add rest manager methods, Add token params * Add authorization headers * Add auth to editUserApplicationRoleConnection * fix logging header always displaying bot as auth * Add OAuth2Scope enum * Start testing ratelimit handling * Fix now scopes are separated by a space * move webhook object to DiscordAccessTokenResponse * convert payload to snake_case * fix some typings * more types fixes * add support for upserting commands with tokens * handle correctly ratelimit and concurrently * add guild to DiscordAccessTokenResponse * Add oauth2 create link function * Fix removeTokenPrefix to support Bearer tokens * update jsdoc comment for removeTokenPrefix * fix removeTokenPrefix unit tests * fix see link on getMember and getCurrentMember * add bot helpers and fix some types * Use objects to pass the bearer tokens * fix Deno issue with Buffer.from * Merge 'upstream/main' into feat/oauth2 to fix merge conflict * Fix debug queue logging * keep only 1 route for current user * add Bearer prefixed url to the rest of the logs --------- Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
76 lines
3.4 KiB
TypeScript
76 lines
3.4 KiB
TypeScript
import type { BigString, OAuth2Scope, PermissionStrings } from '@discordeno/types'
|
|
import { calculateBits } from './permissions.js'
|
|
|
|
export function createOAuth2Link(options: CreateOAuth2LinkOptions): string {
|
|
const joinedScopeString = options.scope.join(' ')
|
|
|
|
let url = `https://discord.com/oauth2/authorize?client_id=${options.clientId}&scope=${joinedScopeString}`
|
|
|
|
if (options.responseType) url += `&response_type=${options.responseType}`
|
|
if (options.state) url += `&state=${encodeURIComponent(options.state)}`
|
|
if (options.redirectUri) url += `&redirect_uri=${encodeURIComponent(options.redirectUri)}`
|
|
if (options.prompt) url += `&prompt=${options.prompt}`
|
|
if (options.permissions) url += `&permissions=${Array.isArray(options.permissions) ? calculateBits(options.permissions) : options.permissions}`
|
|
if (options.guildId) url += `&guild_id=${options.guildId}`
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
if (options.disableGuildSelect !== undefined) url += `&disable_guild_select=${options.disableGuildSelect}`
|
|
|
|
return url
|
|
}
|
|
|
|
export interface CreateOAuth2LinkOptions {
|
|
/**
|
|
* The type of response
|
|
*
|
|
* @remarks
|
|
* Should be defined only if using either OAuth2 authorization, implicit or not, or [advanced bot authorization](https://discord.com/developers/docs/topics/oauth2#advanced-bot-authorization)
|
|
*/
|
|
responseType?: 'code' | 'token'
|
|
/** The id of the application */
|
|
clientId: BigString
|
|
/** The scopes for the application */
|
|
scope: OAuth2Scope[]
|
|
/**
|
|
* The optional state for security
|
|
*
|
|
* @see https://discord.com/developers/docs/topics/oauth2#state-and-security
|
|
*/
|
|
state?: string
|
|
/**
|
|
* The redirect uri for after the authentication
|
|
*
|
|
* @remarks
|
|
* Should be defined only if using either OAuth2 authorization, implicit or not, or [advanced bot authorization](https://discord.com/developers/docs/topics/oauth2#advanced-bot-authorization)
|
|
*/
|
|
redirectUri?: string
|
|
/**
|
|
* The type of prompt to give to the user
|
|
*
|
|
* @remarks
|
|
* If set to `none`, it will skip the authorization screen and redirect them back to your redirect URI without requesting their authorization.
|
|
* For passthrough scopes, like bot and webhook.incoming, authorization is always required.
|
|
*/
|
|
prompt?: 'consent' | 'none'
|
|
/**
|
|
* The permissions of the invited bot
|
|
*
|
|
* @remarks
|
|
* Should be defined only in a [bot authorization flow](https://discord.com/developers/docs/topics/oauth2#bot-authorization-flow) or with [advanced bot authorization](https://discord.com/developers/docs/topics/oauth2#advanced-bot-authorization)
|
|
*/
|
|
permissions?: BigString | PermissionStrings[]
|
|
/**
|
|
* Pre-fills the dropdown picker with a guild for the user
|
|
*
|
|
* @remarks
|
|
* Should be defined only in a [bot authorization flow](https://discord.com/developers/docs/topics/oauth2#bot-authorization-flow) or with [advanced bot authorization](https://discord.com/developers/docs/topics/oauth2#advanced-bot-authorization) or with the `webhook.incoming` scope
|
|
*/
|
|
guildId?: BigString
|
|
/**
|
|
* Disallows the user from changing the guild dropdown if set to true
|
|
*
|
|
* @remarks
|
|
* Should be defined only in a [bot authorization flow](https://discord.com/developers/docs/topics/oauth2#bot-authorization-flow), with [advanced bot authorization](https://discord.com/developers/docs/topics/oauth2#advanced-bot-authorization) or with the `webhook.incoming` scope
|
|
*/
|
|
disableGuildSelect?: boolean
|
|
}
|