From fe4acbfee58e840412ed5d496fb565843d4a8cdd Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Sun, 6 Aug 2023 23:15:35 -0400 Subject: Fix linter warnings in connections --- src/connections/Twitter/index.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'src/connections/Twitter') diff --git a/src/connections/Twitter/index.ts b/src/connections/Twitter/index.ts index 4526fdf5..62fd7da1 100644 --- a/src/connections/Twitter/index.ts +++ b/src/connections/Twitter/index.ts @@ -55,17 +55,20 @@ export default class TwitterConnection extends RefreshableConnection { settings: TwitterSettings = new TwitterSettings(); init(): void { - this.settings = ConnectionLoader.getConnectionConfig( + const settings = ConnectionLoader.getConnectionConfig( this.id, this.settings, - ) as TwitterSettings; + ); + + 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(" ")); @@ -92,14 +95,16 @@ export default class TwitterConnection extends RefreshableConnection { 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(), code_verifier: "challenge", // TODO: properly use PKCE challenge }), @@ -126,14 +131,16 @@ export default class TwitterConnection extends RefreshableConnection { 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: "refresh_token", refresh_token, - client_id: this.settings.clientId!, + client_id: this.settings.clientId as string, redirect_uri: this.getRedirectUri(), code_verifier: "challenge", // TODO: properly use PKCE challenge }), @@ -163,8 +170,11 @@ export default class TwitterConnection extends RefreshableConnection { async handleCallback( params: ConnectionCallbackSchema, ): Promise { - 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 userInfo = await this.getUser(tokenData.access_token); const exists = await this.hasConnection(userId, userInfo.data.id); -- cgit 1.5.1