// Metro module 13571 (f13572). import _slicedToArray from "../../../../_bundle/_slicedToArray"; import ApplicationStore from "../../../applications/ApplicationStore"; import ApplicationRecord from "../../../../records/ApplicationRecord"; import UserStore from "../../../../stores/UserStore"; import { RPC_AUTHENTICATED_SCOPE, TransportTypes, RPC_PRIVATE_SCOPE } from "../../Constants"; import { ApplicationFlags, RPCErrors, Endpoints, RPCCommands } from "../../../../Constants"; import LeakyBucket from "../../../../_bundle/LeakyBucket"; import Durations from "../../../../utils/Durations"; import { Storage } from "../../../../_bundle/Module595"; import RPCError from "../../RPCError"; import { hasApplicationFlag } from "../../../applications/utils/ApplicationFlagUtils"; import { ApplicationIntegrationType } from "../../../../_bundle/Module8028"; import { fetchApplication } from "../../../premium_apps/ApplicationSubscriptionsHttpApi"; import * as _actions from "../../../oauth2/actions"; import { fetchAuthorization } from "../../../oauth2/actions"; import { getDisclosures } from "../../../applications/disclosures"; import { OAuth2Prompts } from "../../../oauth2/types"; import { NONE } from "../../../../utils/PermissionUtils"; import { deserialize } from "../../../../_bundle/HighLow"; import { HTTP } from "../../../../_bundle/sendRequest"; import Dispatcher from "../../../../Dispatcher"; import { createRPCCommand } from "../../../../_bundle/Module13535"; import { OAuth2Scopes } from "../../../../_bundle/Module8174"; import Module1464 from "../../../../_bundle/Module1464"; function $mk116550($env) { return function isUserInstallable(a0) { let r12 = hasApplicationFlag(a0, ApplicationFlags.EMBEDDED); if (r12) { const r2 = a0 == null; let r13 = void 0; if (!r2) { const integrationTypesConfig = a0.integrationTypesConfig; r13 = integrationTypesConfig == null ? void 0 : integrationTypesConfig[ApplicationIntegrationType.USER_INSTALL]; } r12 = r13 != null; } return r12; }; } function $mk116551($env) { return (a0) => { if (a0 == null) { throw new RPCError({ errorCode: RPCErrors.UNKNOWN_ERROR }, "Unknown error occurred"); } const r18 = a0.split(/#|\?/); const r26 = Module1464.parse(r18[r18.length - 1]); if (r26.error != null) { const r58 = { errorCode: RPCErrors.OAUTH2_ERROR }; const error = r26.error; let error_description = r26.error_description; if (error_description == null) { error_description = "unknown error"; } throw new RPCError(r58, `OAuth2 Error: ${error}: ${error_description}`); } const r28 = $env; const r10 = r28[2]; const { access_token, scope, expires_in } = r26; let r34 = Storage.get(c9428_16); if (r34 == null) { r34 = {}; } r34[r10] = { accessToken: access_token, scope, expires: Date.now() + expires_in }; Storage.set(c9428_16, r34); return (0, authenticate)(r28[0], r26.access_token); }; } let authenticate, c9428_16, c9428_17; let env9428 = {}; const authorizeWithPrompt = async function authorizeWithPrompt(c27516_0, c27516_1, c27516_2) { let r300, r502, location2; let c27516_3, createFromServer, c27516_5, c27516_6, c27516_7, c27516_8, c27516_10; let c27516_9 = 0; const r23 = env9428; L57: { L45: { try { let env30649; c27516_8 = r23; const r58 = env30649 = { $parent: r23 }; c27516_7 = r58; env30649[0] = void 0; env30649[1] = void 0; env30649[2] = void 0; env30649[3] = void 0; env30649[4] = void 0; env30649[5] = void 0; env30649[6] = void 0; env30649[7] = void 0; env30649[8] = void 0; env30649[9] = void 0; env30649[10] = void 0; env30649[11] = void 0; env30649[12] = void 0; env30649[13] = void 0; env30649[14] = void 0; env30649[15] = void 0; env30649[16] = void 0; env30649[17] = void 0; env30649[18] = void 0; env30649[0] = c27516_0.client_id; let response_type = c27516_0.response_type; if (response_type === void 0) { response_type = "code"; } env30649[1] = response_type; env30649[2] = c27516_0.redirect_uri; env30649[3] = c27516_0.code_challenge; env30649[4] = c27516_0.code_challenge_method; env30649[5] = c27516_0.state; env30649[6] = c27516_0.nonce; env30649[7] = c27516_0.scope; env30649[8] = c27516_0.permissions; env30649[9] = c27516_0.guild_id; env30649[10] = c27516_0.channel_id; env30649[11] = c27516_0.prompt; env30649[12] = c27516_0.disable_guild_select; env30649[13] = c27516_0.integration_type; env30649[14] = c27516_0.pid; env30649[15] = c27516_0.signal; env30649[16] = c27516_0.isSocketRpcPrivateScope; env30649[17] = c27516_1; env30649[18] = c27516_2; env30649[19] = void 0; env30649[20] = void 0; env30649[21] = void 0; env30649[22] = void 0; env30649[23] = void 0; env30649[24] = void 0; env30649[25] = void 0; env30649[26] = void 0; env30649[27] = void 0; env30649[28] = void 0; env30649[29] = void 0; env30649[30] = void 0; env30649[31] = void 0; await void 0; const r100 = "Redirect URI cannot be used in the RPC OAuth2 Authorization flow"; if (env30649[15]?.aborted) { throw new RPCError({ errorCode: RPCErrors.UNKNOWN_ERROR }, "Request aborted"); } if (env30649[0] == null) { throw new RPCError({ errorCode: RPCErrors.OAUTH2_ERROR }, "No Client ID provided"); } if (!env30649[16] && env30649[2] != null) { throw new RPCError({ errorCode: RPCErrors.OAUTH2_ERROR }, r100); } env30649[23] = []; if (typeof env30649[7] === "string") { env30649[23] = env30649[7].split(" ").filter((item) => item.length > 0); } else if (Array.isArray(env30649[7])) { env30649[23] = env30649[7]; } if (UserStore.getCurrentUser() == null) { throw new RPCError({ errorCode: RPCErrors.OAUTH2_ERROR }, "Client is not logged in"); } L36: { if (env30649[13] != null) { env30649[19] = Number(env30649[13]); } else { env30649[32] = $mk116550(env9428); env30649[24] = ApplicationStore.getApplication(env30649[0]); if (!(0, env30649[32])(env30649[24])) { c27516_6 = env30649[32]; c27516_5 = ApplicationRecord; createFromServer = ApplicationRecord.createFromServer; const application = await fetchApplication(env30649[0], env30649[15]); const r256 = c27516_5; const r257 = c27516_6; const r259 = createFromServer.call(r256, application); env30649[24] = r259; env30649[19] = r257(r259) ? ApplicationIntegrationType.USER_INSTALL : ApplicationIntegrationType.GUILD_INSTALL; c27516_9 = 1; r300 = Promise.all([fetchAuthorization({ clientId: env30649[0], scopes: env30649[23], responseType: env30649[1], redirectUri: env30649[2], codeChallenge: env30649[3], codeChallengeMethod: env30649[4], state: env30649[5], integrationType: env30649[19], signal: env30649[15] }), getDisclosures(env30649[0])]); break L36; } env30649[19] = ApplicationIntegrationType.USER_INSTALL; } c27516_9 = 1; r300 = Promise.all([fetchAuthorization({ clientId: env30649[0], scopes: env30649[23], responseType: env30649[1], redirectUri: env30649[2], codeChallenge: env30649[3], codeChallengeMethod: env30649[4], state: env30649[5], integrationType: env30649[19], signal: env30649[15] }), getDisclosures(env30649[0])]); } env30649[25] = await r300; env30649[26] = (0, _slicedToArray)(env30649[25], 2); env30649[20] = env30649[26][0]; env30649[27] = env30649[26][1]; env30649[21] = env30649[27].disclosures; env30649[22] = env30649[27].allAcked; c27516_9 = 0; if (env30649[11] === OAuth2Prompts.NONE && env30649[20] != null && env30649[20].authorized) { if (env30649[22]) { c27516_9 = 2; location2 = (await _actions.authorize({ authorize: true, clientId: env30649[0], scopes: env30649[23], responseType: env30649[1], redirectUri: env30649[2], codeChallenge: env30649[3], codeChallengeMethod: env30649[4], state: env30649[5], nonce: env30649[6], integrationType: env30649[19] })).location; c27516_9 = 0; break L45; } } const r355 = env30649[18]; if (r355 != null) { r355(env30649[20].application, env30649[10], env30649[14]); } env30649[30] = NONE; c27516_9 = 3; const r427 = env30649[8]; c27516_3 = r427; if (r427 == null) { c27516_3 = 0; } env30649[30] = deserialize(c27516_3); c27516_9 = 0; env30649[31] = void 0; if (env30649[20].integration_type != null && Object.values(ApplicationIntegrationType).includes(env30649[20].integration_type)) { const map = new Map(); env30649[31] = map; map.set(env30649[20].integration_type, env30649[20]); } if (c9428_17[env30649[20].application.id] != null) { await c9428_17[env30649[20].application.id].process(); if (!env30649[15]?.aborted) { r502 = (0, env30649[17])({ clientId: env30649[0], authorizations: env30649[31], scopes: env30649[23], parsedPermissions: env30649[30], responseType: env30649[1], redirectUri: env30649[2], codeChallenge: env30649[3], codeChallengeMethod: env30649[4], state: env30649[5], guildId: env30649[9], channelId: env30649[10], prompt: env30649[11], disableGuildSelect: env30649[12], disclosures: env30649[21], integrationType: env30649[19], pid: env30649[14], signal: env30649[15] }); break L57; } throw new RPCError({ errorCode: RPCErrors.UNKNOWN_ERROR }, "Request aborted"); } r502 = (0, env30649[17])({ clientId: env30649[0], authorizations: env30649[31], scopes: env30649[23], parsedPermissions: env30649[30], responseType: env30649[1], redirectUri: env30649[2], codeChallenge: env30649[3], codeChallengeMethod: env30649[4], state: env30649[5], guildId: env30649[9], channelId: env30649[10], prompt: env30649[11], disableGuildSelect: env30649[12], disclosures: env30649[21], integrationType: env30649[19], pid: env30649[14], signal: env30649[15] }); break L57; } catch ($e) { const r56 = $e; let r8 = r56; c27516_10 = r56; let r57 = c27516_9; if (r57 !== 0) { while (true) { if (r57 === 1) { try { const r774 = c27516_10; c27516_9 = 0; env30649[28] = r774.body; throw new RPCError({ errorCode: RPCErrors.OAUTH2_ERROR }, `OAuth2 Authorization Error: ${env30649[28]?.message || "Unknown Error"}`); } catch (r771) { r8 = r771; c27516_10 = r771; r57 = c27516_9; if (r57 === 0) { break; } } } else if (r57 === 2) { try { const r748 = c27516_10; c27516_9 = 0; env30649[29] = r748.body; throw new RPCError({ errorCode: RPCErrors.OAUTH2_ERROR }, `OAuth2 Authorize Error: ${env30649[29]?.message || "Unknown Error"}`); } catch (r745) { r8 = r745; c27516_10 = r745; r57 = c27516_9; if (r57 === 0) { break; } } } else { try { c27516_9 = 0; env30649[31] = void 0; if (env30649[20].integration_type != null && Object.values(ApplicationIntegrationType).includes(env30649[20].integration_type)) { const map2 = new Map(); env30649[31] = map2; map2.set(env30649[20].integration_type, env30649[20]); } if (c9428_17[env30649[20].application.id] != null) { await c9428_17[env30649[20].application.id].process(); if (!env30649[15]?.aborted) { r502 = (0, env30649[17])({ clientId: env30649[0], authorizations: env30649[31], scopes: env30649[23], parsedPermissions: env30649[30], responseType: env30649[1], redirectUri: env30649[2], codeChallenge: env30649[3], codeChallengeMethod: env30649[4], state: env30649[5], guildId: env30649[9], channelId: env30649[10], prompt: env30649[11], disableGuildSelect: env30649[12], disclosures: env30649[21], integrationType: env30649[19], pid: env30649[14], signal: env30649[15] }); break L57; } throw new RPCError({ errorCode: RPCErrors.UNKNOWN_ERROR }, "Request aborted"); } r502 = (0, env30649[17])({ clientId: env30649[0], authorizations: env30649[31], scopes: env30649[23], parsedPermissions: env30649[30], responseType: env30649[1], redirectUri: env30649[2], codeChallenge: env30649[3], codeChallengeMethod: env30649[4], state: env30649[5], guildId: env30649[9], channelId: env30649[10], prompt: env30649[11], disableGuildSelect: env30649[12], disclosures: env30649[21], integrationType: env30649[19], pid: env30649[14], signal: env30649[15] }); break L57; } catch (r606) { r8 = r606; c27516_10 = r606; r57 = c27516_9; if (r57 === 0) { break; } } } } } throw r8; } } return location2; } return r502; }; authenticate = function authenticate(a0, a1) { const accessToken = a1; if (a0.authorization.accessToken) { throw new RPCError({ errorCode: RPCErrors.INVALID_COMMAND }, "Already authenticated"); } if (a0.authorization.authing) { throw new RPCError({ errorCode: RPCErrors.INVALID_COMMAND }, "Already authenticating"); } a0.authorization.authing = true; return HTTP.get({ url: Endpoints.OAUTH2_CURRENT_AUTH, headers: { Authorization: `Bearer ${a1}` }, oldFormErrors: true, rejectWithError: false }).then((result) => { a0.authorization.authing = false; const body = result.body; const { application, user, scopes, expires } = body; if (a0.application.id !== application.id) { throw new RPCError({ errorCode: RPCErrors.INVALID_CLIENTID }, "Application does not match the connection's"); } const currentUser = UserStore.getCurrentUser(); if (currentUser != null && user && currentUser.id === user.id) { a0.authorization.scopes = [...a0.authorization.scopes, ...scopes, RPC_AUTHENTICATED_SCOPE]; a0.authorization.accessToken = accessToken; a0.authorization.expires = new Date(expires); Dispatcher.dispatch({ type: "RPC_APP_AUTHENTICATED", socketId: a0.id, application: a0.application }); return { ...result.body, access_token: accessToken }; } throw new RPCError({ errorCode: RPCErrors.INVALID_TOKEN }, "Token does not match current user"); }, () => { throw new RPCError({ errorCode: RPCErrors.INVALID_TOKEN }, `Invalid access token: ${accessToken}`); }).catch((error) => { a0.authorization.authing = false; throw error; }); }; c9428_16 = "CachedTokens"; c9428_17 = { "1273616940451102832": new LeakyBucket(2, 1 * Durations.Millis.MINUTE) }; export default function createAuthCommandHandlers(a0, a1) { let env20539 = { $parent: env9428 }; env20539[0] = a0; env20539[1] = a1; const r11 = {}; r11[RPCCommands.AUTHENTICATE] = createRPCCommand(RPCCommands.AUTHENTICATE, { handler: (a0) => { let r51, IDENTIFY; let env27517; const r6 = env27517 = { $parent: env20539 }; const socket2 = a0.socket; env27517[0] = socket2; const signal = a0.signal; env27517[1] = signal; const access_token = a0.args.access_token; env27517[2] = void 0; env27517[3] = void 0; env27517[4] = void 0; if (access_token == null && socket2.transport === TransportTypes.IPC) { const id = socket2.application.id; env27517[2] = id; if (id == null) { throw new RPCError({ errorCode: RPCErrors.INVALID_COMMAND }, "No application."); } L12: { IDENTIFY = OAuth2Scopes.IDENTIFY; env27517[3] = IDENTIFY; env27517[4] = () => {}; const r50 = Storage.get(c9428_16); const r2 = r50 != null; r51 = void 0; if (r2) { const r53 = r50[id] != null; r51 = void 0; if (r53) { const r54 = r50[id]; if (r54.scope === IDENTIFY && !(r54.expires <= Date.now())) { r51 = r54.accessToken; break L12; } delete r50[id]; Storage.set(c9428_16, r50); r51 = void 0; } } } return r51 != null ? authenticate(socket2, r51).catch(() => { const r2 = env27517; const client_id = env27517[2]; let r15 = Storage.get(c9428_16); if (r15 == null) { r15 = {}; } delete r15[client_id]; Storage.set(c9428_16, r15); return authorizeWithPrompt({ client_id, scope: env27517[3], response_type: "token", signal: env27517[1], isSocketRpcPrivateScope: false }, env20539[0], env20539[1]).then($mk116551(r2)); }) : authorizeWithPrompt({ client_id: id, scope: IDENTIFY, response_type: "token", signal, isSocketRpcPrivateScope: false }, env20539[0], env20539[1]).then($mk116551(r6)); } if (access_token == null) { throw new RPCError({ errorCode: RPCErrors.INVALID_TOKEN }, "No access token provided"); } return authenticate(socket2, access_token); } }); r11[RPCCommands.AUTHORIZE] = { handler: ({ socket, signal, args }) => { let c27518_1; c27518_1 = void 0; const client_id = args.client_id; if (client_id) { if (socket.authorization.accessToken != null) { throw new RPCError({ errorCode: RPCErrors.INVALID_COMMAND }, "Already authenticated"); } if (socket.authorization.authing) { throw new RPCError({ errorCode: RPCErrors.INVALID_COMMAND }, "Already authing"); } socket.authorization.authing = true; if (args.response_type === "token") { socket.authorization.authing = false; throw new RPCError({ errorCode: RPCErrors.INVALID_COMMAND }, "Authorization response_type \"token\" is not supported"); } const isSocketRpcPrivateScope = socket.authorization.scopes.includes(RPC_PRIVATE_SCOPE); c27518_1 = isSocketRpcPrivateScope; if (!isSocketRpcPrivateScope && socket.application.id !== client_id) { socket.authorization.authing = false; throw new RPCError({ errorCode: RPCErrors.INVALID_CLIENTID }, "Application does not match the connection's"); } const scope = args.scopes || args.scope; delete args["scopes"]; return authorizeWithPrompt({ ...args, scope, signal, isSocketRpcPrivateScope }, env20539[0], env20539[1]).then((result) => { socket.authorization.authing = false; if (result == null) { throw new RPCError({ errorCode: RPCErrors.UNKNOWN_ERROR }, "Unknown error occurred"); } const uRL = new URL(result); const code = uRL.searchParams.get("code"); if (c27518_1) { return { code, location: result }; } const r10 = uRL.searchParams.get("error"); if (r10 != null && r10 !== "") { let r43 = uRL.searchParams.get("error_description"); if (r43 == null) { r43 = "unknown error"; } throw new RPCError({ errorCode: RPCErrors.OAUTH2_ERROR }, `OAuth2 Error: ${r10}: ${r43}`); } if (code == null) { throw new RPCError({ errorCode: RPCErrors.OAUTH2_ERROR }, "OAuth2 Error: Unable to find auth code"); } return { code }; }).catch((error) => { socket.authorization.authing = false; throw error; }); } throw new RPCError({ errorCode: RPCErrors.INVALID_CLIENTID }, "No client id provided"); } }; return r11; }; export { c9428_17 as AUTHORIZE_PROMPT_THROTTLERS };