Skip to content

Commit 90dbb7a

Browse files
Implement client register()
1 parent 2f4f524 commit 90dbb7a

2 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
"url": "https://github.com/solid/solid-auth-oidc/issues"
4747
},
4848
"homepage": "https://github.com/solid/solid-auth-oidc",
49-
"dependencies": {},
49+
"dependencies": {
50+
"oidc-rp": "^0.1.0"
51+
},
5052
"devDependencies": {
5153
"babel-cli": "^6.18.0",
5254
"babel-core": "^6.21.0",

‎src/index.js‎

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,95 @@
2525
https://github.com/solid/solid
2626
*/
2727
'use strict'
28-
29-
module.exports = ClientAuthOIDC
28+
const RelyingParty = require('oidc-rp')
3029

3130
class ClientAuthOIDC {
31+
constructor () {
32+
this.rp = null
33+
this.providerUri = null
34+
}
35+
36+
keyByProvider () {
37+
return `oidc.rp.by-provider.${this.providerUri}`
38+
}
39+
40+
keyByState () {
41+
return `oidc.rp.by-state.${this.state}`
42+
}
43+
44+
get client () {
45+
// If one is already initialized, return it
46+
if (this.rp) {
47+
return this.rp
48+
}
49+
// Check to see if there's already a registered client in local storage
50+
if (this.providerUri) {
51+
return localStorage.getItem(this.keyByProvider())
52+
}
53+
}
54+
55+
set client (rp) {
56+
this.rp = rp
57+
localStorage.setItem(this.keyByProvider(), rp.serialize())
58+
}
59+
60+
/**
61+
* @private
62+
* @param providerUri {string}
63+
* @param [options={}]
64+
* @param [options.redirectUri] {string} Defaults to window.location.href
65+
* @param [options.scope='openid profile'] {string}
66+
* @param [options.store=localStorage]
67+
* @throws {TypeError} If providerUri is missing
68+
* @return {Promise<RelyingParty>} Registered RelyingParty client instance
69+
*/
70+
register (providerUri, options = {}) {
71+
return this.registerPublicClient(providerUri, options)
72+
.then(client => {
73+
this.rp = client
74+
localStorage.setItem('oidc.clients.'+providerUri, client.serialize())
75+
this.client_id = client.registration.client_id
76+
console.log('registered client:', client)
77+
})
78+
.catch(error => {
79+
console.error('Error while registering:', error)
80+
})
81+
}
82+
83+
/**
84+
* @private
85+
* @param providerUri {string}
86+
* @param [options={}]
87+
* @param [options.redirectUri] {string} Defaults to window.location.href
88+
* @param [options.scope='openid profile'] {string}
89+
* @param [options.store=localStorage]
90+
* @throws {TypeError} If providerUri is missing
91+
* @return {Promise<RelyingParty>} Registered RelyingParty client instance
92+
*/
93+
registerPublicClient (providerUri, options = {}) {
94+
if (!providerUri) {
95+
throw TypeError('Cannot register auth client, missing providerUri')
96+
}
97+
let redirectUri = options.redirectUri || window.location.href
98+
this.redirectUri = redirectUri
99+
let registration = {
100+
issuer: providerUri,
101+
grant_types: ['implicit'],
102+
redirect_uris: [ redirectUri ],
103+
response_types: ['id_token token'],
104+
scope: options.scope || 'openid profile'
105+
}
106+
let rpOptions = {
107+
defaults: {
108+
authenticate: {
109+
redirect_uri: redirectUri,
110+
response_type: 'id_token token'
111+
}
112+
},
113+
store: options.store || localStorage
114+
}
115+
return RelyingParty
116+
.register(providerUri, registration, rpOptions)
117+
}
32118
}
119+
module.exports = ClientAuthOIDC

0 commit comments

Comments
 (0)