diff --git a/packages/backend/src/apps/asana/assets/favicon.svg b/packages/backend/src/apps/asana/assets/favicon.svg new file mode 100644 index 00000000..37cfba39 --- /dev/null +++ b/packages/backend/src/apps/asana/assets/favicon.svg @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/packages/backend/src/apps/asana/auth/generate-auth-url.js b/packages/backend/src/apps/asana/auth/generate-auth-url.js new file mode 100644 index 00000000..060a9249 --- /dev/null +++ b/packages/backend/src/apps/asana/auth/generate-auth-url.js @@ -0,0 +1,25 @@ +import { URLSearchParams } from 'url'; +import crypto from 'crypto'; + +export default async function generateAuthUrl($) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const state = crypto.randomBytes(100).toString('base64url'); + + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId, + redirect_uri: redirectUri, + response_type: 'code', + //scope: authScope.join(' '), + state, + }); + + const url = `https://app.asana.com/-/oauth_authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + originalState: state, + }); +} diff --git a/packages/backend/src/apps/asana/auth/index.js b/packages/backend/src/apps/asana/auth/index.js new file mode 100644 index 00000000..83907762 --- /dev/null +++ b/packages/backend/src/apps/asana/auth/index.js @@ -0,0 +1,48 @@ +import generateAuthUrl from './generate-auth-url.js'; +import verifyCredentials from './verify-credentials.js'; +import refreshToken from './refresh-token.js'; +import isStillVerified from './is-still-verified.js'; + +export default { + fields: [ + { + key: 'oAuthRedirectUrl', + label: 'OAuth Redirect URL', + type: 'string', + required: true, + readOnly: true, + value: '{WEB_APP_URL}/app/asana/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Asana, enter the URL above.', + clickToCopy: true, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + { + key: 'clientSecret', + label: 'Client Secret', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, + refreshToken, +}; diff --git a/packages/backend/src/apps/asana/auth/is-still-verified.js b/packages/backend/src/apps/asana/auth/is-still-verified.js new file mode 100644 index 00000000..42cebf80 --- /dev/null +++ b/packages/backend/src/apps/asana/auth/is-still-verified.js @@ -0,0 +1,8 @@ +import getCurrentUser from '../common/get-current-user.js'; + +const isStillVerified = async ($) => { + const currentUser = await getCurrentUser($); + return !!currentUser.data.email; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/asana/auth/refresh-token.js b/packages/backend/src/apps/asana/auth/refresh-token.js new file mode 100644 index 00000000..896a6cd9 --- /dev/null +++ b/packages/backend/src/apps/asana/auth/refresh-token.js @@ -0,0 +1,37 @@ +import { URLSearchParams } from 'node:url'; + +const refreshToken = async ($) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + + const params = new URLSearchParams({ + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + redirect_uri: redirectUri, + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken, + }); + + const { data } = await $.http.post( + 'https://app.asana.com/-/oauth_token', + params.toString(), + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + additionalProperties: { + skipAddingAuthHeader: true, + }, + } + ); + + await $.auth.set({ + accessToken: data.access_token, + expiresIn: data.expires_in, + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/asana/auth/verify-credentials.js b/packages/backend/src/apps/asana/auth/verify-credentials.js new file mode 100644 index 00000000..bee738cc --- /dev/null +++ b/packages/backend/src/apps/asana/auth/verify-credentials.js @@ -0,0 +1,39 @@ +const verifyCredentials = async ($) => { + if ($.auth.data.originalState !== $.auth.data.state) { + throw new Error("The 'state' parameter does not match."); + } + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const { data } = await $.http.post( + 'https://app.asana.com/-/oauth_token', + { + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + code: $.auth.data.code, + grant_type: 'authorization_code', + redirect_uri: redirectUri, + }, + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + } + ); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: $.auth.data.scope, + id: data.data.id, + gid: data.data.gid, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + screenName: `${data.data.name} - ${data.data.email}`, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/asana/common/add-auth-header.js b/packages/backend/src/apps/asana/common/add-auth-header.js new file mode 100644 index 00000000..679ef605 --- /dev/null +++ b/packages/backend/src/apps/asana/common/add-auth-header.js @@ -0,0 +1,12 @@ +const addAuthHeader = ($, requestConfig) => { + if (requestConfig.additionalProperties?.skipAddingAuthHeader) + return requestConfig; + + if ($.auth.data?.accessToken) { + requestConfig.headers.Authorization = `${$.auth.data.tokenType} ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/asana/common/get-current-user.js b/packages/backend/src/apps/asana/common/get-current-user.js new file mode 100644 index 00000000..299796ff --- /dev/null +++ b/packages/backend/src/apps/asana/common/get-current-user.js @@ -0,0 +1,8 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get( + `/1.0/users/${$.auth.data.gid}` + ); + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/asana/index.js b/packages/backend/src/apps/asana/index.js new file mode 100644 index 00000000..3e287342 --- /dev/null +++ b/packages/backend/src/apps/asana/index.js @@ -0,0 +1,16 @@ +import defineApp from '../../helpers/define-app.js'; +import addAuthHeader from './common/add-auth-header.js'; +import auth from './auth/index.js'; + +export default defineApp({ + name: 'Asana', + key: 'asana', + baseUrl: 'https://asana.com', + apiBaseUrl: 'https://app.asana.com/api', + iconUrl: '{BASE_URL}/apps/asana/assets/favicon.svg', + authDocUrl: '{DOCS_URL}/apps/asana/connection', + primaryColor: '690031', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 219a1d51..69ac3e1a 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -50,6 +50,12 @@ export default defineConfig({ { text: 'Connection', link: '/apps/appwrite/connection' }, ], }, + { + text: 'Asana', + collapsible: true, + collapsed: true, + items: [{ text: 'Connection', link: '/apps/asana/connection' }], + }, { text: 'Carbone', collapsible: true, diff --git a/packages/docs/pages/apps/asana/connection.md b/packages/docs/pages/apps/asana/connection.md new file mode 100644 index 00000000..cb703fde --- /dev/null +++ b/packages/docs/pages/apps/asana/connection.md @@ -0,0 +1,17 @@ +# Asana + +:::info +This page explains the steps you need to follow to set up the Asana +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to the [Asana developer console](https://app.asana.com/0/my-apps) to create a project. +2. Click on the **Create new app** in **My Apps**, and click on the **New Project** button. +3. Fill the form for your project and click on the **Create app** button. +4. Go to **Manage distrubition** and select **Any workspace** option in **Choose a distribution method** and save it. +5. Go to **OAuth** tab from left panel and click on the **+Add redirect URL** button. +6. Copy **OAuth Redirect URL** from Automatisch to the redirect url field, and click on the **Add** button. +7. Copy the **Your Client ID** value on the same page to the `Client ID` field on Automatisch. +8. Copy the **Your Client secret** value on the same page to the `Client Secret` field on Automatisch. +9. Click **Submit** button on Automatisch. +10. Congrats! Start using your new Asana connection within the flows. diff --git a/packages/docs/pages/public/favicons/asana.svg b/packages/docs/pages/public/favicons/asana.svg new file mode 100644 index 00000000..37cfba39 --- /dev/null +++ b/packages/docs/pages/public/favicons/asana.svg @@ -0,0 +1,8 @@ + + + + + + + +