-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathauth_code.grant.ts
273 lines (206 loc) · 9.57 KB
/
auth_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
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
import { PlainVerifier } from "../code_verifiers/plain.verifier";
import { S256Verifier } from "../code_verifiers/s256.verifier";
import { CodeChallengeMethod, ICodeChallenge } from "../code_verifiers/verifier";
import { OAuthAuthCode } from "../entities/auth_code.entity";
import { OAuthClient } from "../entities/client.entity";
import { OAuthScope } from "../entities/scope.entity";
import { OAuthException } from "../exceptions/oauth.exception";
import { AuthorizationRequest } from "../requests/authorization.request";
import { RequestInterface } from "../requests/request";
import { RedirectResponse } from "../responses/redirect.response";
import { ResponseInterface } from "../responses/response";
import { base64decode } from "../utils/base64";
import { DateInterval } from "../utils/date_interval";
import { AbstractAuthorizedGrant } from "./abstract/abstract_authorized.grant";
import { GrantIdentifier } from "./abstract/grant.interface";
export interface IAuthCodePayload {
client_id: string;
auth_code_id: string;
expire_time: number;
scopes: string[];
user_id?: string;
redirect_uri?: string;
code_challenge?: string;
code_challenge_method?: string;
}
export const REGEXP_CODE_CHALLENGE = /^[A-Za-z0-9-._~]{43,128}$/;
export const REGEXP_CODE_VERIFIER = /^[A-Za-z0-9-._~]{43,128}$/;
export const REGEX_ACCESS_TOKEN = /[A-Za-z0-9\-\._~\+\/]+=*/g;
export class AuthCodeGrant extends AbstractAuthorizedGrant {
readonly identifier: GrantIdentifier = "authorization_code";
protected authCodeTTL: DateInterval = new DateInterval("15m");
private codeChallengeVerifiers = {
plain: new PlainVerifier(),
S256: new S256Verifier(),
};
async respondToAccessTokenRequest(
request: RequestInterface,
response: ResponseInterface,
accessTokenTTL: DateInterval,
): Promise<ResponseInterface> {
const client = await this.validateClient(request);
const encryptedAuthCode = this.getRequestParameter("code", request);
if (!encryptedAuthCode) throw OAuthException.invalidRequest("code");
const decryptedCode = await this.decrypt(encryptedAuthCode);
const validatedPayload = await this.validateAuthorizationCode(decryptedCode, client, request);
const userId = validatedPayload.user_id;
const user = userId ? await this.userRepository.getUserByCredentials(userId) : undefined;
const scopes: OAuthScope[] = [];
try {
const finalizedScopes = await this.scopeRepository.finalize(
await this.validateScopes(validatedPayload.scopes ?? []),
this.identifier,
client,
userId,
);
finalizedScopes.forEach(scope => scopes.push(scope));
} catch (e) {
throw OAuthException.invalidRequest("code", "Cannot verify scopes");
}
const authCode = await this.authCodeRepository.getByIdentifier(validatedPayload.auth_code_id);
if (authCode.codeChallenge) {
if (!validatedPayload.code_challenge) throw OAuthException.invalidRequest("code_challenge");
if (authCode.codeChallenge !== validatedPayload.code_challenge) {
throw OAuthException.invalidRequest("code_challenge", "Provided code challenge does not match auth code");
}
const codeVerifier = this.getRequestParameter("code_verifier", request);
if (!codeVerifier) {
throw OAuthException.invalidRequest("code_verifier");
}
// Validate code_verifier according to RFC-7636
// @see: https://tools.ietf.org/html/rfc7636#section-4.1
if (!REGEXP_CODE_VERIFIER.test(codeVerifier)) {
throw OAuthException.invalidRequest(
"code_verifier",
"Code verifier must follow the specifications of RFS-7636",
);
}
const codeChallengeMethod: CodeChallengeMethod | undefined = validatedPayload.code_challenge_method;
let verifier: ICodeChallenge = this.codeChallengeVerifiers.plain;
if (codeChallengeMethod?.toLowerCase() === "s256") {
verifier = this.codeChallengeVerifiers.S256;
}
if (!verifier.verifyCodeChallenge(codeVerifier, validatedPayload.code_challenge)) {
throw OAuthException.invalidGrant("Failed to verify code challenge.");
}
}
let accessToken = await this.issueAccessToken(accessTokenTTL, client, user, scopes);
accessToken = await this.issueRefreshToken(accessToken);
await this.authCodeRepository.revoke(validatedPayload.auth_code_id);
const extraJwtFields = user ? await this.userRepository.extraAccessTokenFields?.(user) : undefined;
return await this.makeBearerTokenResponse(client, accessToken, scopes, extraJwtFields);
}
canRespondToAuthorizationRequest(request: RequestInterface): boolean {
const responseType = this.getQueryStringParameter("response_type", request);
const hasClientId = !!this.getQueryStringParameter("client_id", request);
return responseType === "code" && hasClientId;
}
async validateAuthorizationRequest(request: RequestInterface): Promise<AuthorizationRequest> {
const clientId = this.getQueryStringParameter("client_id", request);
if (typeof clientId !== "string") {
throw OAuthException.invalidRequest("client_id");
}
const client = await this.clientRepository.getByIdentifier(clientId);
if (!client) {
throw OAuthException.invalidClient();
}
const redirectUri = this.getRedirectUri(request, client);
const bodyScopes = this.getQueryStringParameter("scope", request, []);
const scopes = await this.validateScopes(bodyScopes);
const stateParameter = this.getQueryStringParameter("state", request);
const authorizationRequest = new AuthorizationRequest(this.identifier, client, redirectUri);
authorizationRequest.state = stateParameter;
authorizationRequest.scopes = scopes;
const codeChallenge = this.getQueryStringParameter("code_challenge", request);
if (this.options.requiresPKCE && !codeChallenge) {
throw OAuthException.invalidRequest(
"code_challenge",
"The authorization server requires public clients to use PKCE RFC-7636",
);
}
if (codeChallenge) {
const codeChallengeMethod = this.getQueryStringParameter("code_challenge_method", request, "plain");
authorizationRequest.codeChallenge = codeChallenge;
authorizationRequest.codeChallengeMethod = codeChallengeMethod;
}
return authorizationRequest;
}
async completeAuthorizationRequest(authorizationRequest: AuthorizationRequest): Promise<ResponseInterface> {
if (!authorizationRequest.user) {
throw OAuthException.logicException("A user should be set on the authorization request");
}
const redirectUri = authorizationRequest.redirectUri;
if (!redirectUri) {
throw OAuthException.invalidRequest("redirect_uri");
}
if (!authorizationRequest.isAuthorizationApproved) {
throw OAuthException.logicException("Authorization is not approved");
}
const authCode = await this.issueAuthCode(
this.authCodeTTL,
authorizationRequest.client,
authorizationRequest.user.id,
authorizationRequest.redirectUri,
authorizationRequest.codeChallenge,
authorizationRequest.codeChallengeMethod,
authorizationRequest.scopes,
);
const payload: IAuthCodePayload = {
client_id: authCode.client.id,
redirect_uri: authCode.redirectUri,
auth_code_id: authCode.code,
scopes: authCode.scopes.map(scope => scope.name),
user_id: authCode.user?.id,
expire_time: this.authCodeTTL.getEndTimeSeconds(),
code_challenge: authorizationRequest.codeChallenge,
code_challenge_method: authorizationRequest.codeChallengeMethod,
};
const jsonPayload = JSON.stringify(payload);
const code = await this.encrypt(jsonPayload);
const finalRedirectUri = this.makeRedirectUrl(redirectUri, {
code,
state: authorizationRequest.state,
});
return new RedirectResponse(finalRedirectUri);
}
private async validateAuthorizationCode(payload: any, client: OAuthClient, request: RequestInterface) {
if (!payload.auth_code_id) {
throw OAuthException.invalidRequest("code", "Authorization code malformed");
}
const isCodeRevoked = await this.authCodeRepository.isRevoked(payload.auth_code_id);
if (Date.now() / 1000 > payload.expire_time || isCodeRevoked) {
throw OAuthException.invalidRequest("code", "Authorization code is expired or revoked");
}
if (payload.client_id !== client.id) {
throw OAuthException.invalidRequest("code", "Authorization code was not issued to this client");
}
const redirectUri = this.getRequestParameter("redirect_uri", request);
if (!!payload.redirect_uri && !redirectUri) {
throw OAuthException.invalidRequest("redirect_uri");
}
if (payload.redirect_uri !== redirectUri) {
throw OAuthException.invalidRequest("redirect_uri", "Invalid redirect URI");
}
return payload;
}
private async issueAuthCode(
authCodeTTL: DateInterval,
client: OAuthClient,
userIdentifier?: string,
redirectUri?: string,
codeChallenge?: string,
codeChallengeMethod?: string,
scopes: OAuthScope[] = [],
): Promise<OAuthAuthCode> {
const user = userIdentifier ? await this.userRepository.getUserByCredentials(userIdentifier) : undefined;
const authCode = await this.authCodeRepository.issueAuthCode(client, user, scopes);
authCode.expiresAt = authCodeTTL.getEndDate();
authCode.redirectUri = redirectUri;
authCode.codeChallenge = codeChallenge;
authCode.codeChallengeMethod = codeChallengeMethod;
authCode.scopes = [];
scopes.forEach(scope => authCode.scopes.push(scope));
await this.authCodeRepository.persist(authCode);
return authCode;
}
}