forked from panva/node-oidc-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice_code.js
246 lines (199 loc) · 6.43 KB
/
device_code.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
const upperFirst = require('../../helpers/_/upper_first');
const camelCase = require('../../helpers/_/camel_case');
const errors = require('../../helpers/errors');
const presence = require('../../helpers/validate_presence');
const instance = require('../../helpers/weak_cache');
const filterClaims = require('../../helpers/filter_claims');
const revoke = require('../../helpers/revoke');
const dpopValidate = require('../../helpers/validate_dpop');
const {
AuthorizationPending,
ExpiredToken,
InvalidGrant,
InvalidTarget,
} = errors;
const gty = 'device_code';
module.exports.handler = async function deviceCodeHandler(ctx, next) {
presence(ctx, 'device_code');
const {
issueRefreshToken,
conformIdTokenClaims,
features: {
userinfo,
dPoP: { iatTolerance },
mTLS: { getCertificate },
resourceIndicators,
},
} = instance(ctx.oidc.provider).configuration();
const code = await ctx.oidc.provider.DeviceCode.find(ctx.oidc.params.device_code, {
ignoreExpiration: true,
});
if (!code) {
throw new InvalidGrant('device code not found');
}
let cert;
if (ctx.oidc.client.tlsClientCertificateBoundAccessTokens) {
cert = getCertificate(ctx);
if (!cert) {
throw new InvalidGrant('mutual TLS client certificate not provided');
}
}
if (code.clientId !== ctx.oidc.client.clientId) {
throw new InvalidGrant('device code client mismatch');
}
if (code.isExpired) {
throw new ExpiredToken('device code is expired');
}
if (!code.accountId && !code.error) {
throw new AuthorizationPending();
}
if (code.consumed) {
await revoke(ctx, code.grantId);
throw new InvalidGrant('device code already consumed');
}
await code.consume();
if (code.error) {
const className = upperFirst(camelCase(code.error));
if (errors[className]) {
throw new errors[className](code.errorDescription);
}
throw new errors.CustomOIDCProviderError(code.error, code.errorDescription);
}
const grant = await ctx.oidc.provider.Grant.find(code.grantId, {
ignoreExpiration: true,
});
if (!grant) {
throw new InvalidGrant('grant not found');
}
if (grant.isExpired) {
throw new InvalidGrant('grant is expired');
}
if (grant.clientId !== ctx.oidc.client.clientId) {
throw new InvalidGrant('device code client mismatch');
}
ctx.oidc.entity('DeviceCode', code);
ctx.oidc.entity('Grant', grant);
const account = await ctx.oidc.provider.Account.findAccount(ctx, code.accountId, code);
if (!account) {
throw new InvalidGrant('device code invalid (referenced account not found)');
}
if (code.accountId !== grant.accountId) {
throw new InvalidGrant('authorization code accountId mismatch');
}
ctx.oidc.entity('Account', account);
const {
AccessToken, IdToken, RefreshToken, ReplayDetection,
} = ctx.oidc.provider;
const at = new AccessToken({
accountId: account.accountId,
client: ctx.oidc.client,
expiresWithSession: code.expiresWithSession,
grantId: code.grantId,
gty,
sessionUid: code.sessionUid,
sid: code.sid,
});
if (ctx.oidc.client.tlsClientCertificateBoundAccessTokens) {
at.setThumbprint('x5t', cert);
}
const dPoP = await dpopValidate(ctx);
if (dPoP) {
const unique = await ReplayDetection.unique(
ctx.oidc.client.clientId, dPoP.jti, dPoP.iat + iatTolerance,
);
ctx.assert(unique, new InvalidGrant('DPoP Token Replay detected'));
at.setThumbprint('jkt', dPoP.thumbprint);
}
let resource;
if (resourceIndicators.enabled) {
if (!ctx.oidc.params.resource && (!userinfo.enabled || !code.scopes.has('openid'))) {
resource = code.resource;
} else {
resource = ctx.oidc.params.resource;
}
if (Array.isArray(resource)) {
resource = await resourceIndicators.defaultResource(ctx, ctx.oidc.client, resource);
}
if (Array.isArray(resource)) {
throw new InvalidTarget('only a single resource indicator value must be requested/resolved during Access Token Request');
}
if (resource && !code.resourceIndicators.has(resource)) {
throw new InvalidTarget();
}
}
if (resource) {
const resourceServerInfo = await resourceIndicators
.getResourceServerInfo(ctx, resource, ctx.oidc.client);
at.resourceServer = new ctx.oidc.provider.ResourceServer(resource, resourceServerInfo);
at.scope = grant.getResourceScopeFiltered(resource, code.scopes);
} else {
at.claims = code.claims;
at.scope = grant.getOIDCScopeFiltered(code.scopes);
}
ctx.oidc.entity('AccessToken', at);
const accessToken = await at.save();
let refreshToken;
if (await issueRefreshToken(ctx, ctx.oidc.client, code)) {
const rt = new RefreshToken({
accountId: account.accountId,
acr: code.acr,
amr: code.amr,
authTime: code.authTime,
claims: code.claims,
client: ctx.oidc.client,
expiresWithSession: code.expiresWithSession,
grantId: code.grantId,
gty,
nonce: code.nonce,
resource: code.resource,
rotations: 0,
scope: code.scope,
sessionUid: code.sessionUid,
sid: code.sid,
});
if (ctx.oidc.client.tokenEndpointAuthMethod === 'none') {
if (at.jkt) {
rt.jkt = at.jkt;
}
if (ctx.oidc.client.tlsClientCertificateBoundAccessTokens) {
rt['x5t#S256'] = at['x5t#S256'];
}
}
ctx.oidc.entity('RefreshToken', rt);
refreshToken = await rt.save();
}
let idToken;
if (code.scopes.has('openid')) {
const claims = filterClaims(code.claims, 'id_token', grant);
const rejected = grant.getRejectedOIDCClaims();
const token = new IdToken({
...await account.claims('id_token', code.scope, claims, rejected),
...{
acr: code.acr,
amr: code.amr,
auth_time: code.authTime,
},
}, { ctx });
if (conformIdTokenClaims && userinfo.enabled && !accessToken.aud) {
token.scope = 'openid';
} else {
token.scope = grant.getOIDCScopeFiltered(code.scopes);
}
token.mask = claims;
token.rejected = rejected;
token.set('nonce', code.nonce);
token.set('at_hash', accessToken);
token.set('sid', code.sid);
idToken = await token.issue({ use: 'idtoken' });
}
ctx.body = {
access_token: accessToken,
expires_in: at.expiration,
id_token: idToken,
refresh_token: refreshToken,
scope: at.scope,
token_type: at.tokenType,
};
return next();
};
module.exports.parameters = new Set(['device_code']);