This repository has been archived by the owner on Aug 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
hydra.js
140 lines (127 loc) · 4.06 KB
/
hydra.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
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
/* global process */
const jwt = require('jsonwebtoken')
const OAuth2 = require('simple-oauth2')
const request = require('superagent')
const jwkToPem = require('jwk-to-pem')
const urlj = require('url-join')
require('superagent-auth-bearer')(request)
class Hydra {
constructor(config = {}) {
const {
client: {
id: clientId = process.env.HYDRA_CLIENT_ID,
secret: clientSecret = process.env.HYDRA_CLIENT_SECRET
} = {},
auth: {
tokenHost: endpoint = process.env.HYDRA_URL,
authorizePath: authorizePath = '/oauth2/auth',
tokenPath: tokenPath = '/oauth2/token'
} = {},
scope: scope = 'hydra.keys.get',
options: {
useBodyAuth: useBodyAuth = false,
useBasicAuthorizationHeader: useBasicAuthorizationHeader = true
} = {}
} = config
this.config = {
client: {
id: clientId,
secret: clientSecret
},
auth: {
tokenHost: endpoint,
authorizePath,
tokenPath
},
options: {
useBodyAuth,
useBasicAuthorizationHeader
}
}
this.scope = scope
this.endpoint = endpoint
this.token = null
}
authenticate() {
if (this.token !== null && !this.token.expired()) {
return Promise.resolve(this.token)
}
this.oauth2 = OAuth2.create(this.config)
return this.oauth2.clientCredentials.getToken({ scope: this.scope }).then((result) => {
this.token = this.oauth2.accessToken.create(result)
return Promise.resolve(this.token)
})
}
getKey(set, kid) {
return this.authenticate().then(() => request
.get(urlj(this.endpoint,`/keys/${set}/${kid}`))
.authBearer(this.token.token.access_token)
.then((res) => !res.ok
? Promise.reject({ error: new Error('Status code is not 2xx'), message: 'Could not retrieve validation key.' })
: Promise.resolve(res.body.keys[0])
)
)
}
verifyConsentChallenge(challenge = '') {
return this.getKey('hydra.consent.challenge', 'public').then((key) => {
return new Promise((resolve, reject) => {
jwt.verify(challenge, jwkToPem(key), (error, decoded) => {
if (error) {
reject({ error, message: 'Could not verify consent challenge.' })
return
}
resolve({ challenge: decoded })
})
})
})
}
generateConsentResponse(challenge, subject, scopes, at = {}, idt = {}) {
return this.verifyConsentChallenge(challenge).then(({ challenge }) => {
return this.getKey('hydra.consent.response', 'private').then((key) => {
return new Promise((resolve, reject) => {
const { aud, exp, jti } = challenge
jwt.sign({
jti,
aud,
exp,
scp: scopes,
sub: subject,
at_ext: at,
id_ext: idt
}, jwkToPem(Object.assign({}, key, {
// the following keys are optional in the spec but for some reason required by the library.
dp: '', dq: '', qi: ''
}), { private: true }), { algorithm: 'RS256' }, (error, token) => {
if (error) {
reject({ error, message: 'Could not verify consent challenge.' })
return
}
resolve({ consent: token })
})
})
})
})
}
getClient(id) {
return this.authenticate().then(() => request
.get(urlj(this.endpoint,`/clients/${id}`))
.authBearer(this.token.token.access_token)
.then((res) => !res.ok
? Promise.reject({ error: new Error('Status code is not 2xx'), message: 'Could not retrieve client.' })
: Promise.resolve(res.body)
)
)
}
validateToken(token) {
return this.authenticate().then(() => request
.post(urlj(this.endpoint,`/oauth2/introspect`))
.send(`token=${token}`)
.authBearer(this.token.token.access_token)
.then((res) => !res.ok
? Promise.reject({ error: new Error('Status code is not 2xx'), message: 'Introspection failed.' })
: Promise.resolve(res.body)
)
)
}
}
module.exports = Hydra