diff --git a/packages/backend/src/apps/ntfy/auth/index.ts b/packages/backend/src/apps/ntfy/auth/index.ts new file mode 100644 index 00000000..5a96d5bc --- /dev/null +++ b/packages/backend/src/apps/ntfy/auth/index.ts @@ -0,0 +1,39 @@ +import verifyCredentials from './verify-credentials'; +import isStillVerified from './is-still-verified'; + +export default { + fields: [ + { + key: 'serverUrl', + label: 'Server URL', + type: 'string' as const, + required: true, + readOnly: false, + value: 'https://ntfy.sh', + placeholder: null, + description: 'ntfy server to use.', + clickToCopy: false, + }, + { + key: 'username', + label: 'Username', + type: 'string' as const, + required: false, + readOnly: false, + placeholder: null, + clickToCopy: false, + }, + { + key: 'password', + label: 'Password', + type: 'string' as const, + required: false, + readOnly: false, + placeholder: null, + clickToCopy: false, + }, + ], + + verifyCredentials, + isStillVerified, +}; diff --git a/packages/backend/src/apps/ntfy/auth/is-still-verified.ts b/packages/backend/src/apps/ntfy/auth/is-still-verified.ts new file mode 100644 index 00000000..66bb963e --- /dev/null +++ b/packages/backend/src/apps/ntfy/auth/is-still-verified.ts @@ -0,0 +1,9 @@ +import { IGlobalVariable } from '@automatisch/types'; +import verifyCredentials from './verify-credentials'; + +const isStillVerified = async ($: IGlobalVariable) => { + await verifyCredentials($); + return true; +}; + +export default isStillVerified; diff --git a/packages/backend/src/apps/ntfy/auth/verify-credentials.ts b/packages/backend/src/apps/ntfy/auth/verify-credentials.ts new file mode 100644 index 00000000..84d1d623 --- /dev/null +++ b/packages/backend/src/apps/ntfy/auth/verify-credentials.ts @@ -0,0 +1,16 @@ +import { IGlobalVariable } from '@automatisch/types'; + +const verifyCredentials = async ($: IGlobalVariable) => { + await $.http.post('/', { topic: 'automatisch' }); + let screenName = $.auth.data.serverUrl; + + if ($.auth.data.username) { + screenName = `${$.auth.data.username} @ ${screenName}` + } + + await $.auth.set({ + screenName, + }); +}; + +export default verifyCredentials; diff --git a/packages/backend/src/apps/ntfy/common/add-auth-header.ts b/packages/backend/src/apps/ntfy/common/add-auth-header.ts new file mode 100644 index 00000000..ff194483 --- /dev/null +++ b/packages/backend/src/apps/ntfy/common/add-auth-header.ts @@ -0,0 +1,18 @@ +import { TBeforeRequest } from '@automatisch/types'; + +const addAuthHeader: TBeforeRequest = ($, requestConfig) => { + if ($.auth.data.apiBaseUrl) { + requestConfig.baseURL = $.auth.data.apiBaseUrl as string; + } + + if ($.auth.data?.username && $.auth.data?.password) { + requestConfig.auth = { + username: $.auth.data.username as string, + password: $.auth.data.password as string, + } + } + + return requestConfig; +}; + +export default addAuthHeader; diff --git a/packages/backend/src/apps/ntfy/index.ts b/packages/backend/src/apps/ntfy/index.ts index b8318283..c1cddab4 100644 --- a/packages/backend/src/apps/ntfy/index.ts +++ b/packages/backend/src/apps/ntfy/index.ts @@ -1,5 +1,6 @@ import defineApp from '../../helpers/define-app'; import addAuthHeader from './common/add-auth-header'; +import auth from './auth'; export default defineApp({ name: 'Ntfy', @@ -10,4 +11,6 @@ export default defineApp({ baseUrl: 'https://ntfy.sh', apiBaseUrl: 'https://ntfy.sh', primaryColor: '56bda8', + beforeRequest: [addAuthHeader], + auth, });