diff --git a/packages/backend/src/apps/firebase/authentication.ts b/packages/backend/src/apps/firebase/authentication.ts index 03f9ff82..31a872ea 100644 --- a/packages/backend/src/apps/firebase/authentication.ts +++ b/packages/backend/src/apps/firebase/authentication.ts @@ -1,10 +1,14 @@ +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; import { google as GoogleApi } from 'googleapis'; +import { OAuth2Client } from 'google-auth-library'; import Field from '../../types/field'; +import AppInfo from '../../types/app-info'; +import JSONObject from '../../types/interfaces/json-object'; -export default class Authentication { - appData: any; - connectionData: any; - client: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; + client: OAuth2Client; scopes: string[] = [ 'https://www.googleapis.com/auth/datastore', @@ -13,13 +17,13 @@ export default class Authentication { 'profile', ]; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.appData = appData; this.connectionData = connectionData; this.client = new GoogleApi.auth.OAuth2( - connectionData.consumerKey, - connectionData.consumerSecret, + connectionData.consumerKey as string, + connectionData.consumerSecret as string, this.oauthRedirectUrl ); @@ -43,7 +47,7 @@ export default class Authentication { async verifyCredentials() { const { tokens } = await this.client.getToken( - this.connectionData.oauthVerifier + this.connectionData.oauthVerifier as string ); this.client.setCredentials(tokens); @@ -74,7 +78,7 @@ export default class Authentication { async isStillVerified() { try { - await this.client.getTokenInfo(this.connectionData.accessToken); + await this.client.getTokenInfo(this.connectionData.accessToken as string); return true; } catch { return false; diff --git a/packages/backend/src/apps/flickr/authentication.ts b/packages/backend/src/apps/flickr/authentication.ts index 4466384f..890d5d31 100644 --- a/packages/backend/src/apps/flickr/authentication.ts +++ b/packages/backend/src/apps/flickr/authentication.ts @@ -1,13 +1,16 @@ +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; import FlickrApi from 'flickr-sdk'; +import AppInfo from '../../types/app-info'; import Field from '../../types/field'; +import JSONObject from '../../types/interfaces/json-object'; -export default class Authentication { - appData: any; - connectionData: any; - client: any; - oauthClient: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; + client: typeof FlickrApi; + oauthClient: typeof FlickrApi; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.oauthClient = new FlickrApi.OAuth( connectionData.consumerKey, connectionData.consumerSecret diff --git a/packages/backend/src/apps/flickr/index.d.ts b/packages/backend/src/apps/flickr/types/index.d.ts similarity index 100% rename from packages/backend/src/apps/flickr/index.d.ts rename to packages/backend/src/apps/flickr/types/index.d.ts diff --git a/packages/backend/src/apps/github/authentication.ts b/packages/backend/src/apps/github/authentication.ts index e51ebd1e..23a417b8 100644 --- a/packages/backend/src/apps/github/authentication.ts +++ b/packages/backend/src/apps/github/authentication.ts @@ -1,18 +1,32 @@ +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; import { getWebFlowAuthorizationUrl, exchangeWebFlowCode, checkToken, } from '@octokit/oauth-methods'; +import AppInfo from '../../types/app-info'; import Field from '../../types/field'; +import JSONObject from '../../types/interfaces/json-object'; -export default class Authentication { - appData: any; - connectionData: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; scopes: string[] = ['repo']; + client: { + getWebFlowAuthorizationUrl: typeof getWebFlowAuthorizationUrl; + exchangeWebFlowCode: typeof exchangeWebFlowCode; + checkToken: typeof checkToken; + }; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.connectionData = connectionData; this.appData = appData; + + this.client = { + getWebFlowAuthorizationUrl, + exchangeWebFlowCode, + checkToken, + }; } get oauthRedirectUrl(): string { @@ -21,10 +35,10 @@ export default class Authentication { ).value; } - async createAuthData(): { url: string } { - const { url } = await getWebFlowAuthorizationUrl({ + async createAuthData(): Promise<{ url: string }> { + const { url } = await this.client.getWebFlowAuthorizationUrl({ clientType: 'oauth-app', - clientId: this.connectionData.consumerKey, + clientId: this.connectionData.consumerKey as string, redirectUrl: this.oauthRedirectUrl, scopes: this.scopes, }); @@ -34,12 +48,12 @@ export default class Authentication { }; } - async verifyCredentials(): any { - const { data } = await exchangeWebFlowCode({ + async verifyCredentials() { + const { data } = await this.client.exchangeWebFlowCode({ clientType: 'oauth-app', - clientId: this.connectionData.consumerKey, - clientSecret: this.connectionData.consumerSecret, - code: this.connectionData.oauthVerifier, + clientId: this.connectionData.consumerKey as string, + clientSecret: this.connectionData.consumerSecret as string, + code: this.connectionData.oauthVerifier as string, }); this.connectionData.accessToken = data.access_token; @@ -58,15 +72,15 @@ export default class Authentication { } async getTokenInfo() { - return checkToken({ + return this.client.checkToken({ clientType: 'oauth-app', - clientId: this.connectionData.consumerKey, - clientSecret: this.connectionData.consumerSecret, - token: this.connectionData.accessToken, + clientId: this.connectionData.consumerKey as string, + clientSecret: this.connectionData.consumerSecret as string, + token: this.connectionData.accessToken as string, }); } - async isStillVerified(): boolean { + async isStillVerified() { try { await this.getTokenInfo(); diff --git a/packages/backend/src/apps/postgresql/authentication.ts b/packages/backend/src/apps/postgresql/authentication.ts index 677ce570..a45e3543 100644 --- a/packages/backend/src/apps/postgresql/authentication.ts +++ b/packages/backend/src/apps/postgresql/authentication.ts @@ -1,18 +1,21 @@ +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; import { Client } from 'pg'; +import AppInfo from '../../types/app-info'; +import JSONObject from '../../types/interfaces/json-object'; -export default class Authentication { - appData: any; - connectionData: any; - client: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; + client: Client; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.client = new Client({ - host: connectionData.host, - port: connectionData.port, - database: connectionData.database, - user: connectionData.username, - password: connectionData.password, - ssl: connectionData.ssl, + host: connectionData.host as string, + port: connectionData.port as number, + database: connectionData.database as string, + user: connectionData.username as string, + password: connectionData.password as string, + ssl: connectionData.ssl as boolean, }); this.connectionData = connectionData; diff --git a/packages/backend/src/apps/smtp/authentication.ts b/packages/backend/src/apps/smtp/authentication.ts index b217e54e..5ade97b6 100644 --- a/packages/backend/src/apps/smtp/authentication.ts +++ b/packages/backend/src/apps/smtp/authentication.ts @@ -1,11 +1,13 @@ -import nodemailer from 'nodemailer'; +import nodemailer, { Transporter, TransportOptions } from 'nodemailer'; +import AppInfo from '../../types/app-info'; +import JSONObject from '../../types/interfaces/json-object'; export default class Authentication { - appData: any; - connectionData: any; - client: any; + appData: AppInfo; + connectionData: JSONObject; + client: Transporter; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.client = nodemailer.createTransport({ host: connectionData.host, port: connectionData.port, @@ -14,7 +16,7 @@ export default class Authentication { user: connectionData.username, pass: connectionData.password, }, - }); + } as TransportOptions); this.connectionData = connectionData; this.appData = appData; diff --git a/packages/backend/src/apps/twilio/authentication.ts b/packages/backend/src/apps/twilio/authentication.ts index ba28fc16..bff16c3a 100644 --- a/packages/backend/src/apps/twilio/authentication.ts +++ b/packages/backend/src/apps/twilio/authentication.ts @@ -1,14 +1,17 @@ +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; import TwilioApi from 'twilio'; +import AppInfo from '../../types/app-info'; +import JSONObject from '../../types/interfaces/json-object'; -export default class Authentication { - appData: any; - connectionData: any; - client: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; + client: TwilioApi.Twilio; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.client = TwilioApi( - connectionData.accountSid, - connectionData.authToken + connectionData.accountSid as string, + connectionData.authToken as string ); this.connectionData = connectionData; diff --git a/packages/backend/src/apps/twitch/authentication.ts b/packages/backend/src/apps/twitch/authentication.ts index a7c451d9..862724b4 100644 --- a/packages/backend/src/apps/twitch/authentication.ts +++ b/packages/backend/src/apps/twitch/authentication.ts @@ -1,6 +1,9 @@ -import TwitchApi from 'twitch-js'; +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; +import TwitchApi, { TwitchJsOptions } from 'twitch-js'; import fetchUtil from 'twitch-js/lib/utils/fetch'; +import AppInfo from '../../types/app-info'; import Field from '../../types/field'; +import JSONObject from '../../types/interfaces/json-object'; type TwitchTokenResponse = { accessToken: string; @@ -9,17 +12,17 @@ type TwitchTokenResponse = { tokenType: string; }; -export default class Authentication { - appData: any; - connectionData: any; - client: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; + client: TwitchApi; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.connectionData = connectionData; this.appData = appData; if (this.clientOptions.token) { - this.client = new TwitchApi(this.clientOptions); + this.client = new TwitchApi(this.clientOptions as TwitchJsOptions); } } @@ -67,7 +70,7 @@ export default class Authentication { this.connectionData.accessToken = verifiedCredentials.accessToken; - const { api } = new TwitchApi(this.clientOptions); + const { api } = new TwitchApi(this.clientOptions as TwitchJsOptions); const { data } = await api.get('users'); const [user] = data; diff --git a/packages/backend/src/apps/twitter/authentication.ts b/packages/backend/src/apps/twitter/authentication.ts index d596ff8c..54bab8ce 100644 --- a/packages/backend/src/apps/twitter/authentication.ts +++ b/packages/backend/src/apps/twitter/authentication.ts @@ -1,21 +1,26 @@ -import TwitterApi from 'twitter-api-v2'; +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; +import TwitterApi, { TwitterApiTokens } from 'twitter-api-v2'; +import AppInfo from '../../types/app-info'; import Field from '../../types/field'; +import JSONObject from '../../types/interfaces/json-object'; -export default class Authentication { - appData: any; - connectionData: any; - client: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; + client: TwitterApi; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.appData = appData; this.connectionData = connectionData; - this.client = new TwitterApi({ + const clientParams = { appKey: connectionData.consumerKey, appSecret: connectionData.consumerSecret, accessToken: connectionData.accessToken, accessSecret: connectionData.accessSecret, - }); + } as TwitterApiTokens; + + this.client = new TwitterApi(clientParams); } async createAuthData() { @@ -35,7 +40,7 @@ export default class Authentication { async verifyCredentials() { const verifiedCredentials = await this.client.login( - this.connectionData.oauthVerifier + this.connectionData.oauthVerifier as string ); return { diff --git a/packages/backend/src/apps/typeform/authentication.ts b/packages/backend/src/apps/typeform/authentication.ts index de563254..f644a55d 100644 --- a/packages/backend/src/apps/typeform/authentication.ts +++ b/packages/backend/src/apps/typeform/authentication.ts @@ -1,10 +1,13 @@ +import AuthenticationInterface from '../../types/interfaces/authentication-interface'; import { URLSearchParams } from 'url'; import axios, { AxiosInstance } from 'axios'; +import AppInfo from '../../types/app-info'; import Field from '../../types/field'; +import JSONObject from '../../types/interfaces/json-object'; -export default class Authentication { - appData: any; - connectionData: any; +export default class Authentication implements AuthenticationInterface { + appData: AppInfo; + connectionData: JSONObject; client: AxiosInstance = axios.create({ baseURL: 'https://api.typeform.com', }); @@ -19,7 +22,7 @@ export default class Authentication { 'workspaces:read', ]; - constructor(appData: any, connectionData: any) { + constructor(appData: AppInfo, connectionData: JSONObject) { this.connectionData = connectionData; this.appData = appData; } @@ -32,7 +35,7 @@ export default class Authentication { async createAuthData() { const searchParams = new URLSearchParams({ - client_id: this.connectionData.consumerKey, + client_id: this.connectionData.consumerKey as string, redirect_uri: this.oauthRedirectUrl, scope: this.scope.join(' '), }); @@ -45,9 +48,9 @@ export default class Authentication { async verifyCredentials() { const params = new URLSearchParams({ grant_type: 'authorization_code', - code: this.connectionData.oauthVerifier, - client_id: this.connectionData.consumerKey, - client_secret: this.connectionData.consumerSecret, + code: this.connectionData.oauthVerifier as string, + client_id: this.connectionData.consumerKey as string, + client_secret: this.connectionData.consumerSecret as string, redirect_uri: this.oauthRedirectUrl, }); diff --git a/packages/backend/src/types/interfaces/authentication-interface.ts b/packages/backend/src/types/interfaces/authentication-interface.ts new file mode 100644 index 00000000..61c75f37 --- /dev/null +++ b/packages/backend/src/types/interfaces/authentication-interface.ts @@ -0,0 +1,10 @@ +import appInfoType from '../../types/app-info'; +import JSONObject from './json-object'; + +export default interface AuthenticationInterface { + appData: appInfoType; + connectionData: JSONObject; + client: unknown; + verifyCredentials(): Promise; + isStillVerified(): Promise; +} diff --git a/packages/backend/src/types/interfaces/index.d.ts b/packages/backend/src/types/interfaces/index.d.ts new file mode 100644 index 00000000..e13fa839 --- /dev/null +++ b/packages/backend/src/types/interfaces/index.d.ts @@ -0,0 +1 @@ +declare module 'interfaces'; diff --git a/packages/backend/src/types/interfaces/json-object.ts b/packages/backend/src/types/interfaces/json-object.ts new file mode 100644 index 00000000..a051165f --- /dev/null +++ b/packages/backend/src/types/interfaces/json-object.ts @@ -0,0 +1,9 @@ +type JSONValue = string | number | boolean | JSONObject | JSONArray; + +interface JSONObject { + [x: string]: JSONValue; +} + +type JSONArray = Array; + +export default JSONObject; diff --git a/packages/backend/tsconfig.json b/packages/backend/tsconfig.json index 0924afc8..158a30ba 100644 --- a/packages/backend/tsconfig.json +++ b/packages/backend/tsconfig.json @@ -9,18 +9,9 @@ "outDir": "dist", "baseUrl": ".", "paths": { - "*": [ - "node_modules/*", - "src/types/*" - ] + "*": ["node_modules/*", "src/types/*"] }, - "typeRoots": [ - "node_modules/@types", - "./src/types", - "./src/apps" - ] + "typeRoots": ["node_modules/@types", "./src/types", "./src/apps/*/types"] }, - "include": [ - "src/**/*" - ] + "include": ["src/**/*"] }