-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathauth-helper.js
332 lines (286 loc) · 8.79 KB
/
auth-helper.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { URL } from 'url'
import dayjs from 'dayjs'
import Joi from 'joi'
import checkErrorResponse from './check-error-response.js'
import { InvalidParameter, InvalidResponse } from './errors.js'
import { fetch } from './got.js'
import { parseJson } from './json.js'
import validate from './validate.js'
let jwtCache = Object.create(null)
class AuthHelper {
constructor(
{
userKey,
passKey,
authorizedOrigins,
serviceKey,
isRequired = false,
defaultToEmptyStringForUser = false,
},
config,
) {
if (!userKey && !passKey) {
throw Error('Expected userKey or passKey to be set')
}
if (!authorizedOrigins && !serviceKey) {
throw Error('Expected authorizedOrigins or serviceKey to be set')
}
this._userKey = userKey
this._passKey = passKey
if (userKey) {
this._user = config.private[userKey]
} else {
this._user = defaultToEmptyStringForUser ? '' : undefined
}
this._pass = passKey ? config.private[passKey] : undefined
this.isRequired = isRequired
if (serviceKey !== undefined && !(serviceKey in config.public.services)) {
// Keep this as its own error, as it's useful to the programmer as they're
// getting auth set up.
throw Error(`Service key ${serviceKey} was missing from config schema`)
}
let requireStrictSsl, requireStrictSslToAuthenticate
if (serviceKey === undefined) {
requireStrictSsl = true
requireStrictSslToAuthenticate = true
} else {
;({
authorizedOrigins,
requireStrictSsl = true,
requireStrictSslToAuthenticate = true,
} = config.public.services[serviceKey])
}
if (!Array.isArray(authorizedOrigins)) {
throw Error('Expected authorizedOrigins to be an array of origins')
}
this._authorizedOrigins = authorizedOrigins
this._requireStrictSsl = requireStrictSsl
this._requireStrictSslToAuthenticate = requireStrictSslToAuthenticate
}
get isConfigured() {
return (
this._authorizedOrigins.length > 0 &&
(this._userKey ? Boolean(this._user) : true) &&
(this._passKey ? Boolean(this._pass) : true)
)
}
get isValid() {
if (this.isRequired) {
return this.isConfigured
} else {
const configIsEmpty = !this._user && !this._pass
return this.isConfigured || configIsEmpty
}
}
static _isInsecureSslRequest({ options = {} }) {
const strictSSL = options?.https?.rejectUnauthorized ?? true
return strictSSL !== true
}
enforceStrictSsl({ options = {} }) {
if (
this._requireStrictSsl &&
this.constructor._isInsecureSslRequest({ options })
) {
throw new InvalidParameter({ prettyMessage: 'strict ssl is required' })
}
}
isAllowedOrigin(url) {
let parsed
try {
parsed = new URL(url)
} catch (e) {
throw new InvalidParameter({ prettyMessage: 'invalid url parameter' })
}
const { protocol, host } = parsed
const origin = `${protocol}//${host}`
return this._authorizedOrigins.includes(origin)
}
shouldAuthenticateRequest({ url, options = {} }) {
const originViolation = !this.isAllowedOrigin(url)
const strictSslCheckViolation =
this._requireStrictSslToAuthenticate &&
this.constructor._isInsecureSslRequest({ options })
return this.isConfigured && !originViolation && !strictSslCheckViolation
}
get _basicAuth() {
const { _user: username, _pass: password } = this
return this.isConfigured
? { username: username || '', password: password || '' }
: undefined
}
/*
* Helper function for `withBasicAuth()` and friends.
*/
_withAnyAuth(requestParams, mergeAuthFn) {
this.enforceStrictSsl(requestParams)
const shouldAuthenticate = this.shouldAuthenticateRequest(requestParams)
if (this.isRequired && !shouldAuthenticate) {
throw new InvalidParameter({
prettyMessage: 'requested origin not authorized',
})
}
return shouldAuthenticate ? mergeAuthFn(requestParams) : requestParams
}
static _mergeAuth(requestParams, auth) {
const { options, ...rest } = requestParams
return {
options: {
...auth,
...options,
},
...rest,
}
}
withBasicAuth(requestParams) {
return this._withAnyAuth(requestParams, requestParams =>
this.constructor._mergeAuth(requestParams, this._basicAuth),
)
}
_bearerAuthHeader(bearerKey) {
const { _pass: pass } = this
return this.isConfigured
? { Authorization: `${bearerKey} ${pass}` }
: undefined
}
_apiKeyHeader(apiKeyHeader) {
const { _pass: pass } = this
return this.isConfigured ? { [apiKeyHeader]: pass } : undefined
}
static _mergeHeaders(requestParams, headers) {
const {
options: { headers: existingHeaders, ...restOptions } = {},
...rest
} = requestParams
return {
options: {
headers: {
...existingHeaders,
...headers,
},
...restOptions,
},
...rest,
}
}
withApiKeyHeader(requestParams, header = 'x-api-key') {
return this._withAnyAuth(requestParams, requestParams =>
this.constructor._mergeHeaders(requestParams, this._apiKeyHeader(header)),
)
}
withBearerAuthHeader(
requestParams,
bearerKey = 'Bearer', // lgtm [js/hardcoded-credentials]
) {
return this._withAnyAuth(requestParams, requestParams =>
this.constructor._mergeHeaders(
requestParams,
this._bearerAuthHeader(bearerKey),
),
)
}
static _mergeQueryParams(requestParams, query) {
const {
options: { searchParams: existingQuery, ...restOptions } = {},
...rest
} = requestParams
return {
options: {
searchParams: {
...existingQuery,
...query,
},
...restOptions,
},
...rest,
}
}
withQueryStringAuth({ userKey, passKey }, requestParams) {
return this._withAnyAuth(requestParams, requestParams =>
this.constructor._mergeQueryParams(requestParams, {
...(userKey ? { [userKey]: this._user } : undefined),
...(passKey ? { [passKey]: this._pass } : undefined),
}),
)
}
static _getJwtExpiry(token, max = dayjs().add(1, 'hours').unix()) {
// get the expiry timestamp for this JWT (capped at a max length)
const parts = token.split('.')
if (parts.length < 2) {
throw new InvalidResponse({
prettyMessage: 'invalid response data from auth endpoint',
})
}
const json = validate(
{
ErrorClass: InvalidResponse,
prettyErrorMessage: 'invalid response data from auth endpoint',
},
parseJson(Buffer.from(parts[1], 'base64').toString()),
Joi.object({ exp: Joi.number().required() }).required(),
)
return Math.min(json.exp, max)
}
static _isJwtValid(expiry) {
// we consider the token valid if the expiry
// datetime is later than (now + 1 minute)
return dayjs.unix(expiry).isAfter(dayjs().add(1, 'minutes'))
}
async _getJwt(loginEndpoint) {
const { _user: username, _pass: password } = this
// attempt to get JWT from cache
if (
jwtCache?.[loginEndpoint]?.[username]?.token &&
jwtCache?.[loginEndpoint]?.[username]?.expiry &&
this.constructor._isJwtValid(jwtCache[loginEndpoint][username].expiry)
) {
// cache hit
return jwtCache[loginEndpoint][username].token
}
// cache miss - request a new JWT
const originViolation = !this.isAllowedOrigin(loginEndpoint)
if (originViolation) {
throw new InvalidParameter({
prettyMessage: 'requested origin not authorized',
})
}
const { buffer } = await checkErrorResponse({})(
await fetch(loginEndpoint, {
method: 'POST',
form: { username, password },
}),
)
const json = validate(
{
ErrorClass: InvalidResponse,
prettyErrorMessage: 'invalid response data from auth endpoint',
},
parseJson(buffer),
Joi.object({ token: Joi.string().required() }).required(),
)
const token = json.token
const expiry = this.constructor._getJwtExpiry(token)
// store in the cache
if (!(loginEndpoint in jwtCache)) {
jwtCache[loginEndpoint] = {}
}
jwtCache[loginEndpoint][username] = { token, expiry }
return token
}
async _getJwtAuthHeader(loginEndpoint) {
if (!this.isConfigured) {
return undefined
}
const token = await this._getJwt(loginEndpoint)
return { Authorization: `Bearer ${token}` }
}
async withJwtAuth(requestParams, loginEndpoint) {
const authHeader = await this._getJwtAuthHeader(loginEndpoint)
return this._withAnyAuth(requestParams, requestParams =>
this.constructor._mergeHeaders(requestParams, authHeader),
)
}
}
function clearJwtCache() {
jwtCache = Object.create(null)
}
export { AuthHelper, clearJwtCache }