feat: introduce twitch auth
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
"morgan": "^1.10.0",
|
||||
"objection": "^2.2.17",
|
||||
"pg": "^8.7.1",
|
||||
"twitch-js": "2.0.0-beta.42",
|
||||
"twitter-api-v2": "1.6.0",
|
||||
"winston": "^3.3.3"
|
||||
},
|
||||
|
96
packages/backend/src/apps/twitch/index.ts
Normal file
96
packages/backend/src/apps/twitch/index.ts
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@@ -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}"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user