-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery.js
96 lines (88 loc) · 2.58 KB
/
discovery.js
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
/* eslint-disable func-names */
/* eslint-disable no-use-before-define */
/**
* Class to handle OpenIDConnect discovery through .well-known
*/
const api = require('./api')
module.exports = (function () {
/**
* Factory function to hide class instantiation.
*
* @param config the configuration object
* @returns {Discovery}
*/
function factory(config) {
return new Discovery(config)
}
/**
* Discovery class constructor.
*
* @param config the configuration object. Must contain host and path.
* @constructor
*/
function Discovery(config) {
this.config = config || {}
}
/**
* Method to run OpenIDConnect discovery.
* @param onDiscovered callback on when discovery was successful
* @param onError callback on when error occurred during discovery
*/
Discovery.prototype.discover = function (onDiscovered, onError) {
// Begin by fetching data from the discovery endpoint
getDiscoveryData(
this.config,
discoveryData => {
if (!discoveryData.jwks_uri) {
onError('jwkEndpoint missing from OIDC discovery data')
}
// If discovery went OK, fetch the jwk endpoint data
getJwkData(
discoveryData.jwks_uri,
jwkData => {
onDiscovered(discoveryData, jwkData)
},
err => {
onError(err)
}
)
},
err => {
onError(err)
}
)
}
/**
* Makes an API call to the OpenIDConnect providers discovery endpoint and
* fetches the data as json.
* @param config the Discovery class configuration
* @param onSuccess callback for when discovery fetching is successful
* @param onError callback for when an error occurs during discovery fetching
*/
function getDiscoveryData(config, onSuccess, onError) {
const discoveryApi = api({
host: config.host,
path: config.path,
})
discoveryApi.request(onSuccess, onError)
}
/**
* Makes an API call to the jwk_endpoint on the OpenIDConnect provider.
*
* @param jwksUri the uri for the jwk_endpoint (complete with host and path)
* @param onSuccess callback for when jwk endpoint fetching was successful
* @param onError callback for when an error occurs during jwk endpoint fetching
*/
function getJwkData(jwksUri, onSuccess, onError) {
const parsedJwkEndpoint = new URL(jwksUri)
const jwkDiscovery = api({
host: parsedJwkEndpoint.host,
path: parsedJwkEndpoint.pathname,
})
jwkDiscovery.request(onSuccess, onError)
}
/**
* Module exposes the factory method.
*/
return factory
})()