Skip to content

Commit 77765bc

Browse files
Refactor ClientStore initialization, add comments
1 parent 91ff16d commit 77765bc

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

src/client-store.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,29 @@ const KVPFileStore = require('kvplus-files')
33
const COLLECTION_NAME = 'clients'
44

55
module.exports = class OIDCClientStore {
6+
/**
7+
* @constructor
8+
*
9+
* @param [options={}] {Object}
10+
*
11+
* @param [options.collectionName='clients'] {string}
12+
*
13+
* @param [options.backend] {KVPFileStore} Either pass in a backend store
14+
* @param [options.path] {string} Or initialize the store from path.
15+
*/
616
constructor (options = {}) {
7-
let backend = options.backend || new KVPFileStore(options)
8-
backend.serialize = (client) => { return client.serialize() }
9-
backend.deserialize = (data) => { return JSON.parse(data) }
10-
this.backend = backend
1117
this.collectionName = options.collectionName || COLLECTION_NAME
18+
19+
this.backend = options.backend ||
20+
new KVPFileStore({
21+
path: options.path,
22+
collections: [ this.collectionName ]
23+
})
24+
25+
this.backend.serialize = (client) => { return client.serialize() }
26+
this.backend.deserialize = (data) => { return JSON.parse(data) }
1227
}
28+
1329
del (client) {
1430
if (!this.backend) {
1531
return Promise.reject(new Error('Client store not configured'))
@@ -20,6 +36,7 @@ module.exports = class OIDCClientStore {
2036
let issuer = encodeURIComponent(client.provider.url)
2137
return this.backend.del(this.collectionName, issuer)
2238
}
39+
2340
put (client) {
2441
if (!this.backend) {
2542
return Promise.reject(new Error('Client store not configured'))
@@ -34,6 +51,7 @@ module.exports = class OIDCClientStore {
3451
return client
3552
})
3653
}
54+
3755
get (issuer) {
3856
if (!this.backend) {
3957
return Promise.reject(new Error('Client store not configured'))

src/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
'use strict'
22

3-
module.exports = {
4-
'ClientStore': require('./store'),
5-
'MultiRpClient': require('./multi-rp-client')
6-
}
3+
module.exports = require('./multi-rp-client')

src/multi-rp-client.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
'use strict'
2-
const ClientStore = require('./store')
2+
const ClientStore = require('./client-store')
33
const OIDCRelyingParty = require('oidc-rp')
44
const DEFAULT_MAX_AGE = 86400
55

66
class MultiRpClient {
7+
/**
8+
* @constructor
9+
* @param [options={}] {Object}
10+
*
11+
* @param [options.localConfig={}] {Object}
12+
* @param [options.localConfig.issuer] {string}
13+
* @param [options.localConfig.redirect_uri] {string}
14+
* @param [options.localConfig.post_logout_redirect_uris] {Array<string>}
15+
*
16+
* Needed to initialize the ClientStore:
17+
*
18+
* @param [options.backend] {KVPFileStore} Either pass in a backend store
19+
* @param [options.path] {string} Or initialize the store from path.
20+
* @param [options.collectionName='clients'] {string}
21+
*
22+
* @param [options.debug] {Function}
23+
*/
724
constructor (options = {}) {
8-
this.store = new ClientStore(options.store)
25+
this.store = new ClientStore(options)
26+
927
this.localConfig = options.localConfig || {}
28+
1029
this.debug = options.debug || console.log.bind(console)
1130
}
1231

1332
/**
14-
* Returns the authorization (signin) URL for a given OIDC client (which
33+
* Returns the authorization (login) URL for a given OIDC client (which
1534
* is tied to / registered with a specific OIDC Provider).
35+
*
1636
* @method authUrl
1737
* @param client {RelyingParty}
1838
* @param session {Session} req.session or similar
@@ -47,6 +67,7 @@ class MultiRpClient {
4767
/**
4868
* Returns a constructed `/authorization` URL for a given issuer. Used for
4969
* starting the OIDC workflow.
70+
*
5071
* @param issuer {string} OIDC Provider URL
5172
* @param workflow {string} OIDC workflow type, one of 'code' or 'implicit'
5273
* @returns {Promise<string>}
@@ -140,6 +161,8 @@ class MultiRpClient {
140161
config.redirect_uris = config.redirect_uris || [ redirectUri ]
141162
config.response_types = config.response_types ||
142163
['code', 'id_token token', 'code id_token token']
164+
config.post_logout_redirect_uris = config.post_logout_redirect_uris ||
165+
this.localConfig.post_logout_redirect_uris || []
143166
config.scope = config.scope || 'openid profile'
144167
// client_uri: 'https://github.com/solid/node-solid-server',
145168
// logo_uri: 'solid logo',

test/unit/client-store-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const test = require('tape')
44
const KVPFileStore = require('kvplus-files')
55

6-
const ClientStore = require('../../src/store')
6+
const ClientStore = require('../../src/client-store')
77
const OIDCRelyingParty = require('oidc-rp')
88

99
const storeBasePath = './test/store/'

test/unit/multi-rp-client-test.js

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

33
const test = require('tape')
4-
const OIDCRelyingParty = require('oidc-rp')
5-
const { ClientStore, MultiRpClient } = require('../../src/index')
64
const sinon = require('sinon')
75

6+
7+
const OIDCRelyingParty = require('oidc-rp')
8+
const MultiRpClient = require('../../src/index')
9+
const ClientStore = require('../../src/client-store')
10+
811
const storeBasePath = './test/store/'
912
const storeOptions = { path: storeBasePath }
1013

@@ -25,7 +28,10 @@ test('MultiRpClient constructor test', t => {
2528
let localConfig = {
2629
issuer: localIssuer
2730
}
28-
let options = { store: storeOptions, localConfig }
31+
let options = {
32+
path: storeBasePath,
33+
localConfig
34+
}
2935
let multiClient = new MultiRpClient(options)
3036
t.equal(multiClient.store.backend.path, storeBasePath)
3137
t.equal(multiClient.localConfig, localConfig)

0 commit comments

Comments
 (0)