This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization-code-grant.ts
153 lines (135 loc) · 4.17 KB
/
authorization-code-grant.ts
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
import { requestToClient } from '../../lib/request-to-client'
import { requestToRedirectUri } from '../../lib/request-to-redirect-uri'
import {
invalidGrantErrorFactory,
invalidRequestErrorFactory,
jwtErrorFactory,
} from '../../error/oauth2-error'
import {
CodeChallengeMethod,
verifyCodeChallenge,
} from '../../lib/verify-code-challenge'
import { CodeTokenPayload } from '../../jwt/code-token'
import { ResponseToSend } from '../../lib/response'
import { tokenResponseParametersFactory } from '../parameters/token-response-parameters-factory'
import { TokenResponse } from '../token-response'
import { requestToScopes } from '../../lib/request-to-scopes'
export const authorizationCodeGrant = async ({
audience,
collectionApi,
issuer,
jwtApi,
maxAge,
req,
}: TokenResponse): Promise<ResponseToSend> => {
const codeToken = req.body['code']
if (typeof codeToken !== 'string') {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'Code body parameter is required',
})
}
const codeTokenPayload = (await jwtApi.verify({
jwt: codeToken,
})) as CodeTokenPayload
if (!codeTokenPayload) {
throw jwtErrorFactory({
description: 'Could not verify code token',
})
}
if (typeof codeTokenPayload.auth_code_id !== 'string') {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'auth_code_id field in code JWT is required',
})
}
const knownAuthCode = await collectionApi.authCode.get({
authCodeId: codeTokenPayload.auth_code_id,
req,
})
if (Date.now() > codeTokenPayload.expire_time * 1000) {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'Code JWT has expired',
})
}
if (Date.now() > knownAuthCode.expiresAt.getTime()) {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'Code JWT was revoked',
})
}
if (knownAuthCode.codeChallenge !== codeTokenPayload.code_challenge) {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'code_challenge field in code JWT is invalid',
})
}
const codeVerifier = req.body['code_verifier']
if (typeof codeToken !== 'string') {
throw invalidRequestErrorFactory({
arg: 'code_verifier',
description: 'Code verifier body parameter is required',
})
}
const REGEXP_CODE_VERIFIER = /^[A-Za-z0-9-._~]{43,128}$/
if (!REGEXP_CODE_VERIFIER.test(codeVerifier)) {
throw invalidRequestErrorFactory({
arg: 'code_verifier',
description: 'Code verifier body parameter does not meet RFS-7636',
})
}
const codeChallengeIsValid = verifyCodeChallenge({
method: codeTokenPayload.code_challenge_method as CodeChallengeMethod,
codeChallenge: codeTokenPayload.code_challenge,
codeVerifier,
})
if (!codeChallengeIsValid) {
throw invalidGrantErrorFactory({
description: 'Code challende verification failed',
})
}
// Retrieve and validate client from request
const client = await requestToClient({ collectionApi, req })
// Retrieve and validate redirect uri from request
const redirectUri = requestToRedirectUri({ client, req })
// Retrieve and validate scopes from request
const scopes = await requestToScopes({ client, req })
if (codeTokenPayload.client_id !== client.id) {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'client_id field in code JWT is invalid',
})
}
if (codeTokenPayload.redirect_uri !== redirectUri) {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'redirect_uri field in code JWT is invalid',
})
}
if (typeof codeTokenPayload.user_id !== 'string') {
throw invalidRequestErrorFactory({
arg: 'code',
description: 'user_id field in code JWT is required',
})
}
await collectionApi.authCode.revoke({ authCodeId: knownAuthCode.id, req })
const user = await collectionApi.user.get({
userId: codeTokenPayload.user_id,
req,
})
const parameters = await tokenResponseParametersFactory({
audience,
client,
collectionApi,
grant: 'authorization_code',
issuer,
jwtApi,
knownAuthCode,
maxAge,
req,
scopes,
user,
})
return { parameters, redirectUri }
}