feat(todoist): enhance auth with oauth 2.0
This commit is contained in:
@@ -13,9 +13,8 @@ export default async function generateAuthUrl($: IGlobalVariable) {
|
||||
scope: scopes.join(','),
|
||||
});
|
||||
|
||||
const url = `${
|
||||
$.app.baseUrl
|
||||
}/login/oauth/authorize?${searchParams.toString()}`;
|
||||
const url = `${$.app.baseUrl
|
||||
}/login/oauth/authorize?${searchParams.toString()}`;
|
||||
|
||||
await $.auth.set({
|
||||
url,
|
||||
|
17
packages/backend/src/apps/todoist/auth/generate-auth-url.ts
Normal file
17
packages/backend/src/apps/todoist/auth/generate-auth-url.ts
Normal 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,
|
||||
});
|
||||
}
|
@@ -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,
|
||||
};
|
||||
|
@@ -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;
|
||||
};
|
||||
|
||||
|
@@ -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;
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user