Skip to content

Commit 2c0d7d4

Browse files
Store serialized client config in client store
1 parent 66cab48 commit 2c0d7d4

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

src/multi-rp-client.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ class MultiRpClient {
1010
this.debug = options.debug || console.log.bind(console)
1111
}
1212

13+
/**
14+
* @method clientForIssuer
15+
* @param issuerUri {string}
16+
* @returns {Promise<OIDCExpressClient>}
17+
*/
1318
clientForIssuer (issuerUri) {
1419
let debug = this.debug
15-
return this.store.get(issuerUri)
20+
return this.loadClient(issuerUri)
1621
.then(client => {
1722
debug('Client fetched for issuer.')
1823
if (client) {
@@ -22,17 +27,35 @@ class MultiRpClient {
2227
// client not already in store, create and register it
2328
let registrationConfig = this.registrationConfigFor(issuerUri)
2429
return this.registerClient(registrationConfig)
25-
})
26-
.then(registeredClient => {
27-
// Store and return the newly registered client
28-
return this.store.put(registeredClient)
30+
.then(registeredClient => {
31+
// Store and return the newly registered client
32+
return this.persistClient(registeredClient)
33+
})
2934
})
3035
}
3136

37+
/**
38+
* @method loadClient
39+
* @param issuerUri {string}
40+
* @returns {Promise<OIDCExpressClient>}
41+
*/
42+
loadClient (issuerUri) {
43+
return this.store.get(issuerUri)
44+
}
45+
3246
get localIssuer () {
3347
return this.localConfig.issuer
3448
}
3549

50+
/**
51+
* @method persistClient
52+
* @param expressClient {OIDCExpressClient}
53+
* @return {Promise<OIDCExpressClient>}
54+
*/
55+
persistClient (expressClient) {
56+
return this.store.put(expressClient)
57+
}
58+
3659
/**
3760
* @method redirectUriForIssuer
3861
* @param issuer {string} Issuer URI

src/store.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
const OIDCExpressClient = require('anvil-connect-express')
2+
13
module.exports = class OIDCClientStore {
24
constructor () {
35
this.clients = {}
46
}
57
put (expressClient) {
68
return Promise.resolve()
79
.then(() => {
8-
this.clients[expressClient.client.issuer] = expressClient
10+
this.clients[expressClient.client.issuer] = expressClient.client.serialize()
911
return expressClient
1012
})
1113
}
1214
get (issuer) {
1315
return Promise.resolve()
1416
.then(() => {
1517
if (issuer in this.clients) {
16-
return this.clients[issuer]
18+
let clientConfig = JSON.parse(this.clients[issuer])
19+
return new OIDCExpressClient(clientConfig)
1720
} else {
1821
return null
1922
}

test/unit/client-store-test.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
const test = require('tape')
44
const { ClientStore } = require('../../src/index')
5-
5+
const OIDCExpressClient = require('anvil-connect-express')
66

77
test('client store and retrieve test', t => {
8-
let issuerUrl = 'https://oidc.example.com'
8+
let issuer = 'https://oidc.example.com'
99
let store = new ClientStore()
10-
let expressClient = {
11-
client: {
12-
issuer: issuerUrl
13-
}
14-
}
10+
let expressClient = new OIDCExpressClient({ issuer })
1511
store.put(expressClient)
1612
.then((storedClient) => {
1713
t.equal(storedClient, expressClient,
1814
'store.put() should return the stored client')
19-
return store.get(issuerUrl)
15+
return store.get(issuer)
2016
})
2117
.then(retrievedClient => {
22-
t.equals(retrievedClient, expressClient,
18+
t.equal(retrievedClient.client.issuer, expressClient.client.issuer,
2319
'Should be able to retrieve the stored client')
2420
t.end()
2521
})
22+
.catch(err => { t.fail(err) })
2623
})

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ test('MultiRpClient.clientForIssuer() - client exists in store test', t => {
4545
return multiClient.clientForIssuer(issuer)
4646
})
4747
.then(retrievedClient => {
48-
t.equal(retrievedClient, expressClient,
49-
'If client exists in store, clientForIssuer() should retrieve it')
48+
t.equal(retrievedClient.client.issuer, expressClient.client.issuer,
49+
'If client config exists in store, clientForIssuer() should retrieve it')
5050
t.end()
5151
})
5252
.catch(err => { t.fail(err) })

0 commit comments

Comments
 (0)