refactor: clean up github and rewrite its auth

This commit is contained in:
Ali BARIN
2022-10-16 19:49:45 +02:00
parent a69cd51dda
commit 314787f39c
37 changed files with 398 additions and 1756 deletions

View File

@@ -0,0 +1,21 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
export default async function createAuthData($: IGlobalVariable) {
const scopes = ['read:org', 'repo', 'user'];
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const redirectUri = oauthRedirectUrlField.value as string;
const searchParams = new URLSearchParams({
client_id: $.auth.data.consumerKey as string,
redirect_uri: redirectUri,
scope: scopes.join(','),
});
const url = `${$.app.baseUrl}/login/oauth/authorize?${searchParams.toString()}`;
await $.auth.set({
url,
});
}

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',
required: true,
readOnly: true,
value: '{WEB_APP_URL}/app/github/connections/add',
placeholder: null,
description: 'When asked to input an OAuth callback or redirect URL in Github OAuth, enter the URL above.',
docUrl: 'https://automatisch.io/docs/github#oauth-redirect-url',
clickToCopy: true
},
{
key: 'consumerKey',
label: 'Client ID',
type: 'string',
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
docUrl: 'https://automatisch.io/docs/github#client-id',
clickToCopy: false
},
{
key: 'consumerSecret',
label: 'Client Secret',
type: 'string',
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
docUrl: 'https://automatisch.io/docs/github#client-secret',
clickToCopy: false
}
],
authenticationSteps: [
{
step: 1,
type: 'mutation',
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',
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
}
]
},
{
step: 3,
type: 'openWithPopup',
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}'
}
]
},
{
step: 4,
type: 'mutation',
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.code}'
}
]
}
]
},
{
step: 5,
type: 'mutation',
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{createConnection.id}'
}
]
}
],
reconnectionSteps: [
{
step: 1,
type: 'mutation',
name: 'resetConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
},
{
step: 2,
type: 'mutation',
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',
name: 'createAuthData',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
},
{
step: 4,
type: 'openWithPopup',
name: 'openAuthPopup',
arguments: [
{
name: 'url',
value: '{createAuthData.url}'
}
]
},
{
step: 5,
type: 'mutation',
name: 'updateConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
},
{
name: 'formattedData',
value: null,
properties: [
{
name: 'oauthVerifier',
value: '{openAuthPopup.code}'
}
]
}
]
},
{
step: 6,
type: 'mutation',
name: 'verifyConnection',
arguments: [
{
name: 'id',
value: '{connection.id}'
}
]
}
],
createAuthData,
verifyCredentials,
isStillVerified,
};

View File

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

View File

@@ -0,0 +1,59 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
async function getTokenInfo($: IGlobalVariable) {
const basicAuthToken = Buffer.from(
$.auth.data.consumerKey + ':' + $.auth.data.consumerSecret
).toString('base64');
const headers = {
Authorization: `Basic ${basicAuthToken}`,
};
const body = {
access_token: $.auth.data.accessToken,
};
return await $.http.post(
`${$.app.baseUrl}/applications/${$.auth.data.consumerKey}/token`,
body,
{ headers }
);
}
const verifyCredentials = async ($: IGlobalVariable) => {
try {
const response = await $.http.post(
`${$.app.baseUrl}/login/oauth/access_token`,
{
client_id: $.auth.data.consumerKey,
client_secret: $.auth.data.consumerSecret,
code: $.auth.data.oauthVerifier,
},
{
headers: {
Accept: 'application/json'
}
});
const data = response.data;
$.auth.data.accessToken = data.access_token;
const currentUser = await getCurrentUser($);
await $.auth.set({
consumerKey: $.auth.data.consumerKey,
consumerSecret: $.auth.data.consumerSecret,
accessToken: data.access_token,
scope: data.scope,
tokenType: data.token_type,
userId: currentUser.id,
screenName: currentUser.login,
});
} catch (error) {
throw new Error(error.response.data);
}
};
export default verifyCredentials;