chore: Extract authentication logic of the apps to their own classes
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
6b1dee053f
commit
46321f19f4
@@ -1,37 +1,9 @@
|
||||
import TwilioApi from 'twilio';
|
||||
import App from '../../models/app';
|
||||
import Authentication from './authentication';
|
||||
|
||||
export default class Twilio {
|
||||
client: any
|
||||
connectionData: any
|
||||
appData: any
|
||||
authenticationClient: any;
|
||||
|
||||
constructor(connectionData: any) {
|
||||
this.client = TwilioApi(connectionData.accountSid, connectionData.authToken);
|
||||
|
||||
this.connectionData = connectionData;
|
||||
this.appData = App.findOneByKey('twilio');
|
||||
}
|
||||
|
||||
async verifyCredentials() {
|
||||
await this.verify();
|
||||
|
||||
return {
|
||||
screenName: this.connectionData.accountSid
|
||||
}
|
||||
}
|
||||
|
||||
async verify() {
|
||||
try {
|
||||
await this.client.keys.list({ limit: 1 })
|
||||
return true;
|
||||
} catch(error) {
|
||||
// Test credentials throw HTTP 403 and thus, we need to have an exception.
|
||||
return error?.status === 403;
|
||||
}
|
||||
}
|
||||
|
||||
async isStillVerified() {
|
||||
return this.verify();
|
||||
this.authenticationClient = new Authentication(connectionData);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user