feat: introduce discord authentication

This commit is contained in:
Ali BARIN
2021-10-26 18:11:09 +02:00
parent ce7130b271
commit 901699fe64
5 changed files with 428 additions and 3 deletions

View File

@@ -15,9 +15,11 @@
"db:migrate": "knex migrate:latest"
},
"dependencies": {
"axios": "0.24.0",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"debug": "~2.6.9",
"discord.js": "13.2.0",
"dotenv": "^10.0.0",
"express": "~4.16.1",
"express-graphql": "^0.12.0",
@@ -41,7 +43,7 @@
}
],
"homepage": "https://github.com/automatisch/automatisch#readme",
"main": "src/backend.js",
"main": "src/app.ts",
"directories": {
"src": "src",
"test": "__tests__"

View File

@@ -0,0 +1,4 @@
<svg fill="#5865f2" aria-label="Discord" role="img" version="1.1" viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg">
<rect width="512" height="512" rx="15%" fill="#fff"/>
<path d="m386 137c-24-11-49.5-19-76.3-23.7c-.5 0-1 0-1.2.6c-3.3 5.9-7 13.5-9.5 19.5c-29-4.3-57.5-4.3-85.7 0c-2.6-6.2-6.3-13.7-10-19.5c-.3-.4-.7-.7-1.2-.6c-23 4.6-52.4 13-76 23.7c-.2 0-.4.2-.5.4c-49 73-62 143-55 213c0 .3.2.7.5 1c32 23.6 63 38 93.6 47.3c.5 0 1 0 1.3-.4c7.2-9.8 13.6-20.2 19.2-31.2c.3-.6 0-1.4-.7-1.6c-10-4-20-8.6-29.3-14c-.7-.4-.8-1.5 0-2c2-1.5 4-3 5.8-4.5c.3-.3.8-.3 1.2-.2c61.4 28 128 28 188 0c.4-.2.9-.1 1.2.1c1.9 1.6 3.8 3.1 5.8 4.6c.7.5.6 1.6 0 2c-9.3 5.5-19 10-29.3 14c-.7.3-1 1-.6 1.7c5.6 11 12.1 21.3 19 31c.3.4.8.6 1.3.4c30.6-9.5 61.7-23.8 93.8-47.3c.3-.2.5-.5.5-1c7.8-80.9-13.1-151-55.4-213c0-.2-.3-.4-.5-.4Zm-192 171c-19 0-34-17-34-38c0-21 15-38 34-38c19 0 34 17 34 38c0 21-15 38-34 38zm125 0c-19 0-34-17-34-38c0-21 15-38 34-38c19 0 34 17 34 38c0 21-15 38-34 38z" fill="#5865f2"/>
</svg>

After

Width:  |  Height:  |  Size: 993 B

View File

@@ -0,0 +1,91 @@
import { URLSearchParams } from 'url';
import DiscordApi from 'discord.js';
import axios, { AxiosInstance } from 'axios';
import App from '../../models/app';
import Field from '../../types/field';
export default class Discord {
client?: any
connectionData: any
appData: any
scope: string[] = ['identify', 'email']
httpClient: AxiosInstance = axios.create({
baseURL: 'https://discord.com/api/'
})
constructor(connectionData: any) {
this.connectionData = connectionData;
this.appData = App.findOneByKey('discord');
}
get oauthRedirectUrl() {
return this.appData.fields.find((field: Field) => field.key == 'oAuthRedirectUrl').value;
}
async createAuthLink() {
const searchParams = new URLSearchParams({
client_id: this.connectionData.consumerKey,
redirect_uri: this.oauthRedirectUrl,
response_type: 'code',
scope: this.scope.join(' '),
});
const url = `https://discord.com/api/oauth2/authorize?${searchParams.toString()}`;
return { url };
}
async verifyCredentials() {
const params = new URLSearchParams({
client_id: this.connectionData.consumerKey,
redirect_uri: this.oauthRedirectUrl,
response_type: 'code',
scope: this.scope.join(' '),
client_secret: this.connectionData.consumerSecret,
code: this.connectionData.oauthVerifier,
grant_type: 'authorization_code',
});
const { data: verifiedCredentials }: any = await this.httpClient.post('/oauth2/token', params.toString());
const {
access_token: accessToken,
refresh_token: refreshToken,
expires_in: expiresIn,
scope: scope,
token_type: tokenType,
} = verifiedCredentials;
const { data: user }: any = await this.httpClient.get('/users/@me', {
headers: {
Authorization: `${tokenType} ${accessToken}`,
},
});
return {
consumerKey: this.connectionData.consumerKey,
consumerSecret: this.connectionData.consumerSecret,
accessToken,
refreshToken,
expiresIn,
scope,
tokenType,
userId: user.id,
screenName: user.username,
email: user.email,
};
}
async isStillVerified() {
try {
await this.httpClient.get('/users/@me', {
headers: {
Authorization: `${this.connectionData.tokenType} ${this.connectionData.accessToken}`,
},
});
return true;
} catch {
return false
}
}
}

View File

@@ -0,0 +1,218 @@
{
"name": "Discord",
"key": "discord",
"iconUrl": "{BASE_URL}/apps/discord/assets/favicon.svg",
"docUrl": "https://automatisch.io/docs/discord",
"primaryColor": "2DAAE1",
"fields": [
{
"key": "oAuthRedirectUrl",
"label": "OAuth Redirect URL",
"type": "string",
"required": true,
"readOnly": true,
"value": "https://localhost:3001/app/discord/connections/add",
"placeholder": null,
"description": "When asked to input an OAuth callback or redirect URL in Discord OAuth, enter the URL above.",
"docUrl": "https://automatisch.io/docs/discord#oauth-redirect-url",
"clickToCopy": true
},
{
"key": "consumerKey",
"label": "Consumer Key",
"type": "string",
"required": true,
"readOnly": false,
"value": null,
"placeholder": null,
"description": null,
"docUrl": "https://automatisch.io/docs/discord#consumer-key",
"clickToCopy": false
},
{
"key": "consumerSecret",
"label": "Consumer Secret",
"type": "string",
"required": true,
"readOnly": false,
"value": null,
"placeholder": null,
"description": null,
"docUrl": "https://automatisch.io/docs/discord#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}"
}
]
}
]
}