| 1 | export function extractAuthFromUrl(url) { |
| 2 | // For whatever reason, the `fetch` API does not convert credentials embedded in the URL |
| 3 | // into Basic Authentication headers automatically. Instead it throws an error! |
| 4 | // So we must manually parse the URL, rip out the user:password portion if it is present |
| 5 | // and compute the Authorization header. |
| 6 | // Note: I tried using new URL(url) but that throws a security exception in Edge. :rolleyes: |
| 7 | let userpass = url.match(/^https?:\/\/([^/]+)@/) |
| 8 | // No credentials, return the url unmodified and an empty auth object |
| 9 | if (userpass == null) return { url, auth: {} } |
| 10 | userpass = userpass[1] |
| 11 | const [username, password] = userpass.split(':') |
| 12 | // Remove credentials from URL |
| 13 | url = url.replace(`${userpass}@`, '') |
| 14 | // Has credentials, return the fetch-safe URL and the parsed credentials |
| 15 | return { url, auth: { username, password } } |
| 16 | } |