-
Notifications
You must be signed in to change notification settings - Fork 1
/
oidc-interceptor.js
146 lines (124 loc) · 3.86 KB
/
oidc-interceptor.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
'use strict'
const { createDecoder } = require('fast-jwt')
const { refreshAccessToken } = require('./lib/utils')
const { RetryHandler, getGlobalDispatcher } = require('undici')
const decode = createDecoder()
const EXP_DIFF_MS = 10 * 1000
const NEAR_EXP_DIFF_MS = 30 * 1000
const TOKEN_STATE = {
VALID: 'VALID',
EXPIRED: 'EXPIRED',
NEAR_EXPIRATION: 'NEAR_EXPIRATION'
}
function getTokenState (token) {
if (!token) return TOKEN_STATE.EXPIRED
const { exp } = decode(token)
if (exp <= (Date.now() + EXP_DIFF_MS) / 1000) return TOKEN_STATE.EXPIRED
if (exp <= (Date.now() + NEAR_EXP_DIFF_MS) / 1000) return TOKEN_STATE.NEAR_EXPIRATION
return TOKEN_STATE.VALID
}
function createOidcInterceptor (options) {
const { refreshToken, clientSecret, contentType } = options
let {
accessToken,
retryOnStatusCodes,
idpTokenUrl,
urls,
clientId,
scope,
resource,
audience
} = options
retryOnStatusCodes = retryOnStatusCodes || [401]
urls = urls || []
// TODO: if there is a refresh_token, we might not need the idpTokenUrl and use the standard
// discovery mechanism. See
// https://github.com/panva/oauth4webapi/blob/8173ba2944ede8beff11e59019940bbd6440ea96/src/index.ts#L1054-L1093
if (!idpTokenUrl) {
throw new Error('No idpTokenUrl provided')
}
if (!clientId) throw new Error('No clientId provided')
let _requestingRefresh
function callRefreshToken () {
if (_requestingRefresh) return _requestingRefresh
_requestingRefresh = refreshAccessToken({
idpTokenUrl,
refreshToken,
clientId,
clientSecret,
contentType,
scope,
resource,
audience
}).finally(() => _requestingRefresh = null)
return _requestingRefresh
}
return dispatch => {
return function Intercept (opts, handler) {
if (!opts.oauthRetry && !urls.includes(opts.origin)) {
// do not attempt intercept
return dispatch(opts, handler)
}
if (opts.oauthRetry) {
return callRefreshToken()
.catch(err => {
handler.onError(err)
})
.then(newAccessToken => {
accessToken = newAccessToken
opts.headers.authorization = `Bearer ${accessToken}`
return dispatch(opts, handler)
})
}
if (!opts.headers) opts.headers = {}
if (accessToken && !opts.headers.authorization) {
opts.headers.authorization = `Bearer ${accessToken}`
}
const dispatcher = opts.dispatcher || getGlobalDispatcher()
const retryHandler = new RetryHandler({
...opts,
oauthRetry: true,
retryOptions: {
statusCodes: retryOnStatusCodes,
maxRetries: 1,
retryAfter: 0,
minTimeout: 0,
timeoutFactor: 1
}
}, {
dispatch (opts, handler) {
return dispatcher.dispatch(opts, handler)
},
handler
})
const saveTokenAndRetry = newAccessToken => {
accessToken = newAccessToken
opts.headers = {
...opts.headers,
authorization: `Bearer ${accessToken}`
}
dispatcher.emit('oauth:token-refreshed', newAccessToken)
return dispatch(opts, retryHandler)
}
switch (getTokenState(accessToken)) {
case TOKEN_STATE.EXPIRED:
return callRefreshToken()
.then(saveTokenAndRetry)
.catch(err => {
handler.onError(err)
})
case TOKEN_STATE.NEAR_EXPIRATION:
callRefreshToken()
.then(newAccessToken => {
accessToken = newAccessToken
dispatcher.emit('oauth:token-refreshed', newAccessToken)
})
.catch(/* do nothing */)
default:
return dispatch(opts, retryHandler)
}
}
}
}
module.exports = createOidcInterceptor
module.exports.createOidcInterceptor = createOidcInterceptor