feat(todoist): enhance auth with oauth 2.0

This commit is contained in:
Ali BARIN
2023-01-11 19:22:01 +01:00
parent 8c8fd98bd5
commit 8090d8be5a
6 changed files with 52 additions and 12 deletions

View File

@@ -13,8 +13,7 @@ export default async function generateAuthUrl($: IGlobalVariable) {
scope: scopes.join(','),
});
const url = `${
$.app.baseUrl
const url = `${$.app.baseUrl
}/login/oauth/authorize?${searchParams.toString()}`;
await $.auth.set({

View File

@@ -0,0 +1,17 @@
import { IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
export default async function generateAuthUrl($: IGlobalVariable) {
const scopes = ['data:read_write'];
const searchParams = new URLSearchParams({
client_id: $.auth.data.clientId as string,
scope: scopes.join(','),
});
const url = `${$.app.baseUrl
}/oauth/authorize?${searchParams.toString()}`;
await $.auth.set({
url,
});
}

View File

@@ -1,3 +1,4 @@
import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
@@ -16,19 +17,30 @@ export default {
clickToCopy: false,
},
{
key: 'apiToken',
label: 'API Token',
key: 'clientId',
label: 'Client ID',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description:
'Your Todoist API token. See https://todoist.com/app/settings/integrations/developer',
description: null,
clickToCopy: false,
},
{
key: 'clientSecret',
label: 'Client Secret',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
clickToCopy: false,
},
],
generateAuthUrl,
verifyCredentials,
isStillVerified,
};

View File

@@ -1,8 +1,7 @@
import { IGlobalVariable } from '@automatisch/types';
import verifyCredentials from './verify-credentials';
const isStillVerified = async ($: IGlobalVariable) => {
await verifyCredentials($);
await $.http.get('/projects');
return true;
};

View File

@@ -1,7 +1,19 @@
import { IGlobalVariable } from '@automatisch/types';
const verifyCredentials = async ($: IGlobalVariable) => {
await $.http.get('/projects');
const { data } = await $.http.post(
`${$.app.baseUrl}/oauth/access_token`,
{
client_id: $.auth.data.clientId,
client_secret: $.auth.data.clientSecret,
code: $.auth.data.code,
},
);
await $.auth.set({
tokenType: data.token_type,
accessToken: data.access_token,
});
};
export default verifyCredentials;

View File

@@ -1,8 +1,9 @@
import { TBeforeRequest } from '@automatisch/types';
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
if ($.auth.data?.apiToken) {
const authorizationHeader = `Bearer ${$.auth.data.apiToken}`;
const authData = $.auth.data;
if (authData?.accessToken && authData?.tokenType) {
const authorizationHeader = `${authData.tokenType} ${authData.accessToken}`;
requestConfig.headers.Authorization = authorizationHeader;
}