diff --git a/packages/backend/src/apps/disqus/assets/favicon.svg b/packages/backend/src/apps/disqus/assets/favicon.svg new file mode 100644 index 00000000..66ffeb34 --- /dev/null +++ b/packages/backend/src/apps/disqus/assets/favicon.svg @@ -0,0 +1,16 @@ + + + + + + + + + + + diff --git a/packages/backend/src/apps/disqus/auth/generate-auth-url.js b/packages/backend/src/apps/disqus/auth/generate-auth-url.js new file mode 100644 index 00000000..f94a5257 --- /dev/null +++ b/packages/backend/src/apps/disqus/auth/generate-auth-url.js @@ -0,0 +1,21 @@ +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.apiKey, + scope: authScope.join(','), + response_type: 'code', + redirect_uri: redirectUri, + }); + + const url = `https://disqus.com/api/oauth/2.0/authorize/?${searchParams.toString()}`; + + await $.auth.set({ + url, + }); +} diff --git a/packages/backend/src/apps/disqus/auth/index.js b/packages/backend/src/apps/disqus/auth/index.js new file mode 100644 index 00000000..fb84d805 --- /dev/null +++ b/packages/backend/src/apps/disqus/auth/index.js @@ -0,0 +1,48 @@ +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/disqus/connections/add', + placeholder: null, + description: + 'When asked to input a redirect URL in Disqus, enter the URL above.', + clickToCopy: true, + }, + { + key: 'apiKey', + label: 'API Key', + type: 'string', + required: true, + readOnly: false, + value: null, + placeholder: null, + description: null, + clickToCopy: false, + }, + { + key: 'apiSecret', + label: 'API 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/disqus/auth/is-still-verified.js b/packages/backend/src/apps/disqus/auth/is-still-verified.js new file mode 100644 index 00000000..c42b4a96 --- /dev/null +++ b/packages/backend/src/apps/disqus/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.response.username; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/disqus/auth/refresh-token.js b/packages/backend/src/apps/disqus/auth/refresh-token.js new file mode 100644 index 00000000..c813898a --- /dev/null +++ b/packages/backend/src/apps/disqus/auth/refresh-token.js @@ -0,0 +1,26 @@ +import { URLSearchParams } from 'node:url'; +import authScope from '../common/auth-scope.js'; + +const refreshToken = async ($) => { + const params = new URLSearchParams({ + grant_type: 'refresh_token', + client_id: $.auth.data.apiKey, + client_secret: $.auth.data.apiSecret, + refresh_token: $.auth.data.refreshToken, + }); + + const { data } = await $.http.post( + `https://disqus.com/api/oauth/2.0/access_token/`, + params.toString() + ); + + await $.auth.set({ + accessToken: data.access_token, + refreshToken: data.refresh_token, + expiresIn: data.expires_in, + scope: authScope.join(','), + tokenType: data.token_type, + }); +}; + +export default refreshToken; diff --git a/packages/backend/src/apps/disqus/auth/verify-credentials.js b/packages/backend/src/apps/disqus/auth/verify-credentials.js new file mode 100644 index 00000000..a3c3eb3d --- /dev/null +++ b/packages/backend/src/apps/disqus/auth/verify-credentials.js @@ -0,0 +1,34 @@ +import { URLSearchParams } from 'url'; + +const verifyCredentials = async ($) => { + const oauthRedirectUrlField = $.app.auth.fields.find( + (field) => field.key == 'oAuthRedirectUrl' + ); + const redirectUri = oauthRedirectUrlField.value; + const params = new URLSearchParams({ + grant_type: 'authorization_code', + client_id: $.auth.data.apiKey, + client_secret: $.auth.data.apiSecret, + redirect_uri: redirectUri, + code: $.auth.data.code, + }); + + const { data } = await $.http.post( + `https://disqus.com/api/oauth/2.0/access_token/`, + params.toString() + ); + + await $.auth.set({ + accessToken: data.access_token, + tokenType: data.token_type, + apiKey: $.auth.data.apiKey, + apiSecret: $.auth.data.apiSecret, + scope: $.auth.data.scope, + userId: data.user_id, + expiresIn: data.expires_in, + refreshToken: data.refresh_token, + screenName: data.username, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/disqus/common/add-auth-header.js b/packages/backend/src/apps/disqus/common/add-auth-header.js new file mode 100644 index 00000000..45e5c683 --- /dev/null +++ b/packages/backend/src/apps/disqus/common/add-auth-header.js @@ -0,0 +1,15 @@ +import { URLSearchParams } from 'url'; + +const addAuthHeader = ($, requestConfig) => { + const params = new URLSearchParams({ + access_token: $.auth.data.accessToken, + api_key: $.auth.data.apiKey, + api_secret: $.auth.data.apiSecret, + }); + + requestConfig.params = params; + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/disqus/common/auth-scope.js b/packages/backend/src/apps/disqus/common/auth-scope.js new file mode 100644 index 00000000..97f3eb8d --- /dev/null +++ b/packages/backend/src/apps/disqus/common/auth-scope.js @@ -0,0 +1,3 @@ +const authScope = ['read', 'write', 'admin', 'email']; + +export default authScope; diff --git a/packages/backend/src/apps/disqus/common/get-current-user.js b/packages/backend/src/apps/disqus/common/get-current-user.js new file mode 100644 index 00000000..63b0c78a --- /dev/null +++ b/packages/backend/src/apps/disqus/common/get-current-user.js @@ -0,0 +1,10 @@ +const getCurrentUser = async ($) => { + try { + const { data: currentUser } = await $.http.get('/3.0/users/details.json'); + return currentUser; + } catch (error) { + throw new Error('You are not authenticated.'); + } +}; + +export default getCurrentUser; diff --git a/packages/backend/src/apps/disqus/index.js b/packages/backend/src/apps/disqus/index.js new file mode 100644 index 00000000..8f3e31cc --- /dev/null +++ b/packages/backend/src/apps/disqus/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: 'Disqus', + key: 'disqus', + baseUrl: 'https://disqus.com', + apiBaseUrl: 'https://disqus.com/api', + iconUrl: '{BASE_URL}/apps/disqus/assets/favicon.svg', + authDocUrl: 'https://automatisch.io/docs/apps/disqus/connection', + primaryColor: '2E9FFF', + supportsConnections: true, + beforeRequest: [addAuthHeader], + auth, +}); diff --git a/packages/docs/pages/.vitepress/config.js b/packages/docs/pages/.vitepress/config.js index 04d3fdcf..2e79323b 100644 --- a/packages/docs/pages/.vitepress/config.js +++ b/packages/docs/pages/.vitepress/config.js @@ -68,6 +68,12 @@ export default defineConfig({ { text: 'Connection', link: '/apps/discord/connection' }, ], }, + { + text: 'Disqus', + collapsible: true, + collapsed: true, + items: [{ text: 'Connection', link: '/apps/disqus/connection' }], + }, { text: 'Dropbox', collapsible: true, @@ -305,7 +311,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' }, ], }, { diff --git a/packages/docs/pages/apps/disqus/connection.md b/packages/docs/pages/apps/disqus/connection.md new file mode 100644 index 00000000..b03539cb --- /dev/null +++ b/packages/docs/pages/apps/disqus/connection.md @@ -0,0 +1,19 @@ +# Disqus + +:::info +This page explains the steps you need to follow to set up the Disqus +connection in Automatisch. If any of the steps are outdated, please let us know! +::: + +1. Login to the [Disqus](https://disqus.com/). +2. Go to the [API applications page](https://disqus.com/api/applications/) and click on the **Register new application** button. +3. Fill the **Register Application** form fields. +4. Click on the **Register my application** button. +5. Go to the **Authentication** section and select **Read, Write, and Manage Forums** option. +6. Copy **OAuth Redirect URL** from Automatisch to **Callback URL** field. +7. Click on the **Save Changes** button. +8. Go to the **Details** tab. +9. Copy **API Key** to **API Key** field on Automatisch. +10. Copy **API Secret** to **API Secret** field on Automatisch. +11. Click **Submit** button on Automatisch. +12. Congrats! Start using your new Disqus connection within the flows. diff --git a/packages/docs/pages/public/favicons/disqus.svg b/packages/docs/pages/public/favicons/disqus.svg new file mode 100644 index 00000000..66ffeb34 --- /dev/null +++ b/packages/docs/pages/public/favicons/disqus.svg @@ -0,0 +1,16 @@ + + + + + + + + + + +