From 3fc7fce9ca95b85a0fbe261baa9cffe168edf137 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 3 Jan 2023 19:06:04 +0100 Subject: [PATCH] feat(google-forms): add authentication --- .../google-forms/auth/generate-auth-url.ts | 24 ++++++++ .../src/apps/google-forms/auth/index.ts | 51 +++++++++++++++++ .../google-forms/auth/is-still-verified.ts | 9 +++ .../apps/google-forms/auth/refresh-token.ts | 26 +++++++++ .../google-forms/auth/verify-credentials.ts | 57 +++++++++++++++++++ .../google-forms/common/add-auth-header.ts | 11 ++++ .../apps/google-forms/common/auth-scope.ts | 8 +++ .../google-forms/common/get-current-user.ts | 10 ++++ .../backend/src/apps/google-forms/index.ts | 4 ++ 9 files changed, 200 insertions(+) create mode 100644 packages/backend/src/apps/google-forms/auth/generate-auth-url.ts create mode 100644 packages/backend/src/apps/google-forms/auth/index.ts create mode 100644 packages/backend/src/apps/google-forms/auth/is-still-verified.ts create mode 100644 packages/backend/src/apps/google-forms/auth/refresh-token.ts create mode 100644 packages/backend/src/apps/google-forms/auth/verify-credentials.ts create mode 100644 packages/backend/src/apps/google-forms/common/add-auth-header.ts create mode 100644 packages/backend/src/apps/google-forms/common/auth-scope.ts create mode 100644 packages/backend/src/apps/google-forms/common/get-current-user.ts diff --git a/packages/backend/src/apps/google-forms/auth/generate-auth-url.ts b/packages/backend/src/apps/google-forms/auth/generate-auth-url.ts new file mode 100644 index 00000000..ea89f8a4 --- /dev/null +++ b/packages/backend/src/apps/google-forms/auth/generate-auth-url.ts @@ -0,0 +1,24 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import { URLSearchParams } from 'url'; +import authScope from '../common/auth-scope'; + +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({ + client_id: $.auth.data.clientId as string, + redirect_uri: redirectUri, + prompt: 'select_account', + scope: authScope.join(' '), + response_type: 'code', + access_type: 'offline', + }); + + const url = `https://accounts.google.com/o/oauth2/v2/auth?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/google-forms/auth/index.ts b/packages/backend/src/apps/google-forms/auth/index.ts new file mode 100644 index 00000000..90d9686e --- /dev/null +++ b/packages/backend/src/apps/google-forms/auth/index.ts @@ -0,0 +1,51 @@ +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/google-forms/connections/add', + placeholder: null, + description: + 'When asked to input an OAuth callback or redirect URL in Github OAuth, enter the URL above.', + docUrl: 'https://automatisch.io/docs/github#oauth-redirect-url', + clickToCopy: true, + }, + { + key: 'clientId', + label: 'Client ID', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + docUrl: 'https://automatisch.io/docs/google-forms#client-id', + clickToCopy: false, + }, + { + key: 'clientSecret', + label: 'Client Secret', + type: 'string' as const, + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + docUrl: 'https://automatisch.io/docs/google-forms#client-secret', + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, + refreshToken, +}; diff --git a/packages/backend/src/apps/google-forms/auth/is-still-verified.ts b/packages/backend/src/apps/google-forms/auth/is-still-verified.ts new file mode 100644 index 00000000..c46fa18b --- /dev/null +++ b/packages/backend/src/apps/google-forms/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.resourceName; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/google-forms/auth/refresh-token.ts b/packages/backend/src/apps/google-forms/auth/refresh-token.ts new file mode 100644 index 00000000..17d0c4cc --- /dev/null +++ b/packages/backend/src/apps/google-forms/auth/refresh-token.ts @@ -0,0 +1,26 @@ +import { URLSearchParams } from 'node:url'; +import { IGlobalVariable } from '@automatisch/types'; +import authScope from '../common/auth-scope'; + +const refreshToken = async ($: IGlobalVariable) => { + const params = new URLSearchParams({ + client_id: $.auth.data.clientId as string, + client_secret: $.auth.data.clientSecret as string, + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken as string, + }); + + const { data } = await $.http.post( + 'https://oauth2.googleapis.com/token', + params.toString() + ); + + await $.auth.set({ + accessToken: data.access_token, + expiresIn: data.expires_in, + scope: authScope.join(' '), + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/google-forms/auth/verify-credentials.ts b/packages/backend/src/apps/google-forms/auth/verify-credentials.ts new file mode 100644 index 00000000..124e73c1 --- /dev/null +++ b/packages/backend/src/apps/google-forms/auth/verify-credentials.ts @@ -0,0 +1,57 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; + +type TUser = { + displayName: string; + metadata: { + primary: boolean; + }; +}; + +type TEmailAddress = { + value: string; + metadata: { + primary: boolean; + }; +}; + +const verifyCredentials = async ($: IGlobalVariable) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const { data } = await $.http.post(`https://oauth2.googleapis.com/token`, { + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + code: $.auth.data.code, + grant_type: 'authorization_code', + redirect_uri: redirectUri, + }); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + const currentUser = await getCurrentUser($); + + const { displayName } = currentUser.names.find( + (name: TUser) => name.metadata.primary + ); + const { value: email } = currentUser.emailAddresses.find( + (emailAddress: TEmailAddress) => emailAddress.metadata.primary + ); + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: $.auth.data.scope, + idToken: data.id_token, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + resourceName: currentUser.resourceName, + screenName: `${displayName} - ${email}`, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/google-forms/common/add-auth-header.ts b/packages/backend/src/apps/google-forms/common/add-auth-header.ts new file mode 100644 index 00000000..12bb4a62 --- /dev/null +++ b/packages/backend/src/apps/google-forms/common/add-auth-header.ts @@ -0,0 +1,11 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if (requestConfig.headers && $.auth.data?.accessToken) { + requestConfig.headers.Authorization = `${$.auth.data.tokenType} ${$.auth.data.accessToken}`; + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/google-forms/common/auth-scope.ts b/packages/backend/src/apps/google-forms/common/auth-scope.ts new file mode 100644 index 00000000..dd65c6bd --- /dev/null +++ b/packages/backend/src/apps/google-forms/common/auth-scope.ts @@ -0,0 +1,8 @@ +const authScope: string[] = [ + 'https://www.googleapis.com/auth/forms.body.readonly', + 'https://www.googleapis.com/auth/drive.readonly', + 'https://www.googleapis.com/auth/userinfo.email', + 'profile', +]; + +export default authScope; diff --git a/packages/backend/src/apps/google-forms/common/get-current-user.ts b/packages/backend/src/apps/google-forms/common/get-current-user.ts new file mode 100644 index 00000000..724fe1ac --- /dev/null +++ b/packages/backend/src/apps/google-forms/common/get-current-user.ts @@ -0,0 +1,10 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const getCurrentUser = async ($: IGlobalVariable) => { + const { data: currentUser } = await $.http.get( + 'https://people.googleapis.com/v1/people/me?personFields=names,emailAddresses' + ); + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/google-forms/index.ts b/packages/backend/src/apps/google-forms/index.ts index e1fdf797..c4246c6f 100644 --- a/packages/backend/src/apps/google-forms/index.ts +++ b/packages/backend/src/apps/google-forms/index.ts @@ -1,4 +1,6 @@ import defineApp from '../../helpers/define-app'; +import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; export default defineApp({ name: 'Google Forms', @@ -9,4 +11,6 @@ export default defineApp({ authDocUrl: 'https://automatisch.io/docs/apps/google-forms/connection', primaryColor: '673AB7', supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, });