Merge pull request #348 from automatisch/refactor/use-http-client-for-twitter-oauth

refactor: Adjust twitter authentication to use http client
This commit is contained in:
Ömer Faruk Aydın
2022-07-19 18:33:34 +03:00
committed by GitHub
3 changed files with 91 additions and 25 deletions

View File

@@ -52,6 +52,7 @@
"luxon": "2.3.1", "luxon": "2.3.1",
"morgan": "^1.10.0", "morgan": "^1.10.0",
"nodemailer": "6.7.0", "nodemailer": "6.7.0",
"oauth-1.0a": "^2.2.6",
"objection": "^3.0.0", "objection": "^3.0.0",
"octokit": "^1.7.1", "octokit": "^1.7.1",
"pg": "^8.7.1", "pg": "^8.7.1",

View File

@@ -4,25 +4,37 @@ import type {
IField, IField,
IJSONObject, IJSONObject,
} from '@automatisch/types'; } from '@automatisch/types';
import TwitterApi, { TwitterApiTokens } from 'twitter-api-v2'; import HttpClient from '../../helpers/http-client';
import OAuth from 'oauth-1.0a';
import crypto from 'crypto';
import { URLSearchParams } from 'url';
export default class Authentication implements IAuthentication { export default class Authentication implements IAuthentication {
appData: IApp; appData: IApp;
connectionData: IJSONObject; connectionData: IJSONObject;
client: TwitterApi;
client: HttpClient;
oauthClient: OAuth;
static baseUrl = 'https://api.twitter.com';
constructor(appData: IApp, connectionData: IJSONObject) { constructor(appData: IApp, connectionData: IJSONObject) {
this.appData = appData; this.appData = appData;
this.connectionData = connectionData; this.connectionData = connectionData;
this.client = new HttpClient({ baseURL: Authentication.baseUrl });
const clientParams = { this.oauthClient = new OAuth({
appKey: connectionData.consumerKey, consumer: {
appSecret: connectionData.consumerSecret, key: this.connectionData.consumerKey as string,
accessToken: connectionData.accessToken, secret: this.connectionData.consumerSecret as string,
accessSecret: connectionData.accessSecret, },
} as TwitterApiTokens; signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
this.client = new TwitterApi(clientParams); return crypto
.createHmac('sha1', key)
.update(base_string)
.digest('base64');
},
});
} }
async createAuthData() { async createAuthData() {
@@ -31,35 +43,83 @@ export default class Authentication implements IAuthentication {
); );
const callbackUrl = appFields.value; const callbackUrl = appFields.value;
const authLink = await this.client.generateAuthLink(callbackUrl); const requestData = {
url: `${Authentication.baseUrl}/oauth/request_token`,
method: 'POST',
data: { oauth_callback: callbackUrl },
};
const authHeader = this.oauthClient.toHeader(
this.oauthClient.authorize(requestData)
);
try {
const response = await this.client.post(`/oauth/request_token`, null, {
headers: { ...authHeader },
});
const responseData = Object.fromEntries(
new URLSearchParams(response.data)
);
return { return {
url: authLink.url, url: `${Authentication.baseUrl}/oauth/authorize?oauth_token=${responseData.oauth_token}`,
accessToken: authLink.oauth_token, accessToken: responseData.oauth_token,
accessSecret: authLink.oauth_token_secret, accessSecret: responseData.oauth_token_secret,
}; };
} catch (error) {
const errorMessages = error.response.data.errors
.map((error: IJSONObject) => error.message)
.join(' ');
throw new Error(
`Error occured while verifying credentials: ${errorMessages}`
);
}
} }
async verifyCredentials() { async verifyCredentials() {
const verifiedCredentials = await this.client.login( const verifiedCredentials = await this.client.post(
this.connectionData.oauthVerifier as string `/oauth/access_token?oauth_verifier=${this.connectionData.oauthVerifier}&oauth_token=${this.connectionData.accessToken}`,
null
);
const responseData = Object.fromEntries(
new URLSearchParams(verifiedCredentials.data)
); );
return { return {
consumerKey: this.connectionData.consumerKey, consumerKey: this.connectionData.consumerKey,
consumerSecret: this.connectionData.consumerSecret, consumerSecret: this.connectionData.consumerSecret,
accessToken: verifiedCredentials.accessToken, accessToken: responseData.oauth_token,
accessSecret: verifiedCredentials.accessSecret, accessSecret: responseData.oauth_token_secret,
userId: verifiedCredentials.userId, userId: responseData.user_id,
screenName: verifiedCredentials.screenName, screenName: responseData.screen_name,
}; };
} }
async isStillVerified() { async isStillVerified() {
try { try {
await this.client.currentUser(); const token = {
key: this.connectionData.accessToken as string,
secret: this.connectionData.accessSecret as string,
};
const requestData = {
url: `${Authentication.baseUrl}/1.1/account/verify_credentials.json`,
method: 'GET',
};
const authHeader = this.oauthClient.toHeader(
this.oauthClient.authorize(requestData, token)
);
await this.client.get(`/1.1/account/verify_credentials.json`, {
headers: { ...authHeader },
});
return true; return true;
} catch { } catch (error) {
return false; return false;
} }
} }

View File

@@ -14012,6 +14012,11 @@ nwsapi@^2.2.0:
resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.2.0.tgz#204879a9e3d068ff2a55139c2c772780681a38b7" resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.2.0.tgz#204879a9e3d068ff2a55139c2c772780681a38b7"
integrity sha512-h2AatdwYH+JHiZpv7pt/gSX1XoRGb7L/qSIeuqA6GwYoF9w1vP1cw42TO0aI2pNyshRK5893hNSl+1//vHK7hQ== integrity sha512-h2AatdwYH+JHiZpv7pt/gSX1XoRGb7L/qSIeuqA6GwYoF9w1vP1cw42TO0aI2pNyshRK5893hNSl+1//vHK7hQ==
oauth-1.0a@^2.2.6:
version "2.2.6"
resolved "https://registry.yarnpkg.com/oauth-1.0a/-/oauth-1.0a-2.2.6.tgz#eadbccdb3bceea412d24586e6f39b2b412f0e491"
integrity sha512-6bkxv3N4Gu5lty4viIcIAnq5GbxECviMBeKR3WX/q87SPQ8E8aursPZUtsXDnxCs787af09WPRBLqYrf/lwoYQ==
oauth-sign@~0.9.0: oauth-sign@~0.9.0:
version "0.9.0" version "0.9.0"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"