feat(discord): add auth

This commit is contained in:
Ali BARIN
2022-10-29 16:43:44 +02:00
parent c46fbef03c
commit 2f9e40fc0e
13 changed files with 365 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
import scopes from '../common/scopes';
export default async function createAuthData($: IGlobalVariable) {
try {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value as string;
const searchParams = new URLSearchParams({
client_id: $.auth.data.consumerKey as string,
redirect_uri: callbackUrl,
response_type: 'code',
scope: scopes.join(' '),
});
const url = `${$.app.apiBaseUrl}/oauth2/authorize?${searchParams.toString()}`;
await $.auth.set({ url });
} catch (error) {
throw new Error(
`Error occured while verifying credentials: ${error}`
);
}
}

View File

@@ -0,0 +1,221 @@
import createAuthData from './create-auth-data';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
export default {
fields: [
{
key: 'oAuthRedirectUrl',
label: 'OAuth Redirect URL',
type: 'string' as const,
required: true,
readOnly: true,
value: '{WEB_APP_URL}/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' as const,
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' as const,
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' as const,
name: 'createConnection',
arguments: [
{
name: 'key',
value: '{key}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}'
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}'
}
]
}
]
},
{
step: 2,
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
}
]
},
{
step: 3,
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}'
}
]
},
{
step: 4,
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.code}'
}
]
}
]
},
{
step: 5,
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
}
]
}
],
reconnectionSteps: [
{
step: 1,
type: 'mutation' as const,
name: 'resetConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
},
{
step: 2,
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'consumerKey',
value: '{fields.consumerKey}'
},
{
name: 'consumerSecret',
value: '{fields.consumerSecret}'
}
]
}
]
},
{
step: 3,
type: 'mutation' as const,
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
},
{
step: 4,
type: 'openWithPopup' as const,
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}'
}
]
},
{
step: 5,
type: 'mutation' as const,
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.code}'
}
]
}
]
},
{
step: 6,
type: 'mutation' as const,
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
}
],
createAuthData,
verifyCredentials,
isStillVerified,
};

View File

@@ -0,0 +1,14 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
const isStillVerified = async ($: IGlobalVariable) => {
try {
await getCurrentUser($);
return true;
} catch (error) {
return false;
}
};
export default isStillVerified;

View File

@@ -0,0 +1,50 @@
import { IGlobalVariable, IField } from '@automatisch/types';
import { URLSearchParams } from 'url';
import scopes from '../common/scopes';
import getCurrentUser from '../common/get-current-user';
const verifyCredentials = async ($: IGlobalVariable) => {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value as string;
const params = new URLSearchParams({
client_id: $.auth.data.consumerKey as string,
redirect_uri: callbackUrl,
response_type: 'code',
scope: scopes.join(' '),
client_secret: $.auth.data.consumerSecret as string,
code: $.auth.data.oauthVerifier as string,
grant_type: 'authorization_code',
});
const { data: verifiedCredentials } = await $.http.post(
'/oauth2/token',
params.toString()
);
const {
access_token: accessToken,
refresh_token: refreshToken,
expires_in: expiresIn,
scope: scope,
token_type: tokenType,
} = verifiedCredentials;
await $.auth.set({
accessToken,
refreshToken,
expiresIn,
scope,
tokenType,
})
const user = await getCurrentUser($);
await $.auth.set({
userId: user.id,
screenName: user.username,
email: user.email,
});
};
export default verifyCredentials;