Skip to content

Commit 093dcc3

Browse files
Refactor to use the new oidc-rp lib
1 parent 4806f0a commit 093dcc3

File tree

5 files changed

+43
-43
lines changed

5 files changed

+43
-43
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
},
3939
"homepage": "https://github.com/solid/solid-multi-rp-client",
4040
"dependencies": {
41-
"anvil-connect-express": "^0.3.2"
41+
"oidc-rp": "0.0.0"
4242
},
4343
"devDependencies": {
44+
"sinon": "^1.17.6",
4445
"standard": "^5.4.1",
4546
"tape": "^4.6.2"
4647
},

src/multi-rp-client.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const ClientStore = require('./store')
3-
const OIDCExpressClient = require('anvil-connect-express')
3+
const OIDCRelyingParty = require('oidc-rp')
44
const DEFAULT_MAX_AGE = 86400
55

66
class MultiRpClient {
@@ -14,19 +14,19 @@ class MultiRpClient {
1414
* Returns the authorization (signin) URL for a given OIDC client (which
1515
* is tied to / registered with a specific OIDC Provider).
1616
* @method authUrl
17-
* @param expressClient {OIDCExpressClient}
17+
* @param client {RelyingParty}
1818
* @param workflow {string} OIDC workflow type, one of 'code' or 'implicit'.
1919
* @return {string} Absolute URL for an OIDC auth call (to start either
2020
* the Authorization Code workflow, or the Implicit workflow).
2121
*/
22-
authUrl (expressClient, workflow = 'code') {
22+
authUrl (client, workflow = 'code') {
2323
let debug = this.debug
2424
let authParams = {
2525
endpoint: 'signin',
2626
response_mode: 'query',
2727
// response_mode: 'form_post',
28-
client_id: expressClient.client.client_id,
29-
redirect_uri: expressClient.client.redirect_uri,
28+
client_id: client.client_id,
29+
redirect_uri: client.redirect_uri,
3030
// state: '...', // not doing state for the moment
3131
scope: 'openid profile' // not doing 'openid profile' for the moment
3232
}
@@ -37,7 +37,7 @@ class MultiRpClient {
3737
authParams.nonce = '123' // TODO: Implement proper nonce generation
3838
}
3939

40-
var signinUrl = expressClient.client.authorizationUri(authParams)
40+
var signinUrl = client.authorizationUri(authParams)
4141
debug('Signin url: ' + signinUrl)
4242
return signinUrl
4343
}
@@ -115,20 +115,8 @@ class MultiRpClient {
115115

116116
registerClient (config) {
117117
let debug = this.debug
118-
let oidcExpress = new OIDCExpressClient(config)
119-
debug('Running client.initProvider()...')
120-
return oidcExpress.client.initProvider()
121-
.then(() => {
122-
debug('Client discovered, JWKs retrieved')
123-
if (!oidcExpress.client.client_id) {
124-
// Register if you haven't already.
125-
debug('Registering client')
126-
return oidcExpress.client.register(config)
127-
} else {
128-
// Already registered.
129-
return oidcExpress
130-
}
131-
})
118+
debug('new OIDCRelyingParty.register()', config)
119+
return OIDCRelyingParty.register(config.issuer, config, {})
132120
}
133121

134122
/**

src/store.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
const OIDCExpressClient = require('anvil-connect-express')
1+
const OIDCRelyingParty = require('oidc-rp')
22

33
module.exports = class OIDCClientStore {
44
constructor () {
55
this.clients = {}
66
}
7-
put (expressClient) {
7+
put (client) {
8+
if (!client) {
9+
return Promise.reject(new Error('Cannot store null client'))
10+
}
811
return Promise.resolve()
912
.then(() => {
10-
this.clients[expressClient.client.issuer] = expressClient.client.serialize()
11-
return expressClient
13+
let issuer = client.provider.url
14+
this.clients[issuer] = client.serialize()
15+
return client
1216
})
1317
}
1418
get (issuer) {
1519
return Promise.resolve()
1620
.then(() => {
1721
if (issuer in this.clients) {
1822
let clientConfig = JSON.parse(this.clients[issuer])
19-
return new OIDCExpressClient(clientConfig)
23+
return OIDCRelyingParty.from(clientConfig)
2024
} else {
2125
return null
2226
}

test/unit/client-store-test.js

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

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

7-
test('client store and retrieve test', t => {
7+
test('client store test', t => {
88
let issuer = 'https://oidc.example.com'
99
let store = new ClientStore()
10-
let expressClient = new OIDCExpressClient({ issuer })
11-
store.put(expressClient)
10+
let client = new OIDCRelyingParty({ provider: { url: issuer }})
11+
return store.put(client)
1212
.then((storedClient) => {
13-
t.equal(storedClient, expressClient,
13+
t.equal(storedClient, client,
1414
'store.put() should return the stored client')
15-
return store.get(issuer)
16-
})
17-
.then(retrievedClient => {
18-
t.equal(retrievedClient.client.issuer, expressClient.client.issuer,
19-
'Should be able to retrieve the stored client')
2015
t.end()
2116
})
22-
.catch(err => { t.fail(err) })
17+
.catch(err => {
18+
console.log(err)
19+
t.fail(err)
20+
})
2321
})

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict'
22

33
const test = require('tape')
4-
const OIDCExpressClient = require('anvil-connect-express')
4+
const OIDCRelyingParty = require('oidc-rp')
55
const { ClientStore, MultiRpClient } = require('../../src/index')
6+
const sinon = require('sinon')
67

78
test('MultiRpClient constructor test', t => {
89
let store = new ClientStore()
@@ -37,19 +38,27 @@ test('MultiRpClient.registrationConfigFor() test', t => {
3738
test('MultiRpClient.clientForIssuer() - client exists in store test', t => {
3839
let issuer = 'https://oidc.example.com'
3940
let store = new ClientStore()
40-
let expressClient = new OIDCExpressClient({ issuer })
41+
let getStub = sinon.stub(store, 'get', (issuer) => {
42+
return Promise.resolve(new OIDCRelyingParty({ provider: { url: issuer }}))
43+
})
44+
let client = new OIDCRelyingParty({ provider: { url: issuer }})
4145
let multiClient
42-
store.put(expressClient)
46+
store.put(client)
4347
.then(() => {
4448
multiClient = new MultiRpClient({ store })
4549
return multiClient.clientForIssuer(issuer)
4650
})
4751
.then(retrievedClient => {
48-
t.equal(retrievedClient.client.issuer, expressClient.client.issuer,
52+
t.equal(retrievedClient.issuer, client.issuer,
4953
'If client config exists in store, clientForIssuer() should retrieve it')
54+
t.ok(getStub.calledWith(issuer))
55+
getStub.restore()
5056
t.end()
5157
})
52-
.catch(err => { t.fail(err) })
58+
.catch(err => {
59+
console.log(err)
60+
t.fail(err)
61+
})
5362
})
5463

5564
test('MultiRpClient.redirectUriForIssuer() test', t => {

0 commit comments

Comments
 (0)