diff --git a/packages/backend/src/apps/firebase/assets/favicon.svg b/packages/backend/src/apps/firebase/assets/favicon.svg new file mode 100644 index 00000000..f396a8fd --- /dev/null +++ b/packages/backend/src/apps/firebase/assets/favicon.svg @@ -0,0 +1,52 @@ + + + + logo_lockup_firebase_vertical + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/backend/src/apps/firebase/auth/generate-auth-url.js b/packages/backend/src/apps/firebase/auth/generate-auth-url.js new file mode 100644 index 00000000..c972ae16 --- /dev/null +++ b/packages/backend/src/apps/firebase/auth/generate-auth-url.js @@ -0,0 +1,23 @@ +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 searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId, + 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/firebase/auth/index.js b/packages/backend/src/apps/firebase/auth/index.js new file mode 100644 index 00000000..88453ad5 --- /dev/null +++ b/packages/backend/src/apps/firebase/auth/index.js @@ -0,0 +1,71 @@ +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/firebase/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Google Cloud, 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, + }, + { + key: 'projectId', + label: 'Project ID', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: 'The project id of your Firebase project', + clickToCopy: false, + }, + { + key: 'realtimeDatabaseId', + label: 'Realtime Database Domain', + type: 'string', + required: false, + readOnly: false, + value: null, + placeholder: null, + description: + 'If you want to use Realtime Database, please provide the domain of your Realtime Database (https://{{domain}}.firebaseio.com)', + clickToCopy: false, + }, + ], + + generateAuthUrl, + verifyCredentials, + isStillVerified, + refreshToken, +}; diff --git a/packages/backend/src/apps/firebase/auth/is-still-verified.js b/packages/backend/src/apps/firebase/auth/is-still-verified.js new file mode 100644 index 00000000..68f4d7db --- /dev/null +++ b/packages/backend/src/apps/firebase/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.resourceName; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/firebase/auth/refresh-token.js b/packages/backend/src/apps/firebase/auth/refresh-token.js new file mode 100644 index 00000000..2c137caa --- /dev/null +++ b/packages/backend/src/apps/firebase/auth/refresh-token.js @@ -0,0 +1,31 @@ +import { URLSearchParams } from 'node:url'; + +import authScope from '../common/auth-scope.js'; + +const refreshToken = async ($) => { + const params = new URLSearchParams({ + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken, + }); + + const { data } = await $.http.post( + 'https://oauth2.googleapis.com/token', + params.toString(), + { + additionalProperties: { + skipAddingAuthHeader: true, + }, + } + ); + + 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/firebase/auth/verify-credentials.js b/packages/backend/src/apps/firebase/auth/verify-credentials.js new file mode 100644 index 00000000..84f992dd --- /dev/null +++ b/packages/backend/src/apps/firebase/auth/verify-credentials.js @@ -0,0 +1,50 @@ +import getCurrentUser from '../common/get-current-user.js'; + +const verifyCredentials = async ($) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + 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, + }, + { + additionalProperties: { + skipAddingAuthHeader: true, + }, + } + ); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + const currentUser = await getCurrentUser($); + + const { displayName } = currentUser.names.find( + (name) => name.metadata.primary + ); + const { value: email } = currentUser.emailAddresses.find( + (emailAddress) => 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/firebase/common/add-auth-header.js b/packages/backend/src/apps/firebase/common/add-auth-header.js new file mode 100644 index 00000000..02477aa4 --- /dev/null +++ b/packages/backend/src/apps/firebase/common/add-auth-header.js @@ -0,0 +1,9 @@ +const addAuthHeader = ($, 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/firebase/common/auth-scope.js b/packages/backend/src/apps/firebase/common/auth-scope.js new file mode 100644 index 00000000..c238db2f --- /dev/null +++ b/packages/backend/src/apps/firebase/common/auth-scope.js @@ -0,0 +1,9 @@ +const authScope = [ + 'https://www.googleapis.com/auth/datastore', + 'https://www.googleapis.com/auth/firebase.database', + 'https://www.googleapis.com/auth/datastore', + 'https://www.googleapis.com/auth/userinfo.email', + 'https://www.googleapis.com/auth/userinfo.profile', +]; + +export default authScope; diff --git a/packages/backend/src/apps/firebase/common/get-current-user.js b/packages/backend/src/apps/firebase/common/get-current-user.js new file mode 100644 index 00000000..d9d801f8 --- /dev/null +++ b/packages/backend/src/apps/firebase/common/get-current-user.js @@ -0,0 +1,13 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get( + 'https://people.googleapis.com/v1/people/me?personFields=names,emailAddresses', + { + additionalProperties: { + skipAddingAuthHeader: true, + }, + } + ); + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/firebase/common/set-base-url.js b/packages/backend/src/apps/firebase/common/set-base-url.js new file mode 100644 index 00000000..e2200a80 --- /dev/null +++ b/packages/backend/src/apps/firebase/common/set-base-url.js @@ -0,0 +1,16 @@ +const setBaseUrl = ($, requestConfig) => { + const realtimeDatabaseId = $.auth.data.realtimeDatabaseId; + + if (requestConfig.additionalProperties?.skipAddingAuthHeader) + return requestConfig; + + if (requestConfig.additionalProperties?.setFirestoreBaseUrl) { + requestConfig.baseURL = 'https://firestore.googleapis.com'; + } else { + requestConfig.baseURL = `https://${realtimeDatabaseId}.firebaseio.com`; + } + + return requestConfig; +}; + +export default setBaseUrl; diff --git a/packages/backend/src/apps/firebase/index.js b/packages/backend/src/apps/firebase/index.js new file mode 100644 index 00000000..1f159526 --- /dev/null +++ b/packages/backend/src/apps/firebase/index.js @@ -0,0 +1,17 @@ +import defineApp from '../../helpers/define-app.js'; +import addAuthHeader from './common/add-auth-header.js'; +import auth from './auth/index.js'; +import setBaseUrl from './common/set-base-url.js'; + +export default defineApp({ + name: 'Firebase', + key: 'firebase', + baseUrl: 'https://firebase.google.com', + apiBaseUrl: '', + iconUrl: '{BASE_URL}/apps/firebase/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/firebase/connection', + primaryColor: 'FFA000', + supportsConnections: true, + beforeRequest: [setBaseUrl, addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 6e9d171f..4fa96b2e 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -95,6 +95,12 @@ export default defineConfig({ { text: 'Connection', link: '/apps/filter/connection' }, ], }, + { + text: 'Firebase', + collapsible: true, + collapsed: true, + items: [{ text: 'Connection', link: '/apps/firebase/connection' }], + }, { text: 'Flickr', collapsible: true, diff --git a/packages/docs/pages/apps/firebase/connection.md b/packages/docs/pages/apps/firebase/connection.md new file mode 100644 index 00000000..4252d8c1 --- /dev/null +++ b/packages/docs/pages/apps/firebase/connection.md @@ -0,0 +1,31 @@ +# Firebase + +:::info +This page explains the steps you need to follow to set up the Firebase +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to the [Google Cloud Console](https://console.cloud.google.com) to create a project. +2. Click on the project drop-down menu at the top of the page, and click on the **New Project** button. +3. Enter a name for your project and click on the **Create** button. +4. Go to [API Library](https://console.cloud.google.com/apis/library) in Google Cloud console. +5. Search for **Cloud Firestore API** in the search bar and click on it. +6. Click on the **Enable** button to enable the API. +7. Repeat steps 5 and 6 for the **Firebase Realtime Database API and People API** +8. Go to [OAuth consent screen](https://console.cloud.google.com/apis/credentials/consent) in Google Cloud console. +9. Select **External** here for starting your app in testing mode at first. Click on the **Create** button. +10. Fill **App Name**, **User Support Email**, and **Developer Contact Information**. Click on the **Save and Continue** button. +11. Skip adding or removing scopes and click on the **Save and Continue** button. +12. Click on the **Add Users** button and add a test email because only test users can access the app while publishing status is set to "Testing". +13. Click on the **Save and Continue** button and now you have configured the consent screen. +14. Go to [Credentials](https://console.cloud.google.com/apis/credentials) in Google Cloud console. +15. Click on the **Create Credentials** button and select the **OAuth client ID** option. +16. Select the application type as **Web application** and fill the **Name** field. +17. Copy **OAuth Redirect URL** from Automatisch to **Authorized redirect URIs** field, and click on the **Create** button. +18. Copy the **Your Client ID** value from the following popup to the `Client ID` field on Automatisch. +19. Copy the **Your Client Secret** value from the following popup to the `Client Secret` field on Automatisch. +20. Login to your [Firebase account](https://firebase.google.com/) and go to your project (please create a new project if you don't already have one). +21. Click on the gear icon next to **Project Overview** and go to **Project settings**. +22. Copy the **Project ID** value to the `Project ID` field on Automatisch. +23. Click **Submit** button on Automatisch. +24. Congrats! Start using your new Firebase connection within the flows. diff --git a/packages/docs/pages/public/favicons/firebase.svg b/packages/docs/pages/public/favicons/firebase.svg new file mode 100644 index 00000000..f396a8fd --- /dev/null +++ b/packages/docs/pages/public/favicons/firebase.svg @@ -0,0 +1,52 @@ + + + + logo_lockup_firebase_vertical + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file