Add Bearer token auth support to sendNtfy
Enhanced the sendNtfy function to support Bearer token authentication in addition to Basic auth. Now, URLs in the format token@host/topic will use Bearer tokens, improving flexibility for different notification server setups.
This commit is contained in:
1 parent
133c9f1e8f
commit
ef26d38fce
1 file changed
+20
-8
@@ -289,14 +289,26 @@ async function sendNtfy(appriseUrl: string, payload: NotificationPayload): Promi
|
|||||||
const path = appriseUrl.replace(/^ntfys?:\/\//, '');
|
const path = appriseUrl.replace(/^ntfys?:\/\//, '');
|
||||||
|
|
||||||
let url: string;
|
let url: string;
|
||||||
let auth: string | null = null;
|
let authHeader: string | null = null;
|
||||||
|
|
||||||
// Check for user:pass@host/topic format
|
// Check for user:pass@host/topic format (Basic auth)
|
||||||
const authMatch = path.match(/^([^:]+):([^@]+)@(.+)$/);
|
const basicMatch = path.match(/^([^:]+):([^@]+)@(.+)$/);
|
||||||
if (authMatch) {
|
if (basicMatch) {
|
||||||
const [, user, pass, hostAndTopic] = authMatch;
|
const [, user, pass, hostAndTopic] = basicMatch;
|
||||||
auth = Buffer.from(`${user}:${pass}`).toString('base64');
|
const basic = Buffer.from(`${user}:${pass}`).toString('base64');
|
||||||
|
authHeader = `Basic ${basic}`;
|
||||||
url = `${isSecure ? 'https' : 'http'}://${hostAndTopic}`;
|
url = `${isSecure ? 'https' : 'http'}://${hostAndTopic}`;
|
||||||
|
} else if (path.includes('@') && path.includes('/')) {
|
||||||
|
// token@host/topic -> Bearer token auth
|
||||||
|
const tokenMatch = path.match(/^([^@]+)@(.+)$/);
|
||||||
|
if (tokenMatch) {
|
||||||
|
const [, token, hostAndTopic] = tokenMatch;
|
||||||
|
authHeader = `Bearer ${token}`;
|
||||||
|
url = `${isSecure ? 'https' : 'http'}://${hostAndTopic}`;
|
||||||
|
} else {
|
||||||
|
// Fallback to custom server without auth
|
||||||
|
url = `${isSecure ? 'https' : 'http'}://${path}`;
|
||||||
|
}
|
||||||
} else if (path.includes('/')) {
|
} else if (path.includes('/')) {
|
||||||
// Custom server without auth
|
// Custom server without auth
|
||||||
url = `${isSecure ? 'https' : 'http'}://${path}`;
|
url = `${isSecure ? 'https' : 'http'}://${path}`;
|
||||||
@@ -311,8 +323,8 @@ async function sendNtfy(appriseUrl: string, payload: NotificationPayload): Promi
|
|||||||
'Tags': payload.type || 'info'
|
'Tags': payload.type || 'info'
|
||||||
};
|
};
|
||||||
|
|
||||||
if (auth) {
|
if (authHeader) {
|
||||||
headers['Authorization'] = `Basic ${auth}`;
|
headers['Authorization'] = authHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in new issue
Block a user