diff --git a/packages/backend/src/apps/miro/assets/favicon.svg b/packages/backend/src/apps/miro/assets/favicon.svg new file mode 100644 index 00000000..b87ea9a1 --- /dev/null +++ b/packages/backend/src/apps/miro/assets/favicon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/backend/src/apps/miro/auth/generate-auth-url.ts b/packages/backend/src/apps/miro/auth/generate-auth-url.ts new file mode 100644 index 00000000..0b84ef03 --- /dev/null +++ b/packages/backend/src/apps/miro/auth/generate-auth-url.ts @@ -0,0 +1,20 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; + +export default async function generateAuthUrl($: IGlobalVariable) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const searchParams = new URLSearchParams({ + response_type: 'code', + client_id: $.auth.data.clientId as string, + redirect_uri: redirectUri, + }); + + const url = `https://miro.com/oauth/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/miro/auth/index.ts b/packages/backend/src/apps/miro/auth/index.ts new file mode 100644 index 00000000..6193716a --- /dev/null +++ b/packages/backend/src/apps/miro/auth/index.ts @@ -0,0 +1,48 @@ +import generateAuthUrl from './generate-auth-url'; +import verifyCredentials from './verify-credentials'; +import refreshToken from './refresh-token'; +import isStillVerified from './is-still-verified'; + +export default { + fields: [ + { + key: 'oAuthRedirectUrl', + label: 'OAuth Redirect URL', + type: 'string' as const, + required: true, + readOnly: true, + value: '{WEB_APP_URL}/app/miro/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Miro, enter the URL above.', + clickToCopy: true, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + 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, + refreshToken, +}; diff --git a/packages/backend/src/apps/miro/auth/is-still-verified.ts b/packages/backend/src/apps/miro/auth/is-still-verified.ts new file mode 100644 index 00000000..93a01099 --- /dev/null +++ b/packages/backend/src/apps/miro/auth/is-still-verified.ts @@ -0,0 +1,9 @@ +import { IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; + +const isStillVerified = async ($: IGlobalVariable) => { + const currentUser = await getCurrentUser($); + return !!currentUser; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/miro/auth/refresh-token.ts b/packages/backend/src/apps/miro/auth/refresh-token.ts new file mode 100644 index 00000000..03228573 --- /dev/null +++ b/packages/backend/src/apps/miro/auth/refresh-token.ts @@ -0,0 +1,22 @@ +import { URLSearchParams } from 'node:url'; +import { IGlobalVariable } from '@automatisch/types'; + +const refreshToken = async ($: IGlobalVariable) => { + const params = new URLSearchParams({ + grant_type: 'refresh_token', + client_id: $.auth.data.clientId as string, + client_secret: $.auth.data.clientSecret as string, + refresh_token: $.auth.data.refreshToken as string, + }); + + const { data } = await $.http.post('/v1/oauth/token', params.toString()); + + await $.auth.set({ + accessToken: data.access_token, + expiresIn: data.expires_in, + scope: data.scope, + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/miro/auth/verify-credentials.ts b/packages/backend/src/apps/miro/auth/verify-credentials.ts new file mode 100644 index 00000000..9fe3915e --- /dev/null +++ b/packages/backend/src/apps/miro/auth/verify-credentials.ts @@ -0,0 +1,40 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; + +const verifyCredentials = async ($: IGlobalVariable) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const params = { + grant_type: 'authorization_code', + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + code: $.auth.data.code, + redirect_uri: redirectUri, + }; + + const { data } = await $.http.post(`/v1/oauth/token`, null, { + params, + }); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + const currentUser = await getCurrentUser($); + + await $.auth.set({ + userId: data.user_id, + refreshToken: data.refresh_token, + expiresIn: data.expires_in, + teamId: data.team_id, + scope: data.scope, + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + screenName: currentUser.name, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/miro/common/add-auth-header.ts b/packages/backend/src/apps/miro/common/add-auth-header.ts new file mode 100644 index 00000000..8e7798b8 --- /dev/null +++ b/packages/backend/src/apps/miro/common/add-auth-header.ts @@ -0,0 +1,11 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, 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/miro/common/get-current-user.ts b/packages/backend/src/apps/miro/common/get-current-user.ts new file mode 100644 index 00000000..913da3a6 --- /dev/null +++ b/packages/backend/src/apps/miro/common/get-current-user.ts @@ -0,0 +1,10 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const getCurrentUser = async ($: IGlobalVariable) => { + const { data } = await $.http.get( + `https://api.miro.com/v1/oauth-token?access_token=${$.auth.data.accessToken}` + ); + return data.user; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/miro/index.d.ts b/packages/backend/src/apps/miro/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/backend/src/apps/miro/index.ts b/packages/backend/src/apps/miro/index.ts new file mode 100644 index 00000000..f66e808f --- /dev/null +++ b/packages/backend/src/apps/miro/index.ts @@ -0,0 +1,16 @@ +import defineApp from '../../helpers/define-app'; +import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; + +export default defineApp({ + name: 'Miro', + key: 'miro', + baseUrl: 'https://miro.com', + apiBaseUrl: 'https://api.miro.com', + iconUrl: '{BASE_URL}/apps/miro/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/miro/connection', + primaryColor: 'F2CA02', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +});