chore: Add authentication interface and adjust authentication classes
This commit is contained in:
@@ -1,10 +1,14 @@
|
|||||||
|
import AuthenticationInterface from '../../types/interfaces/authentication-interface';
|
||||||
import { google as GoogleApi } from 'googleapis';
|
import { google as GoogleApi } from 'googleapis';
|
||||||
|
import { OAuth2Client } from 'google-auth-library';
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
import AppInfo from '../../types/app-info';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: any;
|
client: OAuth2Client;
|
||||||
|
|
||||||
scopes: string[] = [
|
scopes: string[] = [
|
||||||
'https://www.googleapis.com/auth/datastore',
|
'https://www.googleapis.com/auth/datastore',
|
||||||
@@ -13,13 +17,13 @@ export default class Authentication {
|
|||||||
'profile',
|
'profile',
|
||||||
];
|
];
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.appData = appData;
|
this.appData = appData;
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
|
|
||||||
this.client = new GoogleApi.auth.OAuth2(
|
this.client = new GoogleApi.auth.OAuth2(
|
||||||
connectionData.consumerKey,
|
connectionData.consumerKey as string,
|
||||||
connectionData.consumerSecret,
|
connectionData.consumerSecret as string,
|
||||||
this.oauthRedirectUrl
|
this.oauthRedirectUrl
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -43,7 +47,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const { tokens } = await this.client.getToken(
|
const { tokens } = await this.client.getToken(
|
||||||
this.connectionData.oauthVerifier
|
this.connectionData.oauthVerifier as string
|
||||||
);
|
);
|
||||||
this.client.setCredentials(tokens);
|
this.client.setCredentials(tokens);
|
||||||
|
|
||||||
@@ -74,7 +78,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
async isStillVerified() {
|
async isStillVerified() {
|
||||||
try {
|
try {
|
||||||
await this.client.getTokenInfo(this.connectionData.accessToken);
|
await this.client.getTokenInfo(this.connectionData.accessToken as string);
|
||||||
return true;
|
return true;
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
|
@@ -1,13 +1,16 @@
|
|||||||
|
import AuthenticationInterface from '../../types/interfaces/authentication-interface';
|
||||||
import FlickrApi from 'flickr-sdk';
|
import FlickrApi from 'flickr-sdk';
|
||||||
|
import AppInfo from '../../types/app-info';
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: any;
|
client: typeof FlickrApi;
|
||||||
oauthClient: any;
|
oauthClient: typeof FlickrApi;
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.oauthClient = new FlickrApi.OAuth(
|
this.oauthClient = new FlickrApi.OAuth(
|
||||||
connectionData.consumerKey,
|
connectionData.consumerKey,
|
||||||
connectionData.consumerSecret
|
connectionData.consumerSecret
|
||||||
|
@@ -1,18 +1,32 @@
|
|||||||
|
import AuthenticationInterface from '../../types/interfaces/authentication-interface';
|
||||||
import {
|
import {
|
||||||
getWebFlowAuthorizationUrl,
|
getWebFlowAuthorizationUrl,
|
||||||
exchangeWebFlowCode,
|
exchangeWebFlowCode,
|
||||||
checkToken,
|
checkToken,
|
||||||
} from '@octokit/oauth-methods';
|
} from '@octokit/oauth-methods';
|
||||||
|
import AppInfo from '../../types/app-info';
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
scopes: string[] = ['repo'];
|
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.connectionData = connectionData;
|
||||||
this.appData = appData;
|
this.appData = appData;
|
||||||
|
|
||||||
|
this.client = {
|
||||||
|
getWebFlowAuthorizationUrl,
|
||||||
|
exchangeWebFlowCode,
|
||||||
|
checkToken,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
get oauthRedirectUrl(): string {
|
get oauthRedirectUrl(): string {
|
||||||
@@ -21,10 +35,10 @@ export default class Authentication {
|
|||||||
).value;
|
).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAuthData(): { url: string } {
|
async createAuthData(): Promise<{ url: string }> {
|
||||||
const { url } = await getWebFlowAuthorizationUrl({
|
const { url } = await this.client.getWebFlowAuthorizationUrl({
|
||||||
clientType: 'oauth-app',
|
clientType: 'oauth-app',
|
||||||
clientId: this.connectionData.consumerKey,
|
clientId: this.connectionData.consumerKey as string,
|
||||||
redirectUrl: this.oauthRedirectUrl,
|
redirectUrl: this.oauthRedirectUrl,
|
||||||
scopes: this.scopes,
|
scopes: this.scopes,
|
||||||
});
|
});
|
||||||
@@ -34,12 +48,12 @@ export default class Authentication {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyCredentials(): any {
|
async verifyCredentials() {
|
||||||
const { data } = await exchangeWebFlowCode({
|
const { data } = await this.client.exchangeWebFlowCode({
|
||||||
clientType: 'oauth-app',
|
clientType: 'oauth-app',
|
||||||
clientId: this.connectionData.consumerKey,
|
clientId: this.connectionData.consumerKey as string,
|
||||||
clientSecret: this.connectionData.consumerSecret,
|
clientSecret: this.connectionData.consumerSecret as string,
|
||||||
code: this.connectionData.oauthVerifier,
|
code: this.connectionData.oauthVerifier as string,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.connectionData.accessToken = data.access_token;
|
this.connectionData.accessToken = data.access_token;
|
||||||
@@ -58,15 +72,15 @@ export default class Authentication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getTokenInfo() {
|
async getTokenInfo() {
|
||||||
return checkToken({
|
return this.client.checkToken({
|
||||||
clientType: 'oauth-app',
|
clientType: 'oauth-app',
|
||||||
clientId: this.connectionData.consumerKey,
|
clientId: this.connectionData.consumerKey as string,
|
||||||
clientSecret: this.connectionData.consumerSecret,
|
clientSecret: this.connectionData.consumerSecret as string,
|
||||||
token: this.connectionData.accessToken,
|
token: this.connectionData.accessToken as string,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async isStillVerified(): boolean {
|
async isStillVerified() {
|
||||||
try {
|
try {
|
||||||
await this.getTokenInfo();
|
await this.getTokenInfo();
|
||||||
|
|
||||||
|
@@ -1,18 +1,21 @@
|
|||||||
|
import AuthenticationInterface from '../../types/interfaces/authentication-interface';
|
||||||
import { Client } from 'pg';
|
import { Client } from 'pg';
|
||||||
|
import AppInfo from '../../types/app-info';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: any;
|
client: Client;
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.client = new Client({
|
this.client = new Client({
|
||||||
host: connectionData.host,
|
host: connectionData.host as string,
|
||||||
port: connectionData.port,
|
port: connectionData.port as number,
|
||||||
database: connectionData.database,
|
database: connectionData.database as string,
|
||||||
user: connectionData.username,
|
user: connectionData.username as string,
|
||||||
password: connectionData.password,
|
password: connectionData.password as string,
|
||||||
ssl: connectionData.ssl,
|
ssl: connectionData.ssl as boolean,
|
||||||
});
|
});
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
|
@@ -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 {
|
export default class Authentication {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: any;
|
client: Transporter;
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.client = nodemailer.createTransport({
|
this.client = nodemailer.createTransport({
|
||||||
host: connectionData.host,
|
host: connectionData.host,
|
||||||
port: connectionData.port,
|
port: connectionData.port,
|
||||||
@@ -14,7 +16,7 @@ export default class Authentication {
|
|||||||
user: connectionData.username,
|
user: connectionData.username,
|
||||||
pass: connectionData.password,
|
pass: connectionData.password,
|
||||||
},
|
},
|
||||||
});
|
} as TransportOptions);
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = appData;
|
this.appData = appData;
|
||||||
|
@@ -1,14 +1,17 @@
|
|||||||
|
import AuthenticationInterface from '../../types/interfaces/authentication-interface';
|
||||||
import TwilioApi from 'twilio';
|
import TwilioApi from 'twilio';
|
||||||
|
import AppInfo from '../../types/app-info';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: any;
|
client: TwilioApi.Twilio;
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.client = TwilioApi(
|
this.client = TwilioApi(
|
||||||
connectionData.accountSid,
|
connectionData.accountSid as string,
|
||||||
connectionData.authToken
|
connectionData.authToken as string
|
||||||
);
|
);
|
||||||
|
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
|
@@ -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 fetchUtil from 'twitch-js/lib/utils/fetch';
|
||||||
|
import AppInfo from '../../types/app-info';
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
type TwitchTokenResponse = {
|
type TwitchTokenResponse = {
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
@@ -9,17 +12,17 @@ type TwitchTokenResponse = {
|
|||||||
tokenType: string;
|
tokenType: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: any;
|
client: TwitchApi;
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = appData;
|
this.appData = appData;
|
||||||
|
|
||||||
if (this.clientOptions.token) {
|
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;
|
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 { data } = await api.get('users');
|
||||||
const [user] = data;
|
const [user] = data;
|
||||||
|
@@ -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 Field from '../../types/field';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: any;
|
client: TwitterApi;
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.appData = appData;
|
this.appData = appData;
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
|
|
||||||
this.client = new TwitterApi({
|
const clientParams = {
|
||||||
appKey: connectionData.consumerKey,
|
appKey: connectionData.consumerKey,
|
||||||
appSecret: connectionData.consumerSecret,
|
appSecret: connectionData.consumerSecret,
|
||||||
accessToken: connectionData.accessToken,
|
accessToken: connectionData.accessToken,
|
||||||
accessSecret: connectionData.accessSecret,
|
accessSecret: connectionData.accessSecret,
|
||||||
});
|
} as TwitterApiTokens;
|
||||||
|
|
||||||
|
this.client = new TwitterApi(clientParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
async createAuthData() {
|
async createAuthData() {
|
||||||
@@ -35,7 +40,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const verifiedCredentials = await this.client.login(
|
const verifiedCredentials = await this.client.login(
|
||||||
this.connectionData.oauthVerifier
|
this.connectionData.oauthVerifier as string
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@@ -1,10 +1,13 @@
|
|||||||
|
import AuthenticationInterface from '../../types/interfaces/authentication-interface';
|
||||||
import { URLSearchParams } from 'url';
|
import { URLSearchParams } from 'url';
|
||||||
import axios, { AxiosInstance } from 'axios';
|
import axios, { AxiosInstance } from 'axios';
|
||||||
|
import AppInfo from '../../types/app-info';
|
||||||
import Field from '../../types/field';
|
import Field from '../../types/field';
|
||||||
|
import JSONObject from '../../types/interfaces/json-object';
|
||||||
|
|
||||||
export default class Authentication {
|
export default class Authentication implements AuthenticationInterface {
|
||||||
appData: any;
|
appData: AppInfo;
|
||||||
connectionData: any;
|
connectionData: JSONObject;
|
||||||
client: AxiosInstance = axios.create({
|
client: AxiosInstance = axios.create({
|
||||||
baseURL: 'https://api.typeform.com',
|
baseURL: 'https://api.typeform.com',
|
||||||
});
|
});
|
||||||
@@ -19,7 +22,7 @@ export default class Authentication {
|
|||||||
'workspaces:read',
|
'workspaces:read',
|
||||||
];
|
];
|
||||||
|
|
||||||
constructor(appData: any, connectionData: any) {
|
constructor(appData: AppInfo, connectionData: JSONObject) {
|
||||||
this.connectionData = connectionData;
|
this.connectionData = connectionData;
|
||||||
this.appData = appData;
|
this.appData = appData;
|
||||||
}
|
}
|
||||||
@@ -32,7 +35,7 @@ export default class Authentication {
|
|||||||
|
|
||||||
async createAuthData() {
|
async createAuthData() {
|
||||||
const searchParams = new URLSearchParams({
|
const searchParams = new URLSearchParams({
|
||||||
client_id: this.connectionData.consumerKey,
|
client_id: this.connectionData.consumerKey as string,
|
||||||
redirect_uri: this.oauthRedirectUrl,
|
redirect_uri: this.oauthRedirectUrl,
|
||||||
scope: this.scope.join(' '),
|
scope: this.scope.join(' '),
|
||||||
});
|
});
|
||||||
@@ -45,9 +48,9 @@ export default class Authentication {
|
|||||||
async verifyCredentials() {
|
async verifyCredentials() {
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
grant_type: 'authorization_code',
|
grant_type: 'authorization_code',
|
||||||
code: this.connectionData.oauthVerifier,
|
code: this.connectionData.oauthVerifier as string,
|
||||||
client_id: this.connectionData.consumerKey,
|
client_id: this.connectionData.consumerKey as string,
|
||||||
client_secret: this.connectionData.consumerSecret,
|
client_secret: this.connectionData.consumerSecret as string,
|
||||||
redirect_uri: this.oauthRedirectUrl,
|
redirect_uri: this.oauthRedirectUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -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<JSONObject>;
|
||||||
|
isStillVerified(): Promise<boolean>;
|
||||||
|
}
|
1
packages/backend/src/types/interfaces/index.d.ts
vendored
Normal file
1
packages/backend/src/types/interfaces/index.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
declare module 'interfaces';
|
9
packages/backend/src/types/interfaces/json-object.ts
Normal file
9
packages/backend/src/types/interfaces/json-object.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
type JSONValue = string | number | boolean | JSONObject | JSONArray;
|
||||||
|
|
||||||
|
interface JSONObject {
|
||||||
|
[x: string]: JSONValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
type JSONArray = Array<JSONValue>;
|
||||||
|
|
||||||
|
export default JSONObject;
|
@@ -9,18 +9,9 @@
|
|||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
"*": [
|
"*": ["node_modules/*", "src/types/*"]
|
||||||
"node_modules/*",
|
|
||||||
"src/types/*"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"typeRoots": [
|
"typeRoots": ["node_modules/@types", "./src/types", "./src/apps/*/types"]
|
||||||
"node_modules/@types",
|
|
||||||
"./src/types",
|
|
||||||
"./src/apps"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"include": [
|
"include": ["src/**/*"]
|
||||||
"src/**/*"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user