chore: Add authentication interface and adjust authentication classes

This commit is contained in:
Faruk AYDIN
2022-03-01 15:14:56 +03:00
committed by Ali BARIN
parent a506d1985f
commit b2369fa8cd
14 changed files with 144 additions and 93 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 {

View File

@@ -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,
});

View File

@@ -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>;
}

View File

@@ -0,0 +1 @@
declare module 'interfaces';

View File

@@ -0,0 +1,9 @@
type JSONValue = string | number | boolean | JSONObject | JSONArray;
interface JSONObject {
[x: string]: JSONValue;
}
type JSONArray = Array<JSONValue>;
export default JSONObject;

View File

@@ -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/**/*"]
}