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(','),
|
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,
|
||||||
|
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 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,
|
||||||
};
|
};
|
||||||
|
@@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -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;
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user