UNPKG

@sapphire/plugin-api

Version:

Plugin for @sapphire/framework to expose a REST API

132 lines (129 loc) 5.06 kB
'use strict'; var utilities = require('@sapphire/utilities'); var crypto = require('crypto'); var discord_js = require('discord.js'); var undici = require('undici'); var __defProp = Object.defineProperty; var __typeError = (msg) => { throw TypeError(msg); }; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value); var _secret; var _Auth = class _Auth { constructor(options) { /** * The client's application id, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications. * @since 1.0.0 */ __publicField(this, "id"); /** * The name for the cookie, this will be used to identify a Secure HttpOnly cookie. * @since 1.0.0 */ __publicField(this, "cookie"); /** * The scopes defined at https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes. * @since 1.0.0 */ __publicField(this, "scopes"); /** * The redirect uri. * @since 1.0.0 */ __publicField(this, "redirect"); /** * The transformers used for {@link Auth.fetchData}. * @since 1.4.0 */ __publicField(this, "transformers"); __publicField(this, "domainOverwrite", null); __privateAdd(this, _secret); this.id = options.id; this.cookie = options.cookie ?? "SAPPHIRE_AUTH"; this.scopes = options.scopes ?? [discord_js.OAuth2Scopes.Identify]; this.redirect = options.redirect; __privateSet(this, _secret, options.secret); this.transformers = options.transformers ?? []; this.domainOverwrite = options.domainOverwrite ?? null; } /** * The client secret, this can be retrieved in Discord Developer Portal at https://discord.com/developers/applications. * @since 1.0.0 */ get secret() { return __privateGet(this, _secret); } /** * Encrypts an object with aes-256-cbc to use as a token. * @since 1.0.0 * @param data An object to encrypt */ encrypt(data) { const iv = crypto.randomBytes(16); const cipher = crypto.createCipheriv("aes-256-cbc", __privateGet(this, _secret), iv); return `${cipher.update(JSON.stringify(data), "utf8", "base64") + cipher.final("base64")}.${iv.toString("base64")}`; } /** * Decrypts an object with aes-256-cbc to use as a token. * @since 1.0.0 * @param token An data to decrypt */ decrypt(token) { const [data, iv] = token.split("."); const decipher = crypto.createDecipheriv("aes-256-cbc", __privateGet(this, _secret), Buffer.from(iv, "base64")); try { const parsed = JSON.parse(decipher.update(data, "base64", "utf8") + decipher.final("utf8")); return parsed.expires >= Date.now() ? parsed : null; } catch { return null; } } /** * Retrieves the data for a specific user. * @since 1.4.0 * @param token The access token from the user. */ async fetchData(token) { const [user, guilds, connections] = await Promise.all([ this.fetchInformation(discord_js.OAuth2Scopes.Identify, token, `${discord_js.RouteBases.api}${discord_js.Routes.user()}`), this.fetchInformation(discord_js.OAuth2Scopes.Guilds, token, `${discord_js.RouteBases.api}${discord_js.Routes.userGuilds()}`), this.fetchInformation( discord_js.OAuth2Scopes.Connections, token, `${discord_js.RouteBases.api}${discord_js.Routes.userConnections()}` ) ]); let data = { user, guilds, connections }; for (const transformer of this.transformers) { const result = transformer(data); if (utilities.isThenable(result)) data = await result; else data = result; } return data; } async fetchInformation(scope, token, url) { if (!this.scopes.includes(scope)) return void 0; const result = await undici.fetch(url, { headers: { authorization: `Bearer ${token}` } }); return result.ok ? await result.json() : null; } static create(options) { if (!options?.secret || !options.id) return null; return new _Auth(options); } }; _secret = new WeakMap(); __name(_Auth, "Auth"); var Auth = _Auth; exports.Auth = Auth; //# sourceMappingURL=Auth.cjs.map //# sourceMappingURL=Auth.cjs.map