Skip to content

Commit dc34a3c

Browse files
committed
Avoid multiple readouts of OIDC hash.
1 parent 506c281 commit dc34a3c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/__test__/spec-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const polyfillWindow = () => {
1717
return this.href.replace(/^[^#]+/, '')
1818
},
1919
set hash(value) {
20-
this.href = this.href.replace(/#.*/, value)
20+
this.href = this.href.replace(/#.*|$/, value)
2121
}
2222
}
2323
})

src/webid-oidc.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,26 @@ export async function currentSession(
2929
storage: AsyncStorage = defaultStorage()
3030
): Promise<?webIdOidcSession> {
3131
try {
32+
// Obtain the Relying Party
3233
const rp = await getStoredRp(storage)
3334
if (!rp) {
3435
return null
3536
}
37+
38+
// Obtain and clear the OIDC URL fragment
3639
const url = currentUrl()
3740
if (!/#(.*&)?access_token=/.test(url)) {
3841
return null
3942
}
43+
window.location.hash = ''
44+
await restoreAppHashFragment(storage)
45+
46+
// Obtain a session from the Relying Party
4047
const storeData = await getData(storage)
4148
const session = await rp.validateResponse(url, storeData)
4249
if (!session) {
4350
return null
4451
}
45-
await restoreAppHashFragment(storage)
4652
return {
4753
...session,
4854
webId: session.idClaims.sub,
@@ -161,9 +167,8 @@ async function saveAppHashFragment(store: AsyncStorage): Promise<void> {
161167
}
162168

163169
async function restoreAppHashFragment(store: AsyncStorage): Promise<void> {
164-
await updateStorage(store, data => {
165-
window.location.hash = data.appHashFragment
166-
delete data.appHashFragment
170+
await updateStorage(store, ({ appHashFragment = '', ...data }) => {
171+
window.location.hash = appHashFragment
167172
return data
168173
})
169174
}

0 commit comments

Comments
 (0)