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

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

View File

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

View File

@@ -1,7 +1,19 @@
import { IGlobalVariable } from '@automatisch/types'; import { IGlobalVariable } from '@automatisch/types';
const verifyCredentials = async ($: IGlobalVariable) => { 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; export default verifyCredentials;

View File

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