|
| 1 | +// Required because discord-rpc does not expose the access token |
| 2 | +// and does not even use the refresh token. |
| 3 | +// Without this, users would be prompted on whether they agree, whenever |
| 4 | +// the service connects to discord. |
| 5 | + |
| 6 | +import * as rpc from "discord-rpc"; |
| 7 | +import fetch from "node-fetch"; |
| 8 | +import { URLSearchParams } from "url"; |
| 9 | + |
| 10 | +// Defined here because nodejs does not seem to accept |
| 11 | +// circular imports. |
| 12 | +export interface DiscordRpcConfig { |
| 13 | + clientId: string; |
| 14 | + clientSecret: string; |
| 15 | + accessToken?: string; |
| 16 | + refreshToken?: string; |
| 17 | + redirectUrl?: string; |
| 18 | + expireTime?: number; |
| 19 | +} |
| 20 | + |
| 21 | +// Fill the config values and create matching login data |
| 22 | +export async function createLoginData( |
| 23 | + client: rpc.Client, |
| 24 | + config: DiscordRpcConfig, |
| 25 | + scopes: string[], |
| 26 | +): Promise<rpc.RPCLoginOptions> { |
| 27 | + await client.connect(config.clientId); |
| 28 | + const redirectUrl = config.redirectUrl === undefined ? "http://127.0.0.1" : config.redirectUrl; |
| 29 | + if (config.accessToken === undefined || config.refreshToken === undefined || config.expireTime === undefined) { |
| 30 | + // Call authorize |
| 31 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 32 | + // @ts-ignore |
| 33 | + const authorizeResult = await client.request("AUTHORIZE", { |
| 34 | + client_id: config.clientId, |
| 35 | + scopes: scopes, |
| 36 | + }); |
| 37 | + const response = await fetch("https://discord.com/api/oauth2/token", { |
| 38 | + method: "POST", |
| 39 | + headers: { |
| 40 | + "Content-Type": "application/x-www-form-urlencoded", |
| 41 | + }, |
| 42 | + body: encodeQuery({ |
| 43 | + client_id: config.clientId, |
| 44 | + client_secret: config.clientSecret, |
| 45 | + grant_type: "authorization_code", |
| 46 | + code: authorizeResult.code, |
| 47 | + redirect_uri: redirectUrl, |
| 48 | + scope: scopes.join(" "), |
| 49 | + }), |
| 50 | + }); |
| 51 | + const data = await response.json(); |
| 52 | + if ("error" in data) |
| 53 | + throw new Error("error_description" in data ? data.error_description : "Unknown discord rpc login error"); |
| 54 | + config.accessToken = data.access_token; |
| 55 | + config.refreshToken = data.refresh_token; |
| 56 | + config.expireTime = data.expires_in + Date.now() / 1000 - 1000; |
| 57 | + } else if (config.expireTime < Date.now() / 1000) { |
| 58 | + // Token expired. Request a new one using the refresh token. |
| 59 | + const response = await fetch("https://discord.com/api/oauth2/token", { |
| 60 | + method: "POST", |
| 61 | + headers: { |
| 62 | + "Content-Type": "application/x-www-form-urlencoded", |
| 63 | + }, |
| 64 | + body: encodeQuery({ |
| 65 | + client_id: config.clientId, |
| 66 | + client_secret: config.clientSecret, |
| 67 | + grant_type: "refresh_token", |
| 68 | + refresh_token: config.refreshToken, |
| 69 | + redirect_uri: redirectUrl, |
| 70 | + scope: scopes.join(" "), |
| 71 | + }), |
| 72 | + }); |
| 73 | + const data = await response.json(); |
| 74 | + if ("error" in data) |
| 75 | + throw new Error("error_description" in data ? data.error_description : "Unknown discord rpc refresh error"); |
| 76 | + config.accessToken = data.access_token; |
| 77 | + config.refreshToken = data.refresh_token; |
| 78 | + config.expireTime = data.expires_in + Date.now() / 1000 - 1000; |
| 79 | + } |
| 80 | + return { |
| 81 | + clientId: config.clientId, |
| 82 | + clientSecret: config.clientSecret, |
| 83 | + redirectUri: redirectUrl, |
| 84 | + scopes: scopes, |
| 85 | + accessToken: config.accessToken, |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +function encodeQuery(query: Record<string, unknown>): URLSearchParams { |
| 90 | + const params = new URLSearchParams(); |
| 91 | + for (const key in query) { |
| 92 | + params.append(key, String(query[key])); |
| 93 | + } |
| 94 | + return params; |
| 95 | +} |
0 commit comments