Skip to content

Commit 0e2e201

Browse files
Implement currentUser() and tests
1 parent 884aacd commit 0e2e201

3 files changed

Lines changed: 99 additions & 7 deletions

File tree

demo/index.html

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,37 @@ <h5>App body</h5>
6262
// (equivalent to jQuery's $(document).ready())
6363
// Trigger a login() check on page load, in case user is logged in already
6464
document.addEventListener('DOMContentLoaded', function () {
65-
SolidClient.auth.login()
66-
.then(function (webId, accessToken) {
67-
loginSuccess(webId, accessToken)
65+
window.addEventListener('message', onMessage)
66+
init()
67+
SolidClient.auth.currentUser()
68+
.then(function (webId) {
69+
loginSuccess(webId)
70+
})
71+
})
72+
73+
function onMessage (event) {
74+
console.log('Auth client received event: ', event)
75+
if (!event || !event.data) { return }
76+
switch (event.data.event_type) {
77+
case 'providerSelected':
78+
console.log('Provider selected: ', event.data.value)
79+
loginTo(event.data.value)
80+
break
81+
default:
82+
console.error('onMessage - unknown event type: ', event)
83+
break
84+
}
85+
}
86+
87+
function loginTo (providerUri) {
88+
SolidClient.auth.login(providerUri)
89+
.then(function (webId) {
90+
loginSuccess(webId)
6891
})
6992
.catch(function (error) {
7093
console.error('Error logging in:', error)
7194
})
72-
init()
73-
})
95+
}
7496

7597
function init () {
7698
initButton('getRootAcl', getRootAcl)

src/index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class ClientAuthOIDC {
4242
* @param [options.localStorage=localStorage] Optionally inject localStorage
4343
*/
4444
constructor (options = {}) {
45-
this.window = options.window || window
46-
this.localStorage = options.localStorage || localStorage
45+
this.window = options.window || global.window
46+
this.localStorage = options.localStorage || global.localStorage
4747
this.currentClient = null
4848
this.providerUri = null
4949
this.webId = null
@@ -65,6 +65,19 @@ class ClientAuthOIDC {
6565
return window.location.href
6666
}
6767

68+
currentUser () {
69+
if (this.webId) {
70+
return Promise.resolve(this.webId)
71+
}
72+
// Attempt to find a provider based on the 'state' param of the current URI
73+
let providerUri = this.providerFromCurrentUri()
74+
if (providerUri) {
75+
return this.login(providerUri)
76+
} else {
77+
return Promise.resolve(null)
78+
}
79+
}
80+
6881
/**
6982
* Extracts and returns the `state` query or hash fragment param from a uri
7083
* @param uri {string}
@@ -102,11 +115,14 @@ class ClientAuthOIDC {
102115
* @return {Promise<RelyingParty>}
103116
*/
104117
loadOrRegisterClient (providerUri) {
118+
this.currentClient = null
105119
return this.loadClient(providerUri)
106120
.then(loadedClient => {
107121
if (loadedClient) {
122+
this.currentClient = loadedClient
108123
return loadedClient
109124
} else {
125+
this.currentClient = null
110126
return this.registerClient(providerUri)
111127
}
112128
})
@@ -159,6 +175,7 @@ class ClientAuthOIDC {
159175
* @return {Promise<string>} Resolves to the logged in user's WebID URI
160176
*/
161177
login (providerUri) {
178+
this.clearCurrentUser()
162179
let selectProvider = this.selectProvider.bind(this)
163180
let loadOrRegisterClient = this.loadOrRegisterClient.bind(this)
164181
let validateOrSendAuthRequest = this.validateOrSendAuthRequest.bind(this)
@@ -169,6 +186,12 @@ class ClientAuthOIDC {
169186
.then(validateOrSendAuthRequest)
170187
}
171188

189+
clearCurrentUser () {
190+
this.webId = null
191+
this.accessToken = null
192+
this.idToken = null
193+
}
194+
172195
/**
173196
* Resolves to the URI of an OIDC identity provider, from one of the following:
174197
*
@@ -313,6 +336,7 @@ class ClientAuthOIDC {
313336
*/
314337
extractAndValidateWebId (idToken) {
315338
let webId = idToken.payload.sub
339+
this.webId = webId
316340
return webId
317341
}
318342

test/unit/auth-test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,52 @@ describe('SolidAuthOIDC', () => {
337337
expect(auth.window.location).to.equal(authUri)
338338
done()
339339
})
340+
.catch(err => { console.error(err.message) })
341+
})
342+
})
343+
344+
describe('currentUser()', () => {
345+
it('should return cached webId if present', done => {
346+
let aliceWebId = 'https://alice.example.com'
347+
let auth = new SolidAuthOIDC()
348+
auth.webId = aliceWebId
349+
350+
auth.currentUser()
351+
.then(webId => {
352+
expect(webId).to.equal(aliceWebId)
353+
done()
354+
})
355+
.catch(err => { console.error(err.message) })
356+
})
357+
358+
it('should return null if no cached webId and no current state param', done => {
359+
let auth = new SolidAuthOIDC({ window: { location: {} } })
360+
auth.currentUser()
361+
.then(webId => {
362+
expect(webId).to.not.exist
363+
done()
364+
})
365+
.catch(err => { console.error(err.message) })
366+
})
367+
368+
it('should automatically login if current uri has state param', done => {
369+
let state = 'abcd'
370+
let providerUri = 'https://provider.example.com'
371+
let auth = new SolidAuthOIDC({ window: { location: {} } })
372+
auth.saveProviderByState(state, providerUri)
373+
374+
auth.window.location.href = `https://client-app.example.com#state=${state}`
375+
let aliceWebId = 'https://alice.example.com/'
376+
let loginStub = sinon.stub().returns(Promise.resolve(aliceWebId))
377+
auth.login = loginStub
378+
379+
auth.currentUser()
380+
.then(webId => {
381+
expect(webId).to.equal(aliceWebId)
382+
expect(loginStub).to.have.been.calledWith(providerUri)
383+
done()
384+
})
385+
.catch(err => { console.error(err.message) })
340386
})
341387
})
342388
})

0 commit comments

Comments
 (0)