feat: introduce twitch auth

This commit is contained in:
Ali BARIN
2021-10-24 21:25:04 +02:00
parent 8e91f1f7ed
commit 9e3c61e3df
4 changed files with 469 additions and 6 deletions

View File

@@ -0,0 +1,96 @@
import TwitchApi, { ApiVersions } from 'twitch-js';
import fetchUtil from 'twitch-js/lib/utils/fetch';
import App from '../../models/app';
import Field from '../../types/field';
type TwitchTokenResponse = {
accessToken: string;
refreshToken: string;
expiresIn: string;
tokenType: string;
};
export default class Twitch {
client: any
connectionData: any
appData: any
constructor(connectionData: any) {
this.connectionData = connectionData;
this.appData = App.findOneByKey('twitch');
if (this.clientOptions.token) {
this.client = new TwitchApi(this.clientOptions);
}
}
get clientOptions() {
return {
token: this.connectionData.accessToken,
clientId: this.connectionData.consumerKey,
log: { enabled: true }
};
}
get oauthRedirectUrl() {
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
}
async createAuthLink() {
const { url } = await fetchUtil('https://id.twitch.tv/oauth2/authorize', {
search: {
client_id: this.connectionData.consumerKey,
redirect_uri: this.oauthRedirectUrl,
response_type: 'code',
scope: 'user:read:email',
}
});
return { url };
}
async verifyCredentials() {
const verifiedCredentials = await fetchUtil('https://id.twitch.tv/oauth2/token', {
method: 'post',
search: {
client_id: this.connectionData.consumerKey,
client_secret: this.connectionData.consumerSecret,
code: this.connectionData.oauthVerifier,
grant_type: 'authorization_code',
redirect_uri: this.oauthRedirectUrl
}
}) as TwitchTokenResponse;
this.connectionData.accessToken = verifiedCredentials.accessToken;
const { api } = new TwitchApi(this.clientOptions);
const { data } = await api.get('users');
const [user] = data;
return {
consumerKey: this.connectionData.consumerKey,
consumerSecret: this.connectionData.consumerSecret,
accessToken: verifiedCredentials.accessToken,
refreshToken: verifiedCredentials.refreshToken,
expiresIn: verifiedCredentials.expiresIn,
tokenType: verifiedCredentials.tokenType,
userId: user.id,
screenName: user.displayName,
}
}
async isStillVerified() {
try {
await fetchUtil('https://id.twitch.tv/oauth2/userinfo', {
headers: {
Authorization: `Bearer ${this.connectionData.accessToken}`,
},
});
return true;
} catch (err) {
return false
}
}
}

View File

@@ -3,7 +3,7 @@
"key": "twitch",
"iconUrl": "{BASE_URL}/apps/twitch/assets/favicon.svg",
"docUrl": "https://automatisch.io/docs/twitch",
"primaryColor": "6441a5",
"primaryColor": "2DAAE1",
"fields": [
{
"key": "oAuthRedirectUrl",
@@ -11,7 +11,7 @@
"type": "string",
"required": true,
"readOnly": true,
"value": "http://localhost:3000/sample",
"value": "https://localhost:3001/app/twitch/connections/add",
"placeholder": null,
"description": "When asked to input an OAuth callback or redirect URL in Twitch OAuth, enter the URL above.",
"docUrl": "https://automatisch.io/docs/twitch#oauth-redirect-url",
@@ -41,5 +41,178 @@
"docUrl": "https://automatisch.io/docs/twitch#consumer-secret",
"clickToCopy": false
}
],
"authenticationSteps": [
{
"step": 1,
"type": "mutation",
"name": "createConnection",
"fields": [
{
"name": "key",
"value": "{key}"
},
{
"name": "data",
"value": null,
"fields": [
{
"name": "consumerKey",
"value": "{fields.consumerKey}"
},
{
"name": "consumerSecret",
"value": "{fields.consumerSecret}"
}
]
}
]
},
{
"step": 2,
"type": "mutation",
"name": "createAuthLink",
"fields": [
{
"name": "id",
"value": "{createConnection.id}"
}
]
},
{
"step": 3,
"type": "openWithPopup",
"name": "openAuthPopup",
"fields": [
{
"name": "url",
"value": "{createAuthLink.url}"
}
]
},
{
"step": 4,
"type": "mutation",
"name": "updateConnection",
"fields": [
{
"name": "id",
"value": "{createConnection.id}"
},
{
"name": "data",
"value": null,
"fields": [
{
"name": "oauthVerifier",
"value": "{openAuthPopup.code}"
}
]
}
]
},
{
"step": 5,
"type": "mutation",
"name": "verifyConnection",
"fields": [
{
"name": "id",
"value": "{createConnection.id}"
}
]
}
],
"reconnectionSteps": [
{
"step": 1,
"type": "mutation",
"name": "resetConnection",
"fields": [
{
"name": "id",
"value": "{connection.id}"
}
]
},
{
"step": 2,
"type": "mutation",
"name": "updateConnection",
"fields": [
{
"name": "id",
"value": "{connection.id}"
},
{
"name": "data",
"value": null,
"fields": [
{
"name": "consumerKey",
"value": "{fields.consumerKey}"
},
{
"name": "consumerSecret",
"value": "{fields.consumerSecret}"
}
]
}
]
},
{
"step": 3,
"type": "mutation",
"name": "createAuthLink",
"fields": [
{
"name": "id",
"value": "{connection.id}"
}
]
},
{
"step": 4,
"type": "openWithPopup",
"name": "openAuthPopup",
"fields": [
{
"name": "url",
"value": "{createAuthLink.url}"
}
]
},
{
"step": 5,
"type": "mutation",
"name": "updateConnection",
"fields": [
{
"name": "id",
"value": "{connection.id}"
},
{
"name": "data",
"value": null,
"fields": [
{
"name": "oauthVerifier",
"value": "{openAuthPopup.code}"
}
]
}
]
},
{
"step": 6,
"type": "mutation",
"name": "verifyConnection",
"fields": [
{
"name": "id",
"value": "{connection.id}"
}
]
}
]
}