refactor: Use http client to authenticate github

This commit is contained in:
Faruk AYDIN
2022-07-18 01:31:07 +03:00
parent c85aadf006
commit 95d03e00da
4 changed files with 37 additions and 41 deletions

View File

@@ -24,7 +24,6 @@
"@gitbeaker/node": "^35.6.0", "@gitbeaker/node": "^35.6.0",
"@graphql-tools/graphql-file-loader": "^7.3.4", "@graphql-tools/graphql-file-loader": "^7.3.4",
"@graphql-tools/load": "^7.5.2", "@graphql-tools/load": "^7.5.2",
"@octokit/oauth-methods": "^1.2.6",
"@rudderstack/rudder-sdk-node": "^1.1.2", "@rudderstack/rudder-sdk-node": "^1.1.2",
"@slack/bolt": "3.10.0", "@slack/bolt": "3.10.0",
"@types/luxon": "^2.3.1", "@types/luxon": "^2.3.1",

View File

@@ -4,35 +4,19 @@ import type {
IField, IField,
IJSONObject, IJSONObject,
} from '@automatisch/types'; } from '@automatisch/types';
import { import HttpClient from '../../helpers/http-client';
getWebFlowAuthorizationUrl, import { URLSearchParams } from 'url';
exchangeWebFlowCode,
checkToken,
} from '@octokit/oauth-methods';
export default class Authentication implements IAuthentication { export default class Authentication implements IAuthentication {
appData: IApp; appData: IApp;
connectionData: IJSONObject; connectionData: IJSONObject;
scopes: string[] = [ scopes: string[] = ['read:org', 'repo', 'user'];
'read:org', client: HttpClient;
'repo',
'user',
];
client: {
getWebFlowAuthorizationUrl: typeof getWebFlowAuthorizationUrl;
exchangeWebFlowCode: typeof exchangeWebFlowCode;
checkToken: typeof checkToken;
};
constructor(appData: IApp, connectionData: IJSONObject) { constructor(appData: IApp, connectionData: IJSONObject) {
this.connectionData = connectionData; this.connectionData = connectionData;
this.appData = appData; this.appData = appData;
this.client = new HttpClient({ baseURL: 'https://github.com' });
this.client = {
getWebFlowAuthorizationUrl,
exchangeWebFlowCode,
checkToken,
};
} }
get oauthRedirectUrl(): string { get oauthRedirectUrl(): string {
@@ -42,26 +26,28 @@ export default class Authentication implements IAuthentication {
} }
async createAuthData(): Promise<{ url: string }> { async createAuthData(): Promise<{ url: string }> {
const { url } = await this.client.getWebFlowAuthorizationUrl({ const searchParams = new URLSearchParams({
clientType: 'oauth-app', client_id: this.connectionData.consumerKey as string,
clientId: this.connectionData.consumerKey as string, redirect_uri: this.oauthRedirectUrl,
redirectUrl: this.oauthRedirectUrl, scope: this.scopes.join(','),
scopes: this.scopes,
}); });
const url = `https://github.com/login/oauth/authorize?${searchParams.toString()}`;
return { return {
url: url, url,
}; };
} }
async verifyCredentials() { async verifyCredentials() {
const { data } = await this.client.exchangeWebFlowCode({ const response = await this.client.post('/login/oauth/access_token', {
clientType: 'oauth-app', client_id: this.connectionData.consumerKey,
clientId: this.connectionData.consumerKey as string, client_secret: this.connectionData.consumerSecret,
clientSecret: this.connectionData.consumerSecret as string, code: this.connectionData.oauthVerifier,
code: this.connectionData.oauthVerifier as string,
}); });
const data = Object.fromEntries(new URLSearchParams(response.data));
this.connectionData.accessToken = data.access_token; this.connectionData.accessToken = data.access_token;
const tokenInfo = await this.getTokenInfo(); const tokenInfo = await this.getTokenInfo();
@@ -78,12 +64,23 @@ export default class Authentication implements IAuthentication {
} }
async getTokenInfo() { async getTokenInfo() {
return this.client.checkToken({ const basicAuthToken = Buffer.from(
clientType: 'oauth-app', this.connectionData.consumerKey + ':' + this.connectionData.consumerSecret
clientId: this.connectionData.consumerKey as string, ).toString('base64');
clientSecret: this.connectionData.consumerSecret as string,
token: this.connectionData.accessToken as string, const headers = {
}); Authorization: `Basic ${basicAuthToken}`,
};
const body = {
access_token: this.connectionData.accessToken,
};
return await this.client.post(
`https://api.github.com/applications/${this.connectionData.consumerKey}/token`,
body,
{ headers }
);
} }
async isStillVerified() { async isStillVerified() {

View File

@@ -177,5 +177,5 @@ export interface ISubstep {
} }
export type IHttpClientParams = { export type IHttpClientParams = {
baseURL: string; baseURL?: string;
} }

View File

@@ -3744,7 +3744,7 @@
resolved "https://registry.yarnpkg.com/@octokit/oauth-authorization-url/-/oauth-authorization-url-4.3.3.tgz#6a6ef38f243086fec882b62744f39b517528dfb9" resolved "https://registry.yarnpkg.com/@octokit/oauth-authorization-url/-/oauth-authorization-url-4.3.3.tgz#6a6ef38f243086fec882b62744f39b517528dfb9"
integrity sha512-lhP/t0i8EwTmayHG4dqLXgU+uPVys4WD/qUNvC+HfB1S1dyqULm5Yx9uKc1x79aP66U1Cb4OZeW8QU/RA9A4XA== integrity sha512-lhP/t0i8EwTmayHG4dqLXgU+uPVys4WD/qUNvC+HfB1S1dyqULm5Yx9uKc1x79aP66U1Cb4OZeW8QU/RA9A4XA==
"@octokit/oauth-methods@^1.1.0", "@octokit/oauth-methods@^1.2.2", "@octokit/oauth-methods@^1.2.6": "@octokit/oauth-methods@^1.1.0", "@octokit/oauth-methods@^1.2.2":
version "1.2.6" version "1.2.6"
resolved "https://registry.yarnpkg.com/@octokit/oauth-methods/-/oauth-methods-1.2.6.tgz#b9ac65e374b2cc55ee9dd8dcdd16558550438ea7" resolved "https://registry.yarnpkg.com/@octokit/oauth-methods/-/oauth-methods-1.2.6.tgz#b9ac65e374b2cc55ee9dd8dcdd16558550438ea7"
integrity sha512-nImHQoOtKnSNn05uk2o76om1tJWiAo4lOu2xMAHYsNr0fwopP+Dv+2MlGvaMMlFjoqVd3fF3X5ZDTKCsqgmUaQ== integrity sha512-nImHQoOtKnSNn05uk2o76om1tJWiAo4lOu2xMAHYsNr0fwopP+Dv+2MlGvaMMlFjoqVd3fF3X5ZDTKCsqgmUaQ==