Skip to content

Commit 3f3e715

Browse files
Merge pull request #6 from solid/local-storage-refactor
Clean up/refactor global localStorage usages
2 parents f985e38 + f0b5dc8 commit 3f3e715

File tree

3 files changed

+24
-85
lines changed

3 files changed

+24
-85
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"chai": "^3.5.0",
6767
"chai-as-promised": "^6.0.0",
6868
"dirty-chai": "^1.2.2",
69-
"localstorage-polyfill": "^1.0.1",
69+
"localstorage-memory": "^1.0.2",
7070
"mocha": "^3.3.0",
7171
"nyc": "^10.2.0",
7272
"sinon": "^2.1.0",

src/index.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class ClientAuthOIDC {
4040
* @constructor
4141
* @param [options={}]
4242
* @param [options.window=Window] Optionally inject global browser window
43-
* @param [options.localStorage=localStorage] Optionally inject localStorage
43+
* @param [options.store=localStorage] Optionally inject localStorage
4444
*/
4545
constructor (options = {}) {
4646
this.window = options.window || global.window
47-
this.localStorage = options.localStorage || global.localStorage
47+
this.store = options.store || global.localStorage
4848

4949
this.currentClient = null
5050
this.providerUri = null
@@ -180,7 +180,7 @@ class ClientAuthOIDC {
180180

181181
// Check for client config stored locally
182182
let key = this.keyByProvider(providerUri)
183-
let clientConfig = localStorage.getItem(key)
183+
let clientConfig = this.store.getItem(key)
184184

185185
if (clientConfig) {
186186
clientConfig = JSON.parse(clientConfig)
@@ -191,13 +191,13 @@ class ClientAuthOIDC {
191191
}
192192

193193
/**
194-
* Loads a provider's URI from localStorage, given a `state` uri param.
194+
* Loads a provider's URI from store, given a `state` uri param.
195195
* @param state {string}
196196
* @return {string}
197197
*/
198198
loadProvider (state) {
199199
let key = this.keyByState(state)
200-
let providerUri = localStorage.getItem(key)
200+
let providerUri = this.store.getItem(key)
201201
return providerUri
202202
}
203203

@@ -355,7 +355,7 @@ class ClientAuthOIDC {
355355
let options = {}
356356
let providerUri = client.provider.url
357357

358-
return client.createRequest(options, this.localStorage)
358+
return client.createRequest(options, this.store)
359359
.then(authUri => {
360360
let state = this.extractState(authUri, QUERY)
361361
if (!state) {
@@ -399,7 +399,7 @@ class ClientAuthOIDC {
399399
* @returns {Promise<string>} Current user's web id
400400
*/
401401
initUserFromResponse (client) {
402-
return client.validateResponse(this.currentLocation(), this.localStorage)
402+
return client.validateResponse(this.currentLocation(), this.store)
403403
.then(response => {
404404
this.idToken = response.params.id_token
405405
this.accessToken = response.params.access_token
@@ -466,7 +466,6 @@ class ClientAuthOIDC {
466466
* @param [options={}]
467467
* @param [options.redirectUri] {string} Defaults to window.location.href
468468
* @param [options.scope='openid profile'] {string}
469-
* @param [options.store=localStorage]
470469
* @throws {TypeError} If providerUri is missing
471470
* @return {Promise<RelyingParty>} Registered RelyingParty client instance
472471
*/
@@ -484,7 +483,6 @@ class ClientAuthOIDC {
484483
* @param [options={}]
485484
* @param [options.redirectUri] {string} Defaults to window.location.href
486485
* @param [options.scope='openid profile'] {string}
487-
* @param [options.store=localStorage]
488486
* @throws {TypeError} If providerUri is missing
489487
* @return {Promise<RelyingParty>} Registered RelyingParty client instance
490488
*/
@@ -509,7 +507,7 @@ class ClientAuthOIDC {
509507
response_type: 'id_token token'
510508
}
511509
},
512-
store: options.store || localStorage
510+
store: this.store
513511
}
514512
return RelyingParty
515513
.register(providerUri, registration, rpOptions)
@@ -541,17 +539,17 @@ class ClientAuthOIDC {
541539
throw new Error('Cannot save providerUri - state not provided')
542540
}
543541
let key = this.keyByState(state)
544-
localStorage.setItem(key, providerUri)
542+
this.store.setItem(key, providerUri)
545543
}
546544

547545
/**
548-
* Stores a RelyingParty client for a given provider in localStorage.
546+
* Stores a RelyingParty client for a given provider in the local store.
549547
* @param client {RelyingParty}
550548
* @param providerUri {string}
551549
*/
552550
storeClient (client, providerUri) {
553551
this.currentClient = client
554-
localStorage.setItem(this.keyByProvider(providerUri), client.serialize())
552+
this.store.setItem(this.keyByProvider(providerUri), client.serialize())
555553
}
556554
}
557555

test/auth.test.js

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

3-
require('localstorage-polyfill') // exports 'localStorage' global
43
global.URL = require('url').URL
54
global.URLSearchParams = require('url').URLSearchParams
65

6+
const localStorage = require('localstorage-memory')
7+
78
const chai = require('chai')
89
const sinon = require('sinon')
910
const sinonChai = require('sinon-chai')
@@ -18,18 +19,15 @@ const expect = chai.expect
1819
const SolidAuthOIDC = require('../src/index')
1920

2021
describe('SolidAuthOIDC', () => {
22+
var auth
23+
const providerUri = 'https://provider.example.com'
24+
2125
beforeEach(() => {
2226
localStorage.clear()
27+
auth = new SolidAuthOIDC({ window: { location: {} }, store: localStorage })
2328
})
2429

2530
describe('login()', () => {
26-
let auth, providerUri
27-
28-
beforeEach(() => {
29-
auth = new SolidAuthOIDC()
30-
providerUri = 'https://provider.example.com'
31-
})
32-
3331
it('should invoke selectProvider() if provider uri is not given', () => {
3432
let selectProvider = sinon.stub(auth, 'selectProvider').resolves(null)
3533

@@ -74,12 +72,6 @@ describe('SolidAuthOIDC', () => {
7472
})
7573

7674
describe('logout()', () => {
77-
let auth
78-
79-
beforeEach(() => {
80-
auth = new SolidAuthOIDC()
81-
})
82-
8375
it('should clear the current user', () => {
8476
let clearCurrentUser = sinon.spy(auth, 'clearCurrentUser')
8577

@@ -129,11 +121,6 @@ describe('SolidAuthOIDC', () => {
129121
})
130122

131123
describe('providerFromCurrentUri()', () => {
132-
var auth
133-
beforeEach(() => {
134-
auth = new SolidAuthOIDC({ window: { location: {} } })
135-
})
136-
137124
it('should return null when no state param present', () => {
138125
auth.window.location.href = 'https://client-app.example.com'
139126
let providerUri = auth.providerFromCurrentUri()
@@ -163,8 +150,6 @@ describe('SolidAuthOIDC', () => {
163150

164151
describe('provider persistence', () => {
165152
it('should store and load provider uri, by state', () => {
166-
let auth = new SolidAuthOIDC()
167-
let providerUri = 'https://provider.example.com'
168153
let state = 'abcd'
169154
// Check to see that provider doesn't exist initially
170155
expect(auth.loadProvider(state)).to.not.exist()
@@ -178,12 +163,6 @@ describe('SolidAuthOIDC', () => {
178163
})
179164

180165
describe('extractState()', () => {
181-
var auth
182-
183-
beforeEach(() => {
184-
auth = new SolidAuthOIDC()
185-
})
186-
187166
it('should return null when no uri is provided', () => {
188167
let state = auth.extractState()
189168

@@ -223,15 +202,10 @@ describe('SolidAuthOIDC', () => {
223202

224203
describe('selectProvider()', () => {
225204
it('should pass through a given providerUri', () => {
226-
let auth = new SolidAuthOIDC()
227-
let providerUri = 'https://provider.example.com'
228-
229205
expect(auth.selectProvider(providerUri)).to.eventually.equal(providerUri)
230206
})
231207

232208
it('should derive a provider from the current uri', () => {
233-
let auth = new SolidAuthOIDC()
234-
let providerUri = 'https://provider.example.com'
235209
auth.providerFromCurrentUri = sinon.stub().returns(providerUri)
236210

237211
return auth.selectProvider()
@@ -242,8 +216,6 @@ describe('SolidAuthOIDC', () => {
242216
})
243217

244218
it('should obtain provider from UI, if not present or cached', () => {
245-
let auth = new SolidAuthOIDC()
246-
let providerUri = 'https://provider.example.com'
247219
auth.providerFromCurrentUri = sinon.stub().returns(null)
248220
auth.providerFromUI = sinon.stub().resolves(providerUri)
249221

@@ -256,17 +228,11 @@ describe('SolidAuthOIDC', () => {
256228
})
257229

258230
describe('client persistence', () => {
259-
let providerUri = 'https://provider.example.com'
260231
let clientConfig = { provider: { url: providerUri }}
261232
let mockClient = {
262233
provider: { url: providerUri },
263234
serialize: () => { return clientConfig }
264235
}
265-
var auth
266-
267-
beforeEach(() => {
268-
auth = new SolidAuthOIDC()
269-
})
270236

271237
describe('loadClient()', () => {
272238
it('should throw an error if no providerUri given', () => {
@@ -290,8 +256,6 @@ describe('SolidAuthOIDC', () => {
290256
})
291257

292258
it('should store and load serialized clients', () => {
293-
let auth = new SolidAuthOIDC()
294-
295259
auth.storeClient(mockClient, providerUri)
296260
// Storing a client should cache it in the auth client
297261
expect(auth.currentClient).to.equal(mockClient)
@@ -305,21 +269,18 @@ describe('SolidAuthOIDC', () => {
305269

306270
describe('currentLocation()', () => {
307271
it('should return the current window uri', () => {
272+
localStorage.clear()
273+
308274
let currentUri = 'https://client-app.example.com'
309-
let auth = new SolidAuthOIDC({ window: { location: { href: currentUri } } })
275+
let auth = new SolidAuthOIDC({
276+
window: { location: { href: currentUri } }, store: localStorage
277+
})
310278

311279
expect(auth.currentLocation()).to.equal(currentUri)
312280
})
313281
})
314282

315283
describe('validateOrSendAuthRequest()', () => {
316-
var auth
317-
318-
beforeEach(() => {
319-
localStorage.clear()
320-
auth = new SolidAuthOIDC({ window: { location: {} } })
321-
})
322-
323284
it('should throw an error when no client is given', () => {
324285
expect(auth.validateOrSendAuthRequest())
325286
.to.be.rejectedWith(/Could not load or register a RelyingParty client/)
@@ -353,13 +314,6 @@ describe('SolidAuthOIDC', () => {
353314
})
354315

355316
describe('initUserFromResponse()', () => {
356-
var auth
357-
358-
beforeEach(() => {
359-
localStorage.clear()
360-
auth = new SolidAuthOIDC({ window: { location: {} } })
361-
})
362-
363317
it('should validate the auth response', () => {
364318
let aliceWebId = 'https://alice.example.com/'
365319
let authResponse = {
@@ -386,7 +340,6 @@ describe('SolidAuthOIDC', () => {
386340

387341
describe('sendAuthRequest()', () => {
388342
it('should compose an auth request uri, save provider, and redirect', () => {
389-
let auth = new SolidAuthOIDC({ window: { location: {} } })
390343
let state = 'abcd'
391344
let providerUri = 'https://provider.example.com'
392345
let authUri = `https://provider.example.com/authorize?state=${state}`
@@ -409,22 +362,18 @@ describe('SolidAuthOIDC', () => {
409362
describe('currentUser()', () => {
410363
it('should return cached webId if present', () => {
411364
let aliceWebId = 'https://alice.example.com'
412-
let auth = new SolidAuthOIDC()
413365
auth.webId = aliceWebId
414366

415367
expect(auth.currentUser()).to.eventually.equal(aliceWebId)
416368
})
417369

418370
it('should return null if no cached webId and no current state param', () => {
419-
let auth = new SolidAuthOIDC({ window: { location: {} } })
420-
421371
expect(auth.currentUser()).to.eventually.not.exist()
422372
})
423373

424374
it('should automatically login if current uri has state param', () => {
425375
let state = 'abcd'
426376
let providerUri = 'https://provider.example.com'
427-
let auth = new SolidAuthOIDC({ window: { location: {} } })
428377
auth.saveProviderByState(state, providerUri)
429378

430379
auth.window.location.href = `https://client-app.example.com#state=${state}`
@@ -441,12 +390,6 @@ describe('SolidAuthOIDC', () => {
441390
})
442391

443392
describe('providerEndSessionEndpoint()', () => {
444-
let auth
445-
446-
beforeEach(() => {
447-
auth = new SolidAuthOIDC()
448-
})
449-
450393
it('should return null if no current client', () => {
451394
auth.currentClient = null
452395

@@ -479,7 +422,7 @@ describe('SolidAuthOIDC', () => {
479422
expect(url).to.equal(null)
480423
})
481424

482-
it('should return null if current provider end session endpoint', () => {
425+
it('should return the provider end session endpoint', () => {
483426
auth.currentClient = {
484427
provider: {
485428
configuration: {
@@ -496,8 +439,6 @@ describe('SolidAuthOIDC', () => {
496439

497440
describe('clearAuthResponseFromUrl()', () => {
498441
it('should replace the current url with a no-hash cleared one', () => {
499-
let auth = new SolidAuthOIDC()
500-
501442
let clearedUrl = 'https://rp.com'
502443

503444
auth.currentLocationNoHash = sinon.stub().returns(clearedUrl)

0 commit comments

Comments
 (0)