| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269 |
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
25x
25x
3x
3x
3x
1x
2x
2x
25x
25x
25x
25x
25x
25x
25x
25x
25x
7x
6x
1x
6x
6x
6x
6x
4x
1x
3x
3x
3x
1x
1x
29x
5x
5x
5x
1x
1x
1x
1x
1x
1x
1x
1x
| 'use strict'
/**
* Dependencies
*/
const url = require('url')
const {JSONDocument} = require('@trust/json-document')
const KeyChain = require('@trust/keychain')
const ProviderSchema = require('./schemas/ProviderSchema')
const AuthenticationRequest = require('./handlers/AuthenticationRequest')
const OpenIDConfigurationRequest = require('./handlers/OpenIDConfigurationRequest')
const DynamicRegistrationRequest = require('./handlers/DynamicRegistrationRequest')
const JWKSetRequest = require('./handlers/JWKSetRequest')
const TokenRequest = require('./handlers/TokenRequest')
const UserInfoRequest = require('./handlers/UserInfoRequest')
const RPInitiatedLogoutRequest = require('./handlers/RPInitiatedLogoutRequest')
/**
* OpenID Connect Provider
*/
class Provider extends JSONDocument {
/**
* constructor
*/
constructor (data, options) {
//assert(issuer, 'OpenID Provider must have an issuer')
data = Provider.initializeEndpoints(data, options)
super(data, options)
}
/**
* from
*
* @description
* Factory method, resolves with a Provider instance, initialized from the
* provided serialized provider `data`.
* If data includes an exported JWK Set, the provider's keychain is imported,
* otherwise, a new keychain is generated.
*
* @param data {Object} Parsed JSON of a serialized Provider
*
* @returns {Promise<Provider>}
*/
static from (data) {
let provider = new Provider(data)
let validation = provider.validate()
// schema validation
if (!validation.valid) {
return Promise.reject(new Error('Invalid provider data'))
}
return provider.initializeKeyChain(provider.keys)
.then(() => provider)
}
/**
* initializeEndpoints
*
* @param data
* @param options
*
* @returns {Object} Provider data object
*/
static initializeEndpoints(data, options) {
let issuer = data.issuer || ''
data['authorization_endpoint'] = data['authorization_endpoint'] || url.resolve(issuer, '/authorize')
data['token_endpoint'] = data['token_endpoint'] || url.resolve(issuer, '/token')
data['userinfo_endpoint'] = data['userinfo_endpoint'] || url.resolve(issuer, '/userinfo')
data['jwks_uri'] = data['jwks_uri'] || url.resolve(issuer, '/jwks')
data['registration_endpoint'] = data['registration_endpoint'] || url.resolve(issuer, '/register')
data['check_session_iframe'] = data['check_session_iframe'] || url.resolve(issuer, '/session')
data['end_session_endpoint'] = data['end_session_endpoint'] || url.resolve(issuer, '/logout')
return data
}
/**
* initializeKeyChain
*
* @param data {Object} Parsed JSON of a serialized provider's .keys property
* @returns {Promise<Provider>} Resolves to self, chainable
*/
initializeKeyChain (data) {
if (!data) {
return this.generateKeyChain()
}
return this.importKeyChain(data)
}
/**
* generateKeyChain
*/
generateKeyChain () {
let modulusLength = 2048
let descriptor = {
id_token: {
signing: {
RS256: { alg: 'RS256', modulusLength },
RS384: { alg: 'RS384', modulusLength },
RS512: { alg: 'RS512', modulusLength }
},
encryption: {
// ?
}
},
token: {
signing: {
RS256: { alg: 'RS256', modulusLength },
RS384: { alg: 'RS384', modulusLength },
RS512: { alg: 'RS512', modulusLength }
},
encryption: {}
},
userinfo: {
encryption: {}
},
register: {
signing: {
RS256: { alg: 'RS256', modulusLength }
}
}
}
this.keys = new KeyChain(descriptor)
return this.keys.rotate()
}
/**
* importKeyChain
*
* @param data {Object} Parsed JSON of a serialized provider's .keys property
* @returns {Promise<Provider>} Resolves to self, chainable
*/
importKeyChain (data) {
if (!data) {
return Promise.reject(new Error('Cannot import empty keychain'))
}
return KeyChain.restore(data)
.then(keychain => {
this.keys = keychain
return this
})
}
/**
* openidConfiguration
*/
get openidConfiguration () {
return JSON.stringify(this, Object.keys(ProviderSchema.properties))
}
/**
* jwkSet
*/
get jwkSet () {
return this.keys.jwkSet
}
/**
* Schema
*
* @returns {JSONSchema}
*/
static get schema () {
return ProviderSchema
}
/**
* inject
*/
inject (properties) {
Object.keys(properties).forEach(key => {
let value = properties[key]
Object.defineProperty(this, key, {
enumerable: false,
value
})
})
}
/**
* Authorize
*
* @param {HTTPRequest} req
* @param {HTTPResponse} res
*/
authorize (req, res) {
AuthenticationRequest.handle(req, res, this)
}
/**
* Logout
*
* Bound to the OP's `end_session_endpoint` uri
*
* @param req {HTTPRequest}
* @param res {HTTPResponse}
*/
logout (req, res) {
RPInitiatedLogoutRequest.handle(req, res, this)
}
/**
* Discover
*
* @param {HTTPRequest} req
* @param {HTTPResponse} res
*/
discover (req, res) {
OpenIDConfigurationRequest.handle(req, res, this)
}
/**
* JWKs
*
* @param {HTTPRequest} req
* @param {HTTPResponse} res
*/
jwks (req, res) {
JWKSetRequest.handle(req, res, this)
}
/**
* Register
*
* @param {HTTPRequest} req
* @param {HTTPResponse} res
*/
register (req, res) {
DynamicRegistrationRequest.handle(req, res, this)
}
/**
* Token
*
* @param {HTTPRequest} req
* @param {HTTPResponse} res
*/
token (req, res) {
TokenRequest.handle(req, res, this)
}
/**
* UserInfo
*
* @param {HTTPRequest} req
* @param {HTTPResponse} res
*/
userinfo (req, res) {
UserInfoRequest.handle(req, res, this)
}
}
/**
* Export
*/
module.exports = Provider
|