diff --git a/packages/backend/src/apps/you-need-a-budget/assets/favicon.svg b/packages/backend/src/apps/you-need-a-budget/assets/favicon.svg new file mode 100644 index 00000000..c83333c8 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/assets/favicon.svg @@ -0,0 +1,25 @@ + + + + My Budget + + \ No newline at end of file diff --git a/packages/backend/src/apps/you-need-a-budget/auth/generate-auth-url.js b/packages/backend/src/apps/you-need-a-budget/auth/generate-auth-url.js new file mode 100644 index 00000000..12d647e1 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/generate-auth-url.js @@ -0,0 +1,22 @@ +import { URLSearchParams } from 'url'; + +export default async function generateAuthUrl($) { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const state = Math.random().toString(); + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId, + redirect_uri: redirectUri, + response_type: 'code', + state, + }); + + const url = `https://app.ynab.com/oauth/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + originalState: state, + }); +} diff --git a/packages/backend/src/apps/you-need-a-budget/auth/index.js b/packages/backend/src/apps/you-need-a-budget/auth/index.js new file mode 100644 index 00000000..32eef53e --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/index.js @@ -0,0 +1,60 @@ +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/you-need-a-budget/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in You Need A Budget, enter the URL above.', + clickToCopy: true, + }, + { + key: 'screenName', + label: 'Screen Name', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: + 'Screen name of your connection to be used on Automatisch UI.', + clickToCopy: false, + }, + { + 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/you-need-a-budget/auth/is-still-verified.js b/packages/backend/src/apps/you-need-a-budget/auth/is-still-verified.js new file mode 100644 index 00000000..6d792b12 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/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; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/you-need-a-budget/auth/refresh-token.js b/packages/backend/src/apps/you-need-a-budget/auth/refresh-token.js new file mode 100644 index 00000000..9c7830e4 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/refresh-token.js @@ -0,0 +1,25 @@ +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( + 'https://app.ynab.com/oauth/token', + params.toString() + ); + + await $.auth.set({ + accessToken: data.access_token, + refreshToken: data.refresh_token, + expiresIn: data.expires_in, + scope: data.scope, + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/you-need-a-budget/auth/verify-credentials.js b/packages/backend/src/apps/you-need-a-budget/auth/verify-credentials.js new file mode 100644 index 00000000..19d88fa3 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/auth/verify-credentials.js @@ -0,0 +1,33 @@ +const verifyCredentials = async ($) => { + if ($.auth.data.originalState !== $.auth.data.state) { + throw new Error(`The 'state' parameter does not match.`); + } + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const { data } = await $.http.post('https://app.ynab.com/oauth/token', { + client_id: $.auth.data.clientId, + client_secret: $.auth.data.clientSecret, + redirect_uri: redirectUri, + grant_type: 'authorization_code', + code: $.auth.data.code, + }); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: data.scope, + createdAt: data.created_at, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + screenName: $.auth.data.screenName, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/you-need-a-budget/common/add-auth-header.js b/packages/backend/src/apps/you-need-a-budget/common/add-auth-header.js new file mode 100644 index 00000000..02477aa4 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/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/you-need-a-budget/common/get-current-user.js b/packages/backend/src/apps/you-need-a-budget/common/get-current-user.js new file mode 100644 index 00000000..65dea829 --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/common/get-current-user.js @@ -0,0 +1,6 @@ +const getCurrentUser = async ($) => { + const { data: currentUser } = await $.http.get('/user'); + return currentUser.data.user.id; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/you-need-a-budget/index.js b/packages/backend/src/apps/you-need-a-budget/index.js new file mode 100644 index 00000000..62aad3fb --- /dev/null +++ b/packages/backend/src/apps/you-need-a-budget/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: 'You Need A Budget', + key: 'you-need-a-budget', + baseUrl: 'https://app.ynab.com', + apiBaseUrl: 'https://api.ynab.com/v1', + iconUrl: '{BASE_URL}/apps/you-need-a-budget/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/you-need-a-budget/connection', + primaryColor: '19223C', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 04d3fdcf..d877a163 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -305,7 +305,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' }, ], }, { @@ -475,6 +475,14 @@ export default defineConfig({ { text: 'Connection', link: '/apps/xero/connection' }, ], }, + { + text: 'You Need A Budget', + collapsible: true, + collapsed: true, + items: [ + { text: 'Connection', link: '/apps/you-need-a-budget/connection' }, + ], + }, { text: 'Youtube', collapsible: true, diff --git a/packages/docs/pages/apps/you-need-a-budget/connection.md b/packages/docs/pages/apps/you-need-a-budget/connection.md new file mode 100644 index 00000000..21f35a41 --- /dev/null +++ b/packages/docs/pages/apps/you-need-a-budget/connection.md @@ -0,0 +1,18 @@ +# You Need A Budget + +:::info +This page explains the steps you need to follow to set up the You Need A Budget +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to the [link](https://www.ynab.com/) and login your account. +2. Click on the account name in the top left and go to **Account Settings**. +3. Click on the **Developer Settings**. +4. Click on the **New Application** under the **OAuth Applications** section. +5. Fill the new application form. +6. Copy **OAuth Redirect URL** from Automatisch and paste it in **Redirect URI(s)** section. +7. Click on the **Save Application** button. +8. Copy the **Client ID** value to the **Client ID** field on Automatisch. +9. Copy the **Client Secret** value to the **Client Secret** field on Automatisch. +10. Fill the **Screen Name** field on Automatisch. +11. Congrats! Start using your new You Need A Budget connection within the flows. diff --git a/packages/docs/pages/public/favicons/you-need-a-budget.svg b/packages/docs/pages/public/favicons/you-need-a-budget.svg new file mode 100644 index 00000000..c83333c8 --- /dev/null +++ b/packages/docs/pages/public/favicons/you-need-a-budget.svg @@ -0,0 +1,25 @@ + + + + My Budget + + \ No newline at end of file