MCPcopy Create free account
hub / github.com/isomorphic-git/isomorphic-git / extractAuthFromUrl

Function extractAuthFromUrl

src/utils/extractAuthFromUrl.js:1–16  ·  view source on GitHub ↗
(url)

Source from the content-addressed store, hash-verified

1export 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}

Callers 2

discoverMethod · 0.90
connectMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…