Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,39 @@ export async function handleOIDCCallback(errorCallback) {
}
}

// The code is single use, so the exchange must not run twice.
let oidcCompleteStarted = false

export async function handleOIDCComplete(errorCallback) {
if (oidcCompleteStarted) {
return
}
oidcCompleteStarted = true
try {
const resp = await fetch(`${__APIHOST__}/api/oidc/tokens/`, {
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
},
})
const code = new URLSearchParams(window.location.hash.slice(1)).get('code')

// Remove the code from the address bar before anything can read it back.
if (code) {
window.history.replaceState(null, '', window.location.pathname)
}

Comment thread
DavidMStraub marked this conversation as resolved.
const resp = code
? await fetch(`${__APIHOST__}/api/oidc/tokens/`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({code}),
})
: // Older API versions return the tokens from cookies, which this GET clears.
await fetch(`${__APIHOST__}/api/oidc/tokens/`, {
method: 'GET',
credentials: 'include',
headers: {
Accept: 'application/json',
},
})

if (!resp.ok) {
const errorText = await resp.text()
Expand Down