Repository navigation
Expand file tree
/
Copy pathQuick.Core.Extensions.Authentication.OAuth2.pas
More file actions
461 lines (421 loc) · 14.9 KB
/
Copy pathQuick.Core.Extensions.Authentication.OAuth2.pas
File metadata and controls
461 lines (421 loc) · 14.9 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
{ ***************************************************************************
Copyright (c) 2016-2026 Kike Perez
Unit : Quick.Core.Extensions.Authentication.OAuth2
Description : Core Extensions Authentication OAuth2 / OpenID Connect
Author : Kike Perez
Version : 1.0
Created : 13/04/2026
Modified : 08/05/2026
This file is part of QuickCore: https://github.com/exilon/QuickCore
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Core.Extensions.Authentication.OAuth2;
{$i QuickCore.inc}
interface
uses
System.SysUtils,
System.Classes,
System.NetEncoding,
System.DateUtils,
System.Net.HttpClient,
System.Net.URLClient,
Quick.Options,
Quick.Core.DependencyInjection,
Quick.Core.Security.Authentication,
Quick.Core.Security.Claims,
Quick.Core.Logging.Abstractions;
const
OAuth2Defaults_AuthenticationScheme = 'OAuth2';
type
/// <summary>Options for OAuth2 / OpenID Connect Bearer token validation.</summary>
TOAuth2Options = class(TOptions)
private
fAuthority : string;
fClientId : string;
fAudience : string;
fValidateAudience : Boolean;
fValidateIssuer : Boolean;
fIntrospectionEndpoint : string; // RFC 7662 – overrides auto-discovery
fSaveTokens : Boolean;
fRequireHttpsMetadata : Boolean;
published
/// <summary>Base URL of the identity provider (e.g. https://accounts.google.com).</summary>
property Authority : string read fAuthority write fAuthority;
/// <summary>The OAuth2 client_id registered with the identity provider.</summary>
property ClientId : string read fClientId write fClientId;
/// <summary>Expected audience claim in the token.</summary>
property Audience : string read fAudience write fAudience;
/// <summary>Validate that the token audience matches Audience. Default True.</summary>
property ValidateAudience : Boolean read fValidateAudience write fValidateAudience;
/// <summary>Validate that the token issuer matches Authority. Default True.</summary>
property ValidateIssuer : Boolean read fValidateIssuer write fValidateIssuer;
/// <summary>Optional explicit token introspection endpoint (RFC 7662).
/// When empty, the handler decodes the JWT payload locally.</summary>
property IntrospectionEndpoint : string read fIntrospectionEndpoint write fIntrospectionEndpoint;
/// <summary>Save the raw access token as a Claim on the principal. Default False.</summary>
property SaveTokens : Boolean read fSaveTokens write fSaveTokens;
property RequireHttpsMetadata : Boolean read fRequireHttpsMetadata write fRequireHttpsMetadata;
end;
/// <summary>Authentication handler for OAuth2 / OpenID Connect tokens.
///
/// Two validation modes:
/// 1. Introspection (IntrospectionEndpoint set): issues an RFC 7662 call and
/// maps the response JSON fields to claims.
/// 2. Local decode (default): base64-decodes the JWT payload and maps its
/// JSON fields to claims — useful when the token was already verified by
/// an upstream API gateway or reverse proxy.
/// </summary>
TOAuth2AuthenticationHandler = class(TAuthenticationHandler<TOAuth2Options>, IAuthenticationHandler)
private
{ Parse JWT payload (middle base64url segment) into a list of name=value pairs. }
function DecodeJwtPayload(const aToken : string;
out vClaims : TArray<TPair<string,string>>) : Boolean;
{ Call RFC 7662 introspection endpoint; returns active=true/false and claims. }
function Introspect(const aToken : string;
out vClaims : TArray<TPair<string,string>>) : Boolean;
{ Build a TClaimsPrincipal from claim pairs. }
function BuildPrincipal(const aClaims : TArray<TPair<string,string>>;
const aScheme : string) : TClaimsPrincipal;
{ Minimal JSON string → key/value flat map (top-level scalars only). }
class function ParseJsonFlat(const aJson : string) : TArray<TPair<string,string>>;
protected
procedure DoInitialize; override;
public
constructor Create(aOptions : IOptions<TOAuth2Options>; aLogger : ILogger);
function Authenticate : TAuthenticateResult; override;
end;
TOAuth2ServiceExtension = class(TServiceCollectionExtension)
public
class function AddOAuth2(aConfigureOptions : TConfigureOptionsProc<TOAuth2Options>) : TServiceCollection;
end;
implementation
{ TOAuth2AuthenticationHandler }
constructor TOAuth2AuthenticationHandler.Create(aOptions: IOptions<TOAuth2Options>; aLogger: ILogger);
begin
inherited Create(aOptions, aLogger);
end;
procedure TOAuth2AuthenticationHandler.DoInitialize;
begin
// Nothing extra – base class sets fRequest / fContext / fResponse.
end;
class function TOAuth2AuthenticationHandler.ParseJsonFlat(
const aJson : string) : TArray<TPair<string,string>>;
var
i : Integer;
key : string;
value : string;
s : string;
inStr : Boolean;
depth : Integer;
pairs : TStringList;
p : TStringList;
name : string;
val : string;
raw : string;
pos1 : Integer;
pos2 : Integer;
begin
// Very compact hand-rolled JSON flat parser (no external dependency).
// Only handles top-level string/number/bool scalar fields.
SetLength(Result, 0);
raw := Trim(aJson);
if raw.IsEmpty or (raw[1] <> '{') then Exit;
pairs := TStringList.Create;
try
// Iterate over the JSON looking for "key": value pairs at depth 1.
depth := 0;
inStr := False;
i := 1;
key := '';
while i <= Length(raw) do
begin
case raw[i] of
'"':
begin
inStr := not inStr;
if inStr and (depth = 1) and (key = '') then
begin
// start of a key
Inc(i);
while (i <= Length(raw)) and not ((raw[i] = '"') and (raw[i-1] <> '\')) do
begin
key := key + raw[i];
Inc(i);
end;
// i now points at closing "
end
else if inStr and (depth = 1) and (key <> '') then
begin
// start of a string value
value := '';
Inc(i);
while (i <= Length(raw)) and not ((raw[i] = '"') and (raw[i-1] <> '\')) do
begin
value := value + raw[i];
Inc(i);
end;
pairs.Add(key + '=' + value);
key := '';
value := '';
end;
end;
'{', '[' : if not inStr then Inc(depth);
'}', ']' : if not inStr then Dec(depth);
':' :
if (not inStr) and (depth = 1) and (key <> '') then
begin
// read non-string scalar value (number, bool, null)
Inc(i);
while (i <= Length(raw)) and (raw[i] = ' ') do Inc(i);
if (i <= Length(raw)) and (raw[i] <> '"') and (raw[i] <> '{') and (raw[i] <> '[') then
begin
value := '';
while (i <= Length(raw)) and not (raw[i] in [',', '}', ']', #10, #13]) do
begin
value := value + raw[i];
Inc(i);
end;
value := Trim(value);
if (value <> '') and (value <> 'null') then
pairs.Add(key + '=' + value);
key := '';
value := '';
Continue; // skip the Inc(i) at bottom
end;
end;
end;
Inc(i);
end;
SetLength(Result, pairs.Count);
for i := 0 to pairs.Count - 1 do
begin
pos1 := Pos('=', pairs[i]);
Result[i] := TPair<string,string>.Create(
Copy(pairs[i], 1, pos1 - 1),
Copy(pairs[i], pos1 + 1, MaxInt));
end;
finally
pairs.Free;
end;
end;
function TOAuth2AuthenticationHandler.DecodeJwtPayload(const aToken : string;
out vClaims : TArray<TPair<string,string>>) : Boolean;
var
parts : TArray<string>;
payload : string;
decoded : TBytes;
json : string;
rem : Integer;
nowEpoch : Int64;
expFound : Boolean;
claimIdx : Integer;
begin
Result := False;
SetLength(vClaims, 0);
parts := aToken.Split(['.']);
if Length(parts) <> 3 then Exit;
payload := parts[1];
// Base64url → standard Base64
payload := StringReplace(payload, '-', '+', [rfReplaceAll]);
payload := StringReplace(payload, '_', '/', [rfReplaceAll]);
rem := Length(payload) mod 4;
if rem = 2 then payload := payload + '=='
else if rem = 3 then payload := payload + '=';
try
decoded := TNetEncoding.Base64.DecodeStringToBytes(payload);
json := TEncoding.UTF8.GetString(decoded);
except
Exit;
end;
vClaims := ParseJsonFlat(json);
if Length(vClaims) = 0 then Exit;
// SEC-24: Validate token lifetime from payload claims (exp / nbf)
nowEpoch := DateTimeToUnix(Now, False);
expFound := False;
for claimIdx := 0 to High(vClaims) do
begin
if SameText(vClaims[claimIdx].Key, 'exp') then
begin
expFound := True;
if StrToInt64Def(vClaims[claimIdx].Value, 0) < nowEpoch then Exit; // token expired
end
else if SameText(vClaims[claimIdx].Key, 'nbf') then
begin
if StrToInt64Def(vClaims[claimIdx].Value, 0) > nowEpoch then Exit; // token not yet valid
end;
end;
if not expFound then Exit; // exp claim required
Result := True;
end;
function TOAuth2AuthenticationHandler.Introspect(const aToken : string;
out vClaims : TArray<TPair<string,string>>) : Boolean;
var
http : THTTPClient;
resp : IHTTPResponse;
body : string;
postData : TStringStream;
pair : TPair<string,string>;
begin
Result := False;
SetLength(vClaims, 0);
http := THTTPClient.Create;
try
postData := TStringStream.Create(
'token=' + TNetEncoding.URL.Encode(aToken) +
'&token_type_hint=access_token',
TEncoding.UTF8);
try
http.ContentType := 'application/x-www-form-urlencoded';
resp := http.Post(fOptions.IntrospectionEndpoint, postData);
body := resp.ContentAsString(TEncoding.UTF8);
finally
postData.Free;
end;
finally
http.Free;
end;
if body.IsEmpty then Exit;
vClaims := ParseJsonFlat(body);
// RFC 7662 requires "active": true
for pair in vClaims do
if SameText(pair.Key, 'active') and SameText(pair.Value.Trim, 'true') then
begin
Result := True;
Break;
end;
end;
function TOAuth2AuthenticationHandler.BuildPrincipal(
const aClaims : TArray<TPair<string,string>>;
const aScheme : string) : TClaimsPrincipal;
var
identity : TClaimsIdentity;
pair : TPair<string,string>;
issuer : string;
audience : string;
claimType : string;
begin
issuer := '';
audience := '';
identity := TClaimsIdentity.Create(aScheme);
for pair in aClaims do
begin
if SameText(pair.Key, 'iss') then issuer := pair.Value
else if SameText(pair.Key, 'aud') then audience := pair.Value;
claimType := pair.Key;
// Map standard JWT claim names to well-known claim types
if SameText(claimType, 'sub') then claimType := 'sub'
else if SameText(claimType, 'email') then claimType := 'email'
else if SameText(claimType, 'name') then claimType := 'name'
else if SameText(claimType, 'role') or SameText(claimType, 'roles') then
claimType := 'role';
// SEC-20: Only add DefaultRoleClaimType when the claim is actually a role claim.
// Adding it for every claim causes privilege escalation.
if claimType = 'role' then
identity.AddClaim(TClaimsIdentity.DefaultRoleClaimType, pair.Value);
identity.AddClaim(claimType, pair.Value);
end;
// Optional issuer / audience validation
if fOptions.ValidateIssuer and (not fOptions.Authority.IsEmpty) then
if not SameText(issuer, fOptions.Authority) then
begin
identity.Free;
Result := nil;
Exit;
end;
if fOptions.ValidateAudience and (not fOptions.Audience.IsEmpty) then
if not SameText(audience, fOptions.Audience) then
begin
identity.Free;
Result := nil;
Exit;
end;
Result := TClaimsPrincipal.Create(identity);
end;
function TOAuth2AuthenticationHandler.Authenticate : TAuthenticateResult;
var
authHeader : string;
token : string;
claims : TArray<TPair<string,string>>;
principal : TClaimsPrincipal;
ticket : IAuthenticationTicket;
ok : Boolean;
begin
authHeader := fRequest.Headers.GetValue('Authorization');
if authHeader.IsEmpty then
begin
Result.NoResult;
Exit;
end;
if not authHeader.StartsWith('Bearer ', True) then
begin
Result.NoResult;
Exit;
end;
token := authHeader.Substring(7).Trim;
if token.IsEmpty then
begin
Result.Fail(Exception.Create('Empty Bearer token'));
Exit;
end;
if not fOptions.IntrospectionEndpoint.IsEmpty then
begin
// SEC-25: Enforce HTTPS on introspection endpoint when RequireHttpsMetadata=True
if fOptions.RequireHttpsMetadata and
not fOptions.IntrospectionEndpoint.ToLower.StartsWith('https://') then
begin
Result.Fail(Exception.Create('OAuth2: introspection endpoint must use HTTPS'));
Exit;
end;
ok := Introspect(token, claims);
end
else
ok := DecodeJwtPayload(token, claims);
if not ok then
begin
Result.Fail(Exception.Create('OAuth2: token validation failed'));
Exit;
end;
principal := BuildPrincipal(claims, fScheme.Name);
if principal = nil then
begin
Result.Fail(Exception.Create('OAuth2: issuer or audience validation failed'));
Exit;
end;
if fOptions.SaveTokens then
principal.Identity.AddClaim('access_token', token);
ticket := TAuthenticationTicket.Create(principal, fScheme.Name);
Result.Success(ticket);
end;
{ TOAuth2ServiceExtension }
class function TOAuth2ServiceExtension.AddOAuth2(
aConfigureOptions : TConfigureOptionsProc<TOAuth2Options>) : TServiceCollection;
var
opts : TOAuth2Options;
begin
Result := ServiceCollection;
opts := TOAuth2Options.Create;
opts.ValidateAudience := True;
opts.ValidateIssuer := True;
opts.RequireHttpsMetadata := True;
aConfigureOptions(opts);
opts.HideOptions := True;
Result.Configure<TOAuth2Options>(opts);
Result.Configure<TAuthenticationOptions>(
procedure(aOpts : TAuthenticationOptions)
begin
aOpts.AddScheme(OAuth2Defaults_AuthenticationScheme,
TypeInfo(TOAuth2AuthenticationHandler));
if aOpts.DefaultAuthenticateScheme = '' then
aOpts.DefaultAuthenticateScheme := OAuth2Defaults_AuthenticationScheme;
end);
end;
end.