diff --git a/packages/backend/src/apps/airtable/assets/favicon.svg b/packages/backend/src/apps/airtable/assets/favicon.svg new file mode 100644 index 00000000..867c3b5a --- /dev/null +++ b/packages/backend/src/apps/airtable/assets/favicon.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/packages/backend/src/apps/airtable/auth/generate-auth-url.js b/packages/backend/src/apps/airtable/auth/generate-auth-url.js new file mode 100644 index 00000000..70d5d7e3 --- /dev/null +++ b/packages/backend/src/apps/airtable/auth/generate-auth-url.js @@ -0,0 +1,38 @@ +import crypto from 'crypto'; +import { URLSearchParams } from 'url'; +import authScope from '../common/auth-scope.js'; + +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 codeVerifier = crypto.randomBytes(96).toString('base64url'); + const codeChallenge = crypto + .createHash('sha256') + .update(codeVerifier) + .digest('base64') + .replace(/=/g, '') + .replace(/\+/g, '-') + .replace(/\//g, '_'); + + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId, + redirect_uri: redirectUri, + response_type: 'code', + scope: authScope.join(' '), + state, + code_challenge: codeChallenge, + code_challenge_method: 'S256', + }); + + const url = `https://airtable.com/oauth2/v1/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + originalCodeChallenge: codeChallenge, + originalState: state, + codeVerifier, + }); +} diff --git a/packages/backend/src/apps/airtable/auth/index.js b/packages/backend/src/apps/airtable/auth/index.js new file mode 100644 index 00000000..6422369f --- /dev/null +++ b/packages/backend/src/apps/airtable/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/airtable/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Airtable, 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/airtable/auth/is-still-verified.js b/packages/backend/src/apps/airtable/auth/is-still-verified.js new file mode 100644 index 00000000..08962895 --- /dev/null +++ b/packages/backend/src/apps/airtable/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.id; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/airtable/auth/refresh-token.js b/packages/backend/src/apps/airtable/auth/refresh-token.js new file mode 100644 index 00000000..6f735500 --- /dev/null +++ b/packages/backend/src/apps/airtable/auth/refresh-token.js @@ -0,0 +1,40 @@ +import { URLSearchParams } from 'node:url'; + +import authScope from '../common/auth-scope.js'; + +const refreshToken = async ($) => { + const params = new URLSearchParams({ + client_id: $.auth.data.clientId, + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken, + }); + + const basicAuthToken = Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64'); + + const { data } = await $.http.post( + 'https://airtable.com/oauth2/v1/token', + params.toString(), + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + Authorization: `Basic ${basicAuthToken}`, + }, + additionalProperties: { + skipAddingAuthHeader: true, + }, + } + ); + + await $.auth.set({ + accessToken: data.access_token, + refreshToken: data.refresh_token, + expiresIn: data.expires_in, + refreshExpiresIn: data.refresh_expires_in, + scope: authScope.join(' '), + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/airtable/auth/verify-credentials.js b/packages/backend/src/apps/airtable/auth/verify-credentials.js new file mode 100644 index 00000000..d1b9fef7 --- /dev/null +++ b/packages/backend/src/apps/airtable/auth/verify-credentials.js @@ -0,0 +1,59 @@ +import getCurrentUser from '../common/get-current-user.js'; + +const verifyCredentials = async ($) => { + if ($.auth.data.originalState !== $.auth.data.state) { + throw new Error("The 'state' parameter does not match."); + } + if ($.auth.data.originalCodeChallenge !== $.auth.data.code_challenge) { + throw new Error("The 'code challenge' parameter does not match."); + } + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const basicAuthToken = Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64'); + + const { data } = await $.http.post( + 'https://airtable.com/oauth2/v1/token', + { + code: $.auth.data.code, + client_id: $.auth.data.clientId, + redirect_uri: redirectUri, + grant_type: 'authorization_code', + code_verifier: $.auth.data.codeVerifier, + }, + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + Authorization: `Basic ${basicAuthToken}`, + }, + additionalProperties: { + skipAddingAuthHeader: true, + }, + } + ); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + const currentUser = await getCurrentUser($); + const screenName = [currentUser.email, currentUser.id] + .filter(Boolean) + .join(' @ '); + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: $.auth.data.scope, + expiresIn: data.expires_in, + refreshExpiresIn: data.refresh_expires_in, + refreshToken: data.refresh_token, + screenName, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/airtable/common/add-auth-header.js b/packages/backend/src/apps/airtable/common/add-auth-header.js new file mode 100644 index 00000000..f957ebf9 --- /dev/null +++ b/packages/backend/src/apps/airtable/common/add-auth-header.js @@ -0,0 +1,12 @@ +const addAuthHeader = ($, requestConfig) => { + if ( + !requestConfig.additionalProperties?.skipAddingAuthHeader && + $.auth.data?.accessToken + ) { + requestConfig.headers.Authorization = `${$.auth.data.tokenType} ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/airtable/common/auth-scope.js b/packages/backend/src/apps/airtable/common/auth-scope.js new file mode 100644 index 00000000..8b4cbca8 --- /dev/null +++ b/packages/backend/src/apps/airtable/common/auth-scope.js @@ -0,0 +1,12 @@ +const authScope = [ + 'data.records:read', + 'data.records:write', + 'data.recordComments:read', + 'data.recordComments:write', + 'schema.bases:read', + 'schema.bases:write', + 'user.email:read', + 'webhook:manage', +]; + +export default authScope; diff --git a/packages/backend/src/apps/airtable/common/get-current-user.js b/packages/backend/src/apps/airtable/common/get-current-user.js new file mode 100644 index 00000000..c04f16a9 --- /dev/null +++ b/packages/backend/src/apps/airtable/common/get-current-user.js @@ -0,0 +1,6 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get('/v0/meta/whoami'); + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/airtable/index.js b/packages/backend/src/apps/airtable/index.js new file mode 100644 index 00000000..c658c6ab --- /dev/null +++ b/packages/backend/src/apps/airtable/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: 'Airtable', + key: 'airtable', + baseUrl: 'https://airtable.com', + apiBaseUrl: 'https://api.airtable.com', + iconUrl: '{BASE_URL}/apps/airtable/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/airtable/connection', + primaryColor: 'FFBF00', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 04d3fdcf..d5fee6f8 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -32,6 +32,12 @@ export default defineConfig({ ], sidebar: { '/apps/': [ + { + text: 'Airtable', + collapsible: true, + collapsed: true, + items: [{ text: 'Connection', link: '/apps/airtable/connection' }], + }, { text: 'Carbone', collapsible: true, @@ -305,7 +311,7 @@ export default defineConfig({ collapsed: true, items: [ { text: 'Actions', link: '/apps/removebg/actions' }, - { text: 'Connection', link: '/apps/removebg/connection' } + { text: 'Connection', link: '/apps/removebg/connection' }, ], }, { diff --git a/packages/docs/pages/apps/airtable/connection.md b/packages/docs/pages/apps/airtable/connection.md new file mode 100644 index 00000000..57bcd787 --- /dev/null +++ b/packages/docs/pages/apps/airtable/connection.md @@ -0,0 +1,19 @@ +# Airtable + +:::info +This page explains the steps you need to follow to set up the Airtable +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Login to your [Airtable account](https://www.airtable.com/). +2. Go to this [link](https://airtable.com/create/oauth) and click on the **Register new OAuth integration**. +3. Fill the name field. +4. Copy **OAuth Redirect URL** from Automatisch to **OAuth redirect URL** field. +5. Click on the **Register integration** button. +6. In **Developer Details** section, click on the **Generate client secret**. +7. Check the checkboxes of **Scopes** section. +8. Click on the **Save changes** button. +9. Copy **Client ID** to **Client ID** field on Automatisch. +10. Copy **Client secret** to **Client secret** field on Automatisch. +11. Click **Submit** button on Automatisch. +12. Congrats! Start using your new Airtable connection within the flows. diff --git a/packages/docs/pages/public/favicons/airtable.svg b/packages/docs/pages/public/favicons/airtable.svg new file mode 100644 index 00000000..867c3b5a --- /dev/null +++ b/packages/docs/pages/public/favicons/airtable.svg @@ -0,0 +1,9 @@ + + + + + + + + +