diff options
Diffstat (limited to 'src/connections/Xbox/index.ts')
-rw-r--r-- | src/connections/Xbox/index.ts | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/connections/Xbox/index.ts b/src/connections/Xbox/index.ts index 3df09e27..935ff7ab 100644 --- a/src/connections/Xbox/index.ts +++ b/src/connections/Xbox/index.ts @@ -19,10 +19,10 @@ import { ConnectedAccount, ConnectedAccountCommonOAuthTokenResponse, + Connection, ConnectionCallbackSchema, ConnectionLoader, DiscordApiErrors, - Connection, } from "@spacebar/util"; import wretch from "wretch"; import { XboxSettings } from "./XboxSettings"; @@ -62,17 +62,20 @@ export default class XboxConnection extends Connection { settings: XboxSettings = new XboxSettings(); init(): void { - this.settings = ConnectionLoader.getConnectionConfig( + const settings = ConnectionLoader.getConnectionConfig<XboxSettings>( this.id, this.settings, - ) as XboxSettings; + ); + + if (settings.enabled && (!settings.clientId || !settings.clientSecret)) + throw new Error(`Invalid settings for connection ${this.id}`); } getAuthorizationUrl(userId: string): string { const state = this.createState(userId); const url = new URL(this.authorizeUrl); - url.searchParams.append("client_id", this.settings.clientId!); + url.searchParams.append("client_id", this.settings.clientId as string); url.searchParams.append("redirect_uri", this.getRedirectUri()); url.searchParams.append("response_type", "code"); url.searchParams.append("scope", this.scopes.join(" ")); @@ -124,14 +127,16 @@ export default class XboxConnection extends Connection { Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded", Authorization: `Basic ${Buffer.from( - `${this.settings.clientId!}:${this.settings.clientSecret!}`, + `${this.settings.clientId as string}:${ + this.settings.clientSecret as string + }`, ).toString("base64")}`, }) .body( new URLSearchParams({ grant_type: "authorization_code", code: code, - client_id: this.settings.clientId!, + client_id: this.settings.clientId as string, redirect_uri: this.getRedirectUri(), scope: this.scopes.join(" "), }), @@ -174,8 +179,11 @@ export default class XboxConnection extends Connection { async handleCallback( params: ConnectionCallbackSchema, ): Promise<ConnectedAccount | null> { - const userId = this.getUserId(params.state); - const tokenData = await this.exchangeCode(params.state, params.code!); + const { state, code } = params; + if (!code) throw new Error("No code provided"); + + const userId = this.getUserId(state); + const tokenData = await this.exchangeCode(state, code); const userToken = await this.getUserToken(tokenData.access_token); const userInfo = await this.getUser(userToken); |