From bc67e9c4c436a992cb2dca818c135bafd72ffac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Mon, 20 May 2024 16:49:08 +0200 Subject: [PATCH] feat(firefly-iii): add firefly iii integration --- .../src/apps/firefly-iii/assets/favicon.svg | 21 +++++++ .../firefly-iii/auth/generate-auth-url.js | 20 +++++++ .../src/apps/firefly-iii/auth/index.js | 58 +++++++++++++++++++ .../firefly-iii/auth/is-still-verified.js | 8 +++ .../apps/firefly-iii/auth/refresh-token.js | 20 +++++++ .../firefly-iii/auth/verify-credentials.js | 35 +++++++++++ .../firefly-iii/common/add-auth-header.js | 9 +++ .../firefly-iii/common/get-current-user.js | 10 ++++ .../apps/firefly-iii/common/set-base-url.js | 10 ++++ .../backend/src/apps/firefly-iii/index.js | 17 ++++++ packages/docs/pages/.vitepress/config.js | 6 ++ .../docs/pages/apps/firefly-iii/connection.md | 17 ++++++ .../pages/public/favicons/firefly-iii.svg | 21 +++++++ 13 files changed, 252 insertions(+) create mode 100644 packages/backend/src/apps/firefly-iii/assets/favicon.svg create mode 100644 packages/backend/src/apps/firefly-iii/auth/generate-auth-url.js create mode 100644 packages/backend/src/apps/firefly-iii/auth/index.js create mode 100644 packages/backend/src/apps/firefly-iii/auth/is-still-verified.js create mode 100644 packages/backend/src/apps/firefly-iii/auth/refresh-token.js create mode 100644 packages/backend/src/apps/firefly-iii/auth/verify-credentials.js create mode 100644 packages/backend/src/apps/firefly-iii/common/add-auth-header.js create mode 100644 packages/backend/src/apps/firefly-iii/common/get-current-user.js create mode 100644 packages/backend/src/apps/firefly-iii/common/set-base-url.js create mode 100644 packages/backend/src/apps/firefly-iii/index.js create mode 100644 packages/docs/pages/apps/firefly-iii/connection.md create mode 100644 packages/docs/pages/public/favicons/firefly-iii.svg diff --git a/packages/backend/src/apps/firefly-iii/assets/favicon.svg b/packages/backend/src/apps/firefly-iii/assets/favicon.svg new file mode 100644 index 00000000..cd371be1 --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/assets/favicon.svg @@ -0,0 +1,21 @@ + + + \ No newline at end of file diff --git a/packages/backend/src/apps/firefly-iii/auth/generate-auth-url.js b/packages/backend/src/apps/firefly-iii/auth/generate-auth-url.js new file mode 100644 index 00000000..508ac8b8 --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/auth/generate-auth-url.js @@ -0,0 +1,20 @@ +import { URLSearchParams } from 'url'; + +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, + response_type: 'code', + }); + const instanceUrl = $.auth.data.instanceUrl; + + const url = `${instanceUrl}/oauth/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/firefly-iii/auth/index.js b/packages/backend/src/apps/firefly-iii/auth/index.js new file mode 100644 index 00000000..2d057837 --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/auth/index.js @@ -0,0 +1,58 @@ +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/firefly-iii/connections/add', + placeholder: null, + description: '', + clickToCopy: true, + }, + { + key: 'instanceUrl', + label: 'Instance URL', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: '', + 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/firefly-iii/auth/is-still-verified.js b/packages/backend/src/apps/firefly-iii/auth/is-still-verified.js new file mode 100644 index 00000000..d4124ca2 --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/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.attributes.email; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/firefly-iii/auth/refresh-token.js b/packages/backend/src/apps/firefly-iii/auth/refresh-token.js new file mode 100644 index 00000000..fba44a4d --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/auth/refresh-token.js @@ -0,0 +1,20 @@ +import { URLSearchParams } from 'node:url'; + +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(`/oauth/token`, params.toString()); + + 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/firefly-iii/auth/verify-credentials.js b/packages/backend/src/apps/firefly-iii/auth/verify-credentials.js new file mode 100644 index 00000000..8c3a28ac --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/auth/verify-credentials.js @@ -0,0 +1,35 @@ +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('/oauth/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($); + + 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: currentUser.attributes.email, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/firefly-iii/common/add-auth-header.js b/packages/backend/src/apps/firefly-iii/common/add-auth-header.js new file mode 100644 index 00000000..02477aa4 --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/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/firefly-iii/common/get-current-user.js b/packages/backend/src/apps/firefly-iii/common/get-current-user.js new file mode 100644 index 00000000..53e71562 --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/common/get-current-user.js @@ -0,0 +1,10 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get('/api/v1/about/user', { + Headers: { + Accept: 'application/vnd.api+json', + }, + }); + return currentUser.data; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/firefly-iii/common/set-base-url.js b/packages/backend/src/apps/firefly-iii/common/set-base-url.js new file mode 100644 index 00000000..def1330c --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/common/set-base-url.js @@ -0,0 +1,10 @@ +const setBaseUrl = ($, requestConfig) => { + const instanceUrl = $.auth.data.instanceUrl; + if (instanceUrl) { + requestConfig.baseURL = instanceUrl; + } + + return requestConfig; +}; + +export default setBaseUrl; diff --git a/packages/backend/src/apps/firefly-iii/index.js b/packages/backend/src/apps/firefly-iii/index.js new file mode 100644 index 00000000..e858ea0d --- /dev/null +++ b/packages/backend/src/apps/firefly-iii/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: 'Firefly III', + key: 'firefly-iii', + baseUrl: '', + apiBaseUrl: '', + iconUrl: '{BASE_URL}/apps/firefly-iii/assets/favicon.svg', + authDocUrl: '{DOCS_URL}/apps/firefly-iii/connection', + primaryColor: 'CD5029', + supportsConnections: true, + beforeRequest: [setBaseUrl, addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 219a1d51..6b1481d4 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -122,6 +122,12 @@ export default defineConfig({ { text: 'Connection', link: '/apps/filter/connection' }, ], }, + { + text: 'Firefly III', + collapsible: true, + collapsed: true, + items: [{ text: 'Connection', link: '/apps/firefly-iii/connection' }], + }, { text: 'Flickr', collapsible: true, diff --git a/packages/docs/pages/apps/firefly-iii/connection.md b/packages/docs/pages/apps/firefly-iii/connection.md new file mode 100644 index 00000000..4b1c938d --- /dev/null +++ b/packages/docs/pages/apps/firefly-iii/connection.md @@ -0,0 +1,17 @@ +# Firefly III + +:::info +This page explains the steps you need to follow to set up the Firefly III +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to your dashboard. +2. Click on the **Options** tab on the left, and click on the **Profile** button. +3. Go to **Oauth** tab and click on the **Create New Client** button. +4. Enter a name for your project. +5. Copy **OAuth Redirect URL** from Automatisch to **Redirect URL** field, and click on the **Create** button. +6. Copy the **Client ID** value from the following popup to the `Client ID` field on Automatisch. +7. Copy the **Secret** value from the following popup to the `Client Secret` field on Automatisch. +8. Fill **Instance URL** with your Firefly III instance url. +9. Click **Submit** button on Automatisch. +10. Congrats! Start using your new Firefly III connection within the flows. diff --git a/packages/docs/pages/public/favicons/firefly-iii.svg b/packages/docs/pages/public/favicons/firefly-iii.svg new file mode 100644 index 00000000..cd371be1 --- /dev/null +++ b/packages/docs/pages/public/favicons/firefly-iii.svg @@ -0,0 +1,21 @@ + + + \ No newline at end of file