@@ -23,10 +23,24 @@ const NtfyAPI = {
2323 * @param {string } accessToken - Bearer token for authentication
2424 * @returns {Headers }
2525 */
26- buildAuthHeaders ( accessToken ) {
26+ buildAuthHeaders ( accessToken , apiUrl ) {
2727 const headers = new Headers ( ) ;
2828 if ( accessToken ) {
2929 headers . set ( 'Authorization' , `Bearer ${ accessToken } ` ) ;
30+ } else if ( apiUrl ) {
31+ try {
32+ const url = new URL ( apiUrl ) ;
33+ if ( url . username || url . password ) {
34+ const username = decodeURIComponent ( url . username ) ;
35+ const password = decodeURIComponent ( url . password ) ;
36+ const credentials = `${ username } :${ password } ` ;
37+ // Use robust base64 encoding that supports UTF-8
38+ const auth = btoa ( unescape ( encodeURIComponent ( credentials ) ) ) ;
39+ headers . set ( 'Authorization' , `Basic ${ auth } ` ) ;
40+ }
41+ } catch ( e ) {
42+ // Invoke error handling or logging if necessary, but silent failure relies on no auth
43+ }
3044 }
3145 return headers ;
3246 } ,
@@ -83,7 +97,7 @@ const NtfyAPI = {
8397 */
8498 async sendNotification ( config , topic , { message, title, priority, tags } ) {
8599 const fullUrl = this . buildTopicUrl ( config . apiUrl , topic ) ;
86- const headers = this . buildAuthHeaders ( config . accessToken ) ;
100+ const headers = this . buildAuthHeaders ( config . accessToken , config . apiUrl ) ;
87101
88102 if ( title ) {
89103 headers . set ( 'X-Title' , this . encodeHeaderValue ( title ) ) ;
@@ -123,7 +137,7 @@ const NtfyAPI = {
123137 */
124138 async sendAttachment ( config , topic , { data, filename, message, title, priority, tags } ) {
125139 const fullUrl = this . buildTopicUrl ( config . apiUrl , topic ) ;
126- const headers = this . buildAuthHeaders ( config . accessToken ) ;
140+ const headers = this . buildAuthHeaders ( config . accessToken , config . apiUrl ) ;
127141
128142 headers . set ( 'X-Filename' , this . encodeHeaderValue ( filename ) ) ;
129143
0 commit comments