From 354b331b080cc6eb0e796f58897cf1704db5f726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C4=B1dvan=20Akca?= Date: Fri, 10 Nov 2023 12:47:28 +0300 Subject: [PATCH] feat(reddit): add reddit integration --- .../src/apps/reddit/assets/favicon.svg | 1 + .../src/apps/reddit/auth/generate-auth-url.ts | 26 ++++++++++ .../backend/src/apps/reddit/auth/index.ts | 48 +++++++++++++++++++ .../src/apps/reddit/auth/is-still-verified.ts | 9 ++++ .../src/apps/reddit/auth/refresh-token.ts | 29 +++++++++++ .../apps/reddit/auth/verify-credentials.ts | 48 +++++++++++++++++++ .../src/apps/reddit/common/add-auth-header.ts | 11 +++++ .../src/apps/reddit/common/auth-scope.ts | 3 ++ .../apps/reddit/common/get-current-user.ts | 8 ++++ packages/backend/src/apps/reddit/index.d.ts | 0 packages/backend/src/apps/reddit/index.ts | 16 +++++++ packages/docs/pages/.vitepress/config.js | 6 +++ packages/docs/pages/apps/reddit/connection.md | 15 ++++++ .../docs/pages/public/favicons/reddit.svg | 1 + 14 files changed, 221 insertions(+) create mode 100644 packages/backend/src/apps/reddit/assets/favicon.svg create mode 100644 packages/backend/src/apps/reddit/auth/generate-auth-url.ts create mode 100644 packages/backend/src/apps/reddit/auth/index.ts create mode 100644 packages/backend/src/apps/reddit/auth/is-still-verified.ts create mode 100644 packages/backend/src/apps/reddit/auth/refresh-token.ts create mode 100644 packages/backend/src/apps/reddit/auth/verify-credentials.ts create mode 100644 packages/backend/src/apps/reddit/common/add-auth-header.ts create mode 100644 packages/backend/src/apps/reddit/common/auth-scope.ts create mode 100644 packages/backend/src/apps/reddit/common/get-current-user.ts create mode 100644 packages/backend/src/apps/reddit/index.d.ts create mode 100644 packages/backend/src/apps/reddit/index.ts create mode 100644 packages/docs/pages/apps/reddit/connection.md create mode 100644 packages/docs/pages/public/favicons/reddit.svg diff --git a/packages/backend/src/apps/reddit/assets/favicon.svg b/packages/backend/src/apps/reddit/assets/favicon.svg new file mode 100644 index 00000000..e41ae322 --- /dev/null +++ b/packages/backend/src/apps/reddit/assets/favicon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/packages/backend/src/apps/reddit/auth/generate-auth-url.ts b/packages/backend/src/apps/reddit/auth/generate-auth-url.ts new file mode 100644 index 00000000..b24fb404 --- /dev/null +++ b/packages/backend/src/apps/reddit/auth/generate-auth-url.ts @@ -0,0 +1,26 @@ +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 state = Math.random().toString() as string; + const searchParams = new URLSearchParams({ + client_id: $.auth.data.clientId as string, + response_type: 'code', + redirect_uri: redirectUri, + duration: 'permanent', + scope: authScope.join(' '), + state, + }); + + const url = `https://www.reddit.com/api/v1/authorize?${searchParams.toString()}`; + + await $.auth.set({ + url, + originalState: state, + }); +} diff --git a/packages/backend/src/apps/reddit/auth/index.ts b/packages/backend/src/apps/reddit/auth/index.ts new file mode 100644 index 00000000..c98d7565 --- /dev/null +++ b/packages/backend/src/apps/reddit/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/reddit/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Reddit, 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/reddit/auth/is-still-verified.ts b/packages/backend/src/apps/reddit/auth/is-still-verified.ts new file mode 100644 index 00000000..50107400 --- /dev/null +++ b/packages/backend/src/apps/reddit/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.id; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/reddit/auth/refresh-token.ts b/packages/backend/src/apps/reddit/auth/refresh-token.ts new file mode 100644 index 00000000..c88fdfff --- /dev/null +++ b/packages/backend/src/apps/reddit/auth/refresh-token.ts @@ -0,0 +1,29 @@ +import { URLSearchParams } from 'node:url'; +import { IGlobalVariable } from '@automatisch/types'; + +const refreshToken = async ($: IGlobalVariable) => { + const headers = { + Authorization: `Basic ${Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64')}`, + }; + const params = new URLSearchParams({ + grant_type: 'refresh_token', + refresh_token: $.auth.data.refreshToken as string, + }); + + const { data } = await $.http.post( + 'https://www.reddit.com/api/v1/access_token', + params.toString(), + { headers } + ); + + 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/reddit/auth/verify-credentials.ts b/packages/backend/src/apps/reddit/auth/verify-credentials.ts new file mode 100644 index 00000000..dd44de54 --- /dev/null +++ b/packages/backend/src/apps/reddit/auth/verify-credentials.ts @@ -0,0 +1,48 @@ +import { IField, IGlobalVariable } from '@automatisch/types'; +import getCurrentUser from '../common/get-current-user'; +import { URLSearchParams } from 'url'; + +const verifyCredentials = async ($: IGlobalVariable) => { + if ($.auth.data.originalState !== $.auth.data.state) { + throw new Error(`The 'state' parameter does not match.`); + } + const oauthRedirectUrlField = $.app.auth.fields.find( + (field: IField) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value as string; + const headers = { + Authorization: `Basic ${Buffer.from( + $.auth.data.clientId + ':' + $.auth.data.clientSecret + ).toString('base64')}`, + }; + const params = new URLSearchParams({ + grant_type: 'authorization_code', + code: $.auth.data.code as string, + redirect_uri: redirectUri, + }); + + const { data } = await $.http.post( + 'https://www.reddit.com/api/v1/access_token', + params.toString(), + { headers } + ); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + }); + + const currentUser = await getCurrentUser($); + const screenName = currentUser?.name; + + await $.auth.set({ + clientId: $.auth.data.clientId, + clientSecret: $.auth.data.clientSecret, + scope: $.auth.data.scope, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + screenName, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/reddit/common/add-auth-header.ts b/packages/backend/src/apps/reddit/common/add-auth-header.ts new file mode 100644 index 00000000..8e7798b8 --- /dev/null +++ b/packages/backend/src/apps/reddit/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/reddit/common/auth-scope.ts b/packages/backend/src/apps/reddit/common/auth-scope.ts new file mode 100644 index 00000000..d1010b4a --- /dev/null +++ b/packages/backend/src/apps/reddit/common/auth-scope.ts @@ -0,0 +1,3 @@ +const authScope: string[] = ['identity', 'read', 'account', 'submit']; + +export default authScope; diff --git a/packages/backend/src/apps/reddit/common/get-current-user.ts b/packages/backend/src/apps/reddit/common/get-current-user.ts new file mode 100644 index 00000000..05fe143a --- /dev/null +++ b/packages/backend/src/apps/reddit/common/get-current-user.ts @@ -0,0 +1,8 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const getCurrentUser = async ($: IGlobalVariable) => { + const { data: currentUser } = await $.http.get('/api/v1/me'); + return currentUser; +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/reddit/index.d.ts b/packages/backend/src/apps/reddit/index.d.ts new file mode 100644 index 00000000..e69de29b diff --git a/packages/backend/src/apps/reddit/index.ts b/packages/backend/src/apps/reddit/index.ts new file mode 100644 index 00000000..df965e87 --- /dev/null +++ b/packages/backend/src/apps/reddit/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: 'Reddit', + key: 'reddit', + baseUrl: 'https://www.reddit.com', + apiBaseUrl: 'https://oauth.reddit.com', + iconUrl: '{BASE_URL}/apps/reddit/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/reddit/connection', + primaryColor: 'FF4500', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 3c94db3a..a497e096 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -289,6 +289,12 @@ export default defineConfig({ { text: 'Connection', link: '/apps/pushover/connection' }, ], }, + { + text: 'Reddit', + collapsible: true, + collapsed: true, + items: [{ text: 'Connection', link: '/apps/reddit/connection' }], + }, { text: 'Remove.bg', collapsible: true, diff --git a/packages/docs/pages/apps/reddit/connection.md b/packages/docs/pages/apps/reddit/connection.md new file mode 100644 index 00000000..3cebb999 --- /dev/null +++ b/packages/docs/pages/apps/reddit/connection.md @@ -0,0 +1,15 @@ +# Reddit + +:::info +This page explains the steps you need to follow to set up the Reddit +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Go to [Reddit apps page](https://www.reddit.com/prefs/apps). +2. Click on the **"are you a developer? create an app..."** button in order to create an app. +3. Fill the **Name** field and choose **web app**. +4. Copy **OAuth Redirect URL** from Automatisch to **redirect uri** field. +5. Click on the **create app** button. +6. Copy the client id below **web app** text to the `Client ID` field on Automatisch. +7. Copy the **secret** value to the `Client Secret` field on Automatisch. +8. Start using Reddit integration with Automatisch! diff --git a/packages/docs/pages/public/favicons/reddit.svg b/packages/docs/pages/public/favicons/reddit.svg new file mode 100644 index 00000000..e41ae322 --- /dev/null +++ b/packages/docs/pages/public/favicons/reddit.svg @@ -0,0 +1 @@ + \ No newline at end of file