-
Notifications
You must be signed in to change notification settings - Fork 24
/
openid-connect.js
91 lines (73 loc) · 2.3 KB
/
openid-connect.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
var purest = require('../')
var jws = require('jws')
var pem = require('jwk-to-pem')
// ----------------------------------------------------------------------------
var auth = {
auth0: {
// scope: openid
id_token: '',
// tenant or tenant.region
subdomain: '',
},
google: {
// scope: openid
id_token: '',
},
microsoft: {
// scope: openid
id_token: '',
},
}
// ----------------------------------------------------------------------------
var config = {
"auth0": {
"discovery": {
"origin": "https://{subdomain}.auth0.com",
"path": ".well-known/openid-configuration"
}
},
"google": {
"discovery": {
"origin": "https://accounts.google.com",
"path": ".well-known/openid-configuration"
}
},
"microsoft": {
"discovery": {
"origin": "https://login.microsoftonline.com",
"path": "common/v2.0/.well-known/openid-configuration"
}
}
}
// ----------------------------------------------------------------------------
var verify = ({id_token, jwk}) => {
var jwt = jws.decode(id_token)
console.log(jwt)
var key = jwk.keys.find(({kid}) => kid === jwt.header.kid)
return jws.verify(id_token, jwt.header.alg, pem(key))
}
;({
'auth0 verify': async () => {
var auth0 = purest({provider: 'auth0', config, defaults: {
subdomain: auth.auth0.subdomain
}})
var {body:doc} = await auth0('discovery').request()
var {body:jwk} = await auth0.get(doc.jwks_uri).request()
var valid = verify({id_token: auth.auth0.id_token, jwk})
console.log(valid ? 'Valid id_token!' : 'Invalid id_token!')
},
'google verify': async () => {
var google = purest({provider: 'google', config})
var {body:doc} = await google('discovery').request()
var {body:jwk} = await google.get(doc.jwks_uri).request()
var valid = verify({id_token: auth.google.id_token, jwk})
console.log(valid ? 'Valid id_token!' : 'Invalid id_token!')
},
'microsoft verify': async () => {
var microsoft = purest({provider: 'microsoft', config})
var {body:doc} = await microsoft('discovery').request()
var {body:jwk} = await microsoft.get(doc.jwks_uri).request()
var valid = verify({id_token: auth.microsoft.id_token, jwk})
console.log(valid ? 'Valid id_token!' : 'Invalid id_token!')
},
})[process.argv[2]]()