Skip to content

Commit fcdbba1

Browse files
Clean up/refactor global localStorage usages
1 parent f985e38 commit fcdbba1

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
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: 34 additions & 17 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')
@@ -26,7 +27,7 @@ describe('SolidAuthOIDC', () => {
2627
let auth, providerUri
2728

2829
beforeEach(() => {
29-
auth = new SolidAuthOIDC()
30+
auth = new SolidAuthOIDC({ store: localStorage })
3031
providerUri = 'https://provider.example.com'
3132
})
3233

@@ -77,7 +78,8 @@ describe('SolidAuthOIDC', () => {
7778
let auth
7879

7980
beforeEach(() => {
80-
auth = new SolidAuthOIDC()
81+
localStorage.clear()
82+
auth = new SolidAuthOIDC({ store: localStorage })
8183
})
8284

8385
it('should clear the current user', () => {
@@ -131,7 +133,8 @@ describe('SolidAuthOIDC', () => {
131133
describe('providerFromCurrentUri()', () => {
132134
var auth
133135
beforeEach(() => {
134-
auth = new SolidAuthOIDC({ window: { location: {} } })
136+
localStorage.clear()
137+
auth = new SolidAuthOIDC({ window: { location: {} }, store: localStorage })
135138
})
136139

137140
it('should return null when no state param present', () => {
@@ -163,7 +166,8 @@ describe('SolidAuthOIDC', () => {
163166

164167
describe('provider persistence', () => {
165168
it('should store and load provider uri, by state', () => {
166-
let auth = new SolidAuthOIDC()
169+
localStorage.clear()
170+
let auth = new SolidAuthOIDC({ store: localStorage })
167171
let providerUri = 'https://provider.example.com'
168172
let state = 'abcd'
169173
// Check to see that provider doesn't exist initially
@@ -265,7 +269,8 @@ describe('SolidAuthOIDC', () => {
265269
var auth
266270

267271
beforeEach(() => {
268-
auth = new SolidAuthOIDC()
272+
localStorage.clear()
273+
auth = new SolidAuthOIDC({ store: localStorage })
269274
})
270275

271276
describe('loadClient()', () => {
@@ -290,8 +295,6 @@ describe('SolidAuthOIDC', () => {
290295
})
291296

292297
it('should store and load serialized clients', () => {
293-
let auth = new SolidAuthOIDC()
294-
295298
auth.storeClient(mockClient, providerUri)
296299
// Storing a client should cache it in the auth client
297300
expect(auth.currentClient).to.equal(mockClient)
@@ -305,8 +308,12 @@ describe('SolidAuthOIDC', () => {
305308

306309
describe('currentLocation()', () => {
307310
it('should return the current window uri', () => {
311+
localStorage.clear()
312+
308313
let currentUri = 'https://client-app.example.com'
309-
let auth = new SolidAuthOIDC({ window: { location: { href: currentUri } } })
314+
let auth = new SolidAuthOIDC({
315+
window: { location: { href: currentUri } }, store: localStorage
316+
})
310317

311318
expect(auth.currentLocation()).to.equal(currentUri)
312319
})
@@ -357,7 +364,7 @@ describe('SolidAuthOIDC', () => {
357364

358365
beforeEach(() => {
359366
localStorage.clear()
360-
auth = new SolidAuthOIDC({ window: { location: {} } })
367+
auth = new SolidAuthOIDC({ window: { location: {} }, store: localStorage })
361368
})
362369

363370
it('should validate the auth response', () => {
@@ -385,8 +392,14 @@ describe('SolidAuthOIDC', () => {
385392
})
386393

387394
describe('sendAuthRequest()', () => {
395+
var auth
396+
397+
beforeEach(() => {
398+
localStorage.clear()
399+
auth = new SolidAuthOIDC({ window: { location: {} }, store: localStorage })
400+
})
401+
388402
it('should compose an auth request uri, save provider, and redirect', () => {
389-
let auth = new SolidAuthOIDC({ window: { location: {} } })
390403
let state = 'abcd'
391404
let providerUri = 'https://provider.example.com'
392405
let authUri = `https://provider.example.com/authorize?state=${state}`
@@ -407,24 +420,27 @@ describe('SolidAuthOIDC', () => {
407420
})
408421

409422
describe('currentUser()', () => {
423+
var auth
424+
425+
beforeEach(() => {
426+
localStorage.clear()
427+
auth = new SolidAuthOIDC({ window: { location: {} }, store: localStorage })
428+
})
429+
410430
it('should return cached webId if present', () => {
411431
let aliceWebId = 'https://alice.example.com'
412-
let auth = new SolidAuthOIDC()
413432
auth.webId = aliceWebId
414433

415434
expect(auth.currentUser()).to.eventually.equal(aliceWebId)
416435
})
417436

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

424441
it('should automatically login if current uri has state param', () => {
425442
let state = 'abcd'
426443
let providerUri = 'https://provider.example.com'
427-
let auth = new SolidAuthOIDC({ window: { location: {} } })
428444
auth.saveProviderByState(state, providerUri)
429445

430446
auth.window.location.href = `https://client-app.example.com#state=${state}`
@@ -441,10 +457,11 @@ describe('SolidAuthOIDC', () => {
441457
})
442458

443459
describe('providerEndSessionEndpoint()', () => {
444-
let auth
460+
var auth
445461

446462
beforeEach(() => {
447-
auth = new SolidAuthOIDC()
463+
localStorage.clear()
464+
auth = new SolidAuthOIDC({ window: { location: {} }, store: localStorage })
448465
})
449466

450467
it('should return null if no current client', () => {

0 commit comments

Comments
 (0)