2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
unit MVCFramework.JWT;
|
|
|
|
|
2017-01-05 12:44:34 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
interface
|
2017-01-05 12:44:34 +01:00
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
uses
|
2017-03-23 18:51:25 +01:00
|
|
|
System.Generics.Collections,
|
2019-07-19 19:12:31 +02:00
|
|
|
JsonDataObjects,
|
2017-07-05 00:17:46 +02:00
|
|
|
MVCFramework,
|
2017-03-23 18:51:25 +01:00
|
|
|
MVCFramework.Patches;
|
2016-05-18 18:21:43 +02:00
|
|
|
|
|
|
|
type
|
2017-03-23 18:51:25 +01:00
|
|
|
|
2020-08-13 17:40:02 +02:00
|
|
|
{$SCOPEDENUMS ON}
|
2016-05-21 21:56:12 +02:00
|
|
|
TJWTCheckableClaim = (ExpirationTime, NotBefore, IssuedAt);
|
|
|
|
TJWTCheckableClaims = set of TJWTCheckableClaim;
|
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
TJWTRegisteredClaimNames = class sealed
|
|
|
|
public
|
|
|
|
const
|
2020-08-13 17:40:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The "iss" (issuer) claim identifies the principal that issued the
|
|
|
|
/// JWT. The processing of this claim is generally application specific.
|
|
|
|
/// The "iss" value is a case-sensitive string containing a StringOrURI
|
|
|
|
/// value. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
Issuer: string = 'iss';
|
2020-08-13 17:40:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The "sub" (subject) claim identifies the principal that is the
|
|
|
|
/// subject of the JWT. The claims in a JWT are normally statements
|
|
|
|
/// about the subject. The subject value MUST either be scoped to be
|
|
|
|
/// locally unique in the context of the issuer or be globally unique.
|
|
|
|
/// The processing of this claim is generally application specific. The
|
|
|
|
/// "sub" value is a case-sensitive string containing a StringOrURI
|
|
|
|
/// value. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
Subject: string = 'sub';
|
2020-08-13 17:40:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The "aud" (audience) claim identifies the recipients that the JWT is
|
|
|
|
/// intended for. Each principal intended to process the JWT MUST
|
|
|
|
/// identify itself with a value in the audience claim. If the principal
|
|
|
|
/// processing the claim does not identify itself with a value in the
|
|
|
|
/// "aud" claim when this claim is present, then the JWT MUST be
|
|
|
|
/// rejected. In the general case, the "aud" value is an array of case-
|
|
|
|
/// sensitive strings, each containing a StringOrURI value. In the
|
|
|
|
/// special case when the JWT has one audience, the "aud" value MAY be a
|
|
|
|
/// single case-sensitive string containing a StringOrURI value. The
|
|
|
|
/// interpretation of audience values is generally application specific.
|
|
|
|
/// Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
Audience: string = 'aud';
|
2020-08-13 17:40:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The "exp" (expiration time) claim identifies the expiration time on
|
|
|
|
/// or after which the JWT MUST NOT be accepted for processing. The
|
|
|
|
/// processing of the "exp" claim requires that the current date/time
|
|
|
|
/// MUST be before the expiration date/time listed in the "exp" claim.
|
|
|
|
/// Implementers MAY provide for some small leeway, usually no more than
|
|
|
|
/// a few minutes, to account for clock skew. Its value MUST be a number
|
|
|
|
/// containing a NumericDate value. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
ExpirationTime: string = 'exp';
|
2020-08-13 17:40:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The "nbf" (not before) claim identifies the time before which the JWT
|
|
|
|
/// MUST NOT be accepted for processing. The processing of the "nbf"
|
|
|
|
/// claim requires that the current date/time MUST be after or equal to
|
|
|
|
/// the not-before date/time listed in the "nbf" claim. Implementers MAY
|
|
|
|
/// provide for some small leeway, usually no more than a few minutes, to
|
|
|
|
/// account for clock skew. Its value MUST be a number containing a
|
|
|
|
/// NumericDate value. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
NotBefore: string = 'nbf';
|
2020-08-13 17:40:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The "iat" (issued at) claim identifies the time at which the JWT was
|
|
|
|
/// issued. This claim can be used to determine the age of the JWT. Its
|
|
|
|
/// value MUST be a number containing a NumericDate value. Use of this
|
|
|
|
/// claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
IssuedAt: string = 'iat';
|
2020-08-13 17:40:02 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The "jti" (JWT ID) claim provides a unique identifier for the JWT.
|
|
|
|
/// The identifier value MUST be assigned in a manner that ensures that
|
|
|
|
/// there is a negligible probability that the same value will be
|
|
|
|
/// accidentally assigned to a different data object; if the application
|
|
|
|
/// uses multiple issuers, collisions MUST be prevented among values
|
|
|
|
/// produced by different issuers as well. The "jti" claim can be used
|
|
|
|
/// to prevent the JWT from being replayed. The "jti" value is a case-
|
|
|
|
/// sensitive string. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
JWT_ID: string = 'jti';
|
2020-08-13 17:40:02 +02:00
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
Names: array [0 .. 6] of string = (
|
|
|
|
'iss',
|
|
|
|
'sub',
|
|
|
|
'aud',
|
|
|
|
'exp',
|
|
|
|
'nbf',
|
|
|
|
'iat',
|
|
|
|
'jti');
|
|
|
|
end;
|
|
|
|
|
|
|
|
TJWTDictionaryObject = class
|
|
|
|
private
|
|
|
|
FClaims: TDictionary<string, string>;
|
2017-07-12 00:31:49 +02:00
|
|
|
function GetItem(const Index: string): string;
|
|
|
|
procedure SetItem(const Index, Value: string);
|
|
|
|
function GetItemAsDateTime(const Index: string): TDateTime;
|
|
|
|
procedure SetItemAsDateTime(const Index: string; const Value: TDateTime);
|
|
|
|
property ItemsAsDateTime[const index: string]: TDateTime read GetItemAsDateTime write SetItemAsDateTime;
|
|
|
|
property Items[const index: string]: string read GetItem write SetItem; default;
|
2016-05-18 18:21:43 +02:00
|
|
|
protected
|
2017-07-12 00:31:49 +02:00
|
|
|
function Contains(const Index: string): Boolean;
|
|
|
|
function Keys: TArray<string>;
|
2016-05-18 18:21:43 +02:00
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// https://tools.ietf.org/html/rfc7519#section-4.1.1
|
|
|
|
/// </summary>
|
|
|
|
TJWTRegisteredClaims = class(TJWTDictionaryObject)
|
|
|
|
private
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure SetAudience(const Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
procedure SetExpirationTime(const Value: TDateTime);
|
|
|
|
procedure SetIssuedAt(const Value: TDateTime);
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure SetISSUER(const Value: string);
|
|
|
|
procedure SetJWT_ID(const Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
procedure SetNotBefore(const Value: TDateTime);
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure SetSubject(const Value: string);
|
|
|
|
function GetAudience: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
function GetExpirationTime: TDateTime;
|
|
|
|
function GetIssuedAt: TDateTime;
|
2017-07-12 00:31:49 +02:00
|
|
|
function GetJWT_ID: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
function GetNotBefore: TDateTime;
|
2017-07-12 00:31:49 +02:00
|
|
|
function GetSubject: string;
|
|
|
|
function GetIssuer: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
public
|
|
|
|
/// <summary>
|
|
|
|
/// "iss" (Issuer) Claim
|
|
|
|
/// The " iss "(issuer) claim identifies The principal that issued The
|
|
|
|
/// JWT. The processing of this claim is generally application specific.
|
|
|
|
/// The " iss " value is a case-sensitive string containing a StringOrURI
|
|
|
|
/// value.Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
property Issuer: string read GetIssuer write SetISSUER;
|
2016-05-18 18:21:43 +02:00
|
|
|
/// <summary>
|
|
|
|
/// "sub" (Subject) Claim
|
|
|
|
/// The "sub" (subject) claim identifies the principal that is the
|
|
|
|
/// subject of the JWT. The claims in a JWT are normally statements
|
|
|
|
/// about the subject. The subject value MUST either be scoped to be
|
|
|
|
/// locally unique in the context of the issuer or be globally unique.
|
|
|
|
/// The processing of this claim is generally application specific. The
|
|
|
|
/// "sub" value is a case-sensitive string containing a StringOrURI
|
|
|
|
/// value. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
property Subject: string read GetSubject write SetSubject;
|
2016-05-18 18:21:43 +02:00
|
|
|
/// <summary>
|
|
|
|
/// "aud" (Audience) Claim
|
|
|
|
/// The "aud" (audience) claim identifies the recipients that the JWT is
|
|
|
|
/// intended for. Each principal intended to process the JWT MUST
|
|
|
|
/// identify itself with a value in the audience claim. If the principal
|
|
|
|
/// processing the claim does not identify itself with a value in the
|
|
|
|
/// "aud" claim when this claim is present, then the JWT MUST be
|
|
|
|
/// rejected. In the general case, the "aud" value is an array of case-
|
|
|
|
/// sensitive strings, each containing a StringOrURI value. In the
|
|
|
|
/// special case when the JWT has one audience, the "aud" value MAY be a
|
|
|
|
/// single case-sensitive string containing a StringOrURI value. The
|
|
|
|
/// interpretation of audience values is generally application specific.
|
|
|
|
/// Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
property Audience: string read GetAudience write SetAudience;
|
2016-05-18 18:21:43 +02:00
|
|
|
/// <summary>
|
|
|
|
/// "exp" (Expiration Time) Claim
|
|
|
|
/// The "exp" (expiration time) claim identifies the expiration time on
|
|
|
|
/// or after which the JWT MUST NOT be accepted for processing. The
|
|
|
|
/// processing of the "exp" claim requires that the current date/time
|
|
|
|
/// MUST be before the expiration date/time listed in the "exp" claim.
|
|
|
|
/// Implementers MAY provide for some small leeway, usually no more than
|
|
|
|
/// a few minutes, to account for clock skew. Its value MUST be a number
|
|
|
|
/// containing a NumericDate value. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
|
|
|
property ExpirationTime: TDateTime read GetExpirationTime write SetExpirationTime;
|
|
|
|
/// <summary>
|
|
|
|
/// "nbf" (Not Before) Claim
|
|
|
|
/// The "nbf" (not before) claim identifies the time before which the JWT
|
|
|
|
/// MUST NOT be accepted for processing. The processing of the "nbf"
|
|
|
|
/// claim requires that the current date/time MUST be after or equal to
|
|
|
|
/// the not-before date/time listed in the "nbf" claim. Implementers MAY
|
|
|
|
/// provide for some small leeway, usually no more than a few minutes, to
|
|
|
|
/// account for clock skew. Its value MUST be a number containing a
|
|
|
|
/// NumericDate value. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
|
|
|
property NotBefore: TDateTime read GetNotBefore write SetNotBefore;
|
|
|
|
/// <summary>
|
|
|
|
/// "iat" (Issued At) Claim
|
|
|
|
/// The "iat" (issued at) claim identifies the time at which the JWT was
|
|
|
|
/// issued. This claim can be used to determine the age of the JWT. Its
|
|
|
|
/// value MUST be a number containing a NumericDate value. Use of this
|
|
|
|
/// claim is OPTIONAL.
|
|
|
|
/// </summary>
|
|
|
|
property IssuedAt: TDateTime read GetIssuedAt write SetIssuedAt;
|
|
|
|
/// <summary>
|
|
|
|
/// "jti" (JWT ID) Claim
|
|
|
|
/// The "jti" (JWT ID) claim provides a unique identifier for the JWT.
|
|
|
|
/// The identifier value MUST be assigned in a manner that ensures that
|
|
|
|
/// there is a negligible probability that the same value will be
|
|
|
|
/// accidentally assigned to a different data object; if the application
|
|
|
|
/// uses multiple issuers, collisions MUST be prevented among values
|
|
|
|
/// produced by different issuers as well. The "jti" claim can be used
|
|
|
|
/// to prevent the JWT from being replayed. The "jti" value is a case-
|
|
|
|
/// sensitive string. Use of this claim is OPTIONAL.
|
|
|
|
/// </summary>
|
2017-07-12 00:31:49 +02:00
|
|
|
property JWT_ID: string read GetJWT_ID write SetJWT_ID;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TJWTCustomClaims = class(TJWTDictionaryObject)
|
2020-04-27 17:18:16 +02:00
|
|
|
public
|
2016-05-18 18:21:43 +02:00
|
|
|
property Items; default;
|
2017-07-05 00:17:46 +02:00
|
|
|
function AsCustomData: TMVCCustomData;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
TJWT = class
|
|
|
|
private
|
|
|
|
FSecretKey: string;
|
|
|
|
FRegisteredClaims: TJWTRegisteredClaims;
|
|
|
|
FCustomClaims: TJWTCustomClaims;
|
2017-07-12 00:31:49 +02:00
|
|
|
FHMACAlgorithm: string;
|
2016-05-21 21:56:12 +02:00
|
|
|
FRegClaimsToChecks: TJWTCheckableClaims;
|
2017-07-12 00:31:49 +02:00
|
|
|
FLeewaySeconds: Cardinal;
|
2021-04-08 00:00:08 +02:00
|
|
|
FData: TObject; //the jwt middleware will inject TMVCWebRequest here
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure SetHMACAlgorithm(const Value: string);
|
2016-05-21 21:56:12 +02:00
|
|
|
procedure SetChecks(const Value: TJWTCheckableClaims);
|
2019-07-19 19:12:31 +02:00
|
|
|
function CheckExpirationTime(Payload: TJDOJSONObject; out Error: string): Boolean;
|
|
|
|
function CheckNotBefore(Payload: TJDOJSONObject; out Error: string): Boolean;
|
|
|
|
function CheckIssuedAt(Payload: TJDOJSONObject; out Error: string): Boolean;
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure SetLiveValidityWindowInSeconds(const Value: Cardinal);
|
|
|
|
function GetLiveValidityWindowInSeconds: Cardinal;
|
2019-07-19 19:12:31 +02:00
|
|
|
function IsValidToken(const Token: string; out Header, Payload: TJDOJSONObject; out Error: string): Boolean;
|
2016-05-18 18:21:43 +02:00
|
|
|
public
|
2017-07-12 00:31:49 +02:00
|
|
|
constructor Create(const SecretKey: string; const ALeewaySeconds: Cardinal = 300); virtual;
|
2016-05-18 18:21:43 +02:00
|
|
|
destructor Destroy; override;
|
2017-07-12 00:31:49 +02:00
|
|
|
function GetToken: string;
|
2017-09-22 09:33:21 +02:00
|
|
|
function LoadToken(const Token: string; out Error: string): Boolean;
|
2021-04-08 00:00:08 +02:00
|
|
|
property Data: TObject read fData write fData;
|
2016-05-18 18:21:43 +02:00
|
|
|
property Claims: TJWTRegisteredClaims read FRegisteredClaims;
|
|
|
|
property CustomClaims: TJWTCustomClaims read FCustomClaims;
|
2017-07-12 00:31:49 +02:00
|
|
|
property HMACAlgorithm: string read FHMACAlgorithm write SetHMACAlgorithm;
|
|
|
|
property LeewaySeconds: Cardinal read FLeewaySeconds;
|
2016-05-21 21:56:12 +02:00
|
|
|
property RegClaimsToChecks: TJWTCheckableClaims read FRegClaimsToChecks write SetChecks;
|
2017-07-12 00:31:49 +02:00
|
|
|
/// <summary>
|
2018-05-17 21:55:32 +02:00
|
|
|
/// Use LiveValidityWindowInSeconds to make the ExpirationTime dynamic at each request.
|
|
|
|
/// ExpirationTime will be incremented by LiveValidityWindowInSeconds seconds automatically
|
|
|
|
/// if the remaining seconds are less than the LiveValidityWindowInSeconds.
|
2017-07-12 00:31:49 +02:00
|
|
|
/// </summary>
|
2020-08-13 17:40:02 +02:00
|
|
|
property LiveValidityWindowInSeconds: Cardinal read GetLiveValidityWindowInSeconds
|
|
|
|
write SetLiveValidityWindowInSeconds;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2017-07-05 00:17:46 +02:00
|
|
|
uses
|
2019-07-19 19:12:31 +02:00
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.HMAC,
|
|
|
|
System.DateUtils,
|
|
|
|
IdGlobal;
|
2017-07-05 00:17:46 +02:00
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
{ TJWTRegisteredClaims }
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTRegisteredClaims.GetAudience: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Result := Items[TJWTRegisteredClaimNames.Audience];
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJWTRegisteredClaims.GetExpirationTime: TDateTime;
|
|
|
|
begin
|
|
|
|
Result := ItemsAsDateTime[TJWTRegisteredClaimNames.ExpirationTime];
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJWTRegisteredClaims.GetIssuedAt: TDateTime;
|
|
|
|
begin
|
|
|
|
Result := ItemsAsDateTime[TJWTRegisteredClaimNames.IssuedAt];
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTRegisteredClaims.GetIssuer: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Result := Items[TJWTRegisteredClaimNames.Issuer];
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTRegisteredClaims.GetJWT_ID: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Result := Items[TJWTRegisteredClaimNames.JWT_ID];
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJWTRegisteredClaims.GetNotBefore: TDateTime;
|
|
|
|
begin
|
|
|
|
Result := ItemsAsDateTime[TJWTRegisteredClaimNames.NotBefore];
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTRegisteredClaims.GetSubject: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Result := Items[TJWTRegisteredClaimNames.Subject];
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWTRegisteredClaims.SetAudience(const Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Items[TJWTRegisteredClaimNames.Audience] := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJWTRegisteredClaims.SetExpirationTime(const Value: TDateTime);
|
|
|
|
begin
|
|
|
|
ItemsAsDateTime[TJWTRegisteredClaimNames.ExpirationTime] := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJWTRegisteredClaims.SetIssuedAt(const Value: TDateTime);
|
|
|
|
begin
|
|
|
|
ItemsAsDateTime[TJWTRegisteredClaimNames.IssuedAt] := Value;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWTRegisteredClaims.SetISSUER(const Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Items[TJWTRegisteredClaimNames.Issuer] := Value;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWTRegisteredClaims.SetJWT_ID(const Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Items[TJWTRegisteredClaimNames.JWT_ID] := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TJWTRegisteredClaims.SetNotBefore(const Value: TDateTime);
|
|
|
|
begin
|
|
|
|
ItemsAsDateTime[TJWTRegisteredClaimNames.NotBefore] := Value;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWTRegisteredClaims.SetSubject(const Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Items[TJWTRegisteredClaimNames.Subject] := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TJWTCustomClaims }
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTDictionaryObject.Contains(const Index: string): Boolean;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2017-07-12 00:31:49 +02:00
|
|
|
Result := FClaims.ContainsKey(index);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TJWTDictionaryObject.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
2017-07-12 00:31:49 +02:00
|
|
|
FClaims := TDictionary<string, string>.Create;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TJWTDictionaryObject.Destroy;
|
|
|
|
begin
|
|
|
|
FClaims.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTDictionaryObject.GetItem(const Index: string): string;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2017-07-12 00:31:49 +02:00
|
|
|
if not FClaims.TryGetValue(index, Result) then
|
2016-05-18 18:21:43 +02:00
|
|
|
Result := '';
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTDictionaryObject.GetItemAsDateTime(const Index: string): TDateTime;
|
2016-05-18 18:21:43 +02:00
|
|
|
var
|
|
|
|
lIntValue: Int64;
|
|
|
|
begin
|
2019-07-24 16:12:05 +02:00
|
|
|
Result := -693594;
|
|
|
|
if Trim(Items[index]) <> EmptyStr then
|
|
|
|
begin
|
|
|
|
if not TryStrToInt64(Items[index], lIntValue) then
|
|
|
|
raise Exception.Create('Item cannot be converted as Unix Epoch');
|
|
|
|
Result := UnixToDateTime(lIntValue, False);
|
|
|
|
end;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWTDictionaryObject.Keys: TArray<string>;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
Result := FClaims.Keys.ToArray;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWTDictionaryObject.SetItem(const Index, Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2017-07-12 00:31:49 +02:00
|
|
|
FClaims.AddOrSetValue(index, Value);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWTDictionaryObject.SetItemAsDateTime(const Index: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
const Value: TDateTime);
|
|
|
|
begin
|
2017-07-12 00:31:49 +02:00
|
|
|
Items[index] := IntToStr(DateTimeToUnix(Value, False));
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
2017-07-05 00:17:46 +02:00
|
|
|
{ TJWTCustomClaims }
|
|
|
|
|
|
|
|
function TJWTCustomClaims.AsCustomData: TMVCCustomData;
|
|
|
|
begin
|
|
|
|
Result := TMVCCustomData.Create(FClaims);
|
|
|
|
end;
|
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
{ TJWT }
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
function TJWT.CheckExpirationTime(Payload: TJDOJSONObject; out Error: string): Boolean;
|
2016-05-21 21:56:12 +02:00
|
|
|
var
|
|
|
|
lIntValue: Int64;
|
|
|
|
lValue: string;
|
2017-07-12 00:31:49 +02:00
|
|
|
lExpirationTimeAsDateTime: TDateTime;
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
2019-07-19 19:12:31 +02:00
|
|
|
if not Payload.Contains(TJWTRegisteredClaimNames.ExpirationTime) then
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
|
|
|
Error := TJWTRegisteredClaimNames.ExpirationTime + ' not set';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
lValue := Payload.S[TJWTRegisteredClaimNames.ExpirationTime];
|
2016-05-21 21:56:12 +02:00
|
|
|
if not TryStrToInt64(lValue, lIntValue) then
|
|
|
|
begin
|
|
|
|
Error := TJWTRegisteredClaimNames.ExpirationTime + ' is not an integer';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
lExpirationTimeAsDateTime := UnixToDateTime(lIntValue, False);
|
|
|
|
if lExpirationTimeAsDateTime <= Now - FLeewaySeconds * OneSecond then
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
|
|
|
Error := 'Token expired';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
function TJWT.CheckIssuedAt(Payload: TJDOJSONObject; out Error: string): Boolean;
|
2016-05-21 21:56:12 +02:00
|
|
|
var
|
|
|
|
lIntValue: Int64;
|
|
|
|
lValue: string;
|
|
|
|
begin
|
2019-07-19 19:12:31 +02:00
|
|
|
if not Payload.Contains(TJWTRegisteredClaimNames.IssuedAt) then
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
|
|
|
Error := TJWTRegisteredClaimNames.IssuedAt + ' not set';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
lValue := Payload.S[TJWTRegisteredClaimNames.IssuedAt];
|
2016-05-21 21:56:12 +02:00
|
|
|
if not TryStrToInt64(lValue, lIntValue) then
|
|
|
|
begin
|
|
|
|
Error := TJWTRegisteredClaimNames.IssuedAt + ' is not an integer';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
2016-10-11 11:39:54 +02:00
|
|
|
if UnixToDateTime(lIntValue, False) >= Now + FLeewaySeconds * OneSecond then
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
|
|
|
Error := 'Token is issued in the future';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
function TJWT.CheckNotBefore(Payload: TJDOJSONObject; out Error: string): Boolean;
|
2016-05-21 21:56:12 +02:00
|
|
|
var
|
|
|
|
lIntValue: Int64;
|
|
|
|
lValue: string;
|
|
|
|
begin
|
2019-07-19 19:12:31 +02:00
|
|
|
if not Payload.Contains(TJWTRegisteredClaimNames.NotBefore) then
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
|
|
|
Error := TJWTRegisteredClaimNames.NotBefore + ' not set';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
lValue := Payload.S[TJWTRegisteredClaimNames.NotBefore];
|
2016-05-21 21:56:12 +02:00
|
|
|
if not TryStrToInt64(lValue, lIntValue) then
|
|
|
|
begin
|
|
|
|
Error := TJWTRegisteredClaimNames.NotBefore + ' is not an integer';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
2016-10-11 11:39:54 +02:00
|
|
|
if UnixToDateTime(lIntValue, False) >= Now + FLeewaySeconds * OneSecond then
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
|
|
|
Error := 'Token still not valid';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
constructor TJWT.Create(const SecretKey: string; const ALeewaySeconds: Cardinal = 300);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FSecretKey := SecretKey;
|
|
|
|
FRegisteredClaims := TJWTRegisteredClaims.Create;
|
|
|
|
FCustomClaims := TJWTCustomClaims.Create;
|
2017-09-23 15:03:07 +02:00
|
|
|
FHMACAlgorithm := HMAC_HS512;
|
2017-07-12 00:31:49 +02:00
|
|
|
FLeewaySeconds := ALeewaySeconds;
|
2019-07-19 19:12:31 +02:00
|
|
|
FRegClaimsToChecks := [TJWTCheckableClaim.ExpirationTime, TJWTCheckableClaim.NotBefore, TJWTCheckableClaim.IssuedAt];
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TJWT.Destroy;
|
|
|
|
begin
|
|
|
|
FRegisteredClaims.Free;
|
|
|
|
FCustomClaims.Free;
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
function TJWT.GetLiveValidityWindowInSeconds: Cardinal;
|
|
|
|
begin
|
|
|
|
Result := StrToIntDef(FCustomClaims.Items['lvw'], 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TJWT.GetToken: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
var
|
2019-07-19 19:12:31 +02:00
|
|
|
lHeader, lPayload: TJDOJSONObject;
|
2017-07-12 00:31:49 +02:00
|
|
|
lHeaderEncoded, lPayloadEncoded, lToken, lHash: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
lBytes: TBytes;
|
2017-07-12 00:31:49 +02:00
|
|
|
lRegClaimName: string;
|
|
|
|
lCustomClaimName: string;
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2019-07-19 19:12:31 +02:00
|
|
|
lHeader := TJDOJSONObject.Create;
|
2016-05-18 18:21:43 +02:00
|
|
|
try
|
2019-07-19 19:12:31 +02:00
|
|
|
lPayload := TJDOJSONObject.Create;
|
2016-05-18 18:21:43 +02:00
|
|
|
try
|
2020-08-13 17:40:02 +02:00
|
|
|
lHeader.S['alg'] := HMACAlgorithm;
|
2019-07-19 19:12:31 +02:00
|
|
|
lHeader.S['typ'] := 'JWT';
|
2016-05-18 18:21:43 +02:00
|
|
|
for lRegClaimName in TJWTRegisteredClaimNames.Names do
|
|
|
|
begin
|
|
|
|
if FRegisteredClaims.Contains(lRegClaimName) then
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
|
|
|
if (lRegClaimName = TJWTRegisteredClaimNames.ExpirationTime) or
|
|
|
|
(lRegClaimName = TJWTRegisteredClaimNames.NotBefore) or
|
|
|
|
(lRegClaimName = TJWTRegisteredClaimNames.IssuedAt) then
|
2019-07-19 19:12:31 +02:00
|
|
|
lPayload.L[lRegClaimName] := StrToInt64(FRegisteredClaims[lRegClaimName])
|
2016-05-21 21:56:12 +02:00
|
|
|
else
|
2019-07-19 19:12:31 +02:00
|
|
|
lPayload.S[lRegClaimName] := FRegisteredClaims[lRegClaimName];
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
for lCustomClaimName in FCustomClaims.Keys do
|
|
|
|
begin
|
2020-08-13 17:40:02 +02:00
|
|
|
lPayload.S[lCustomClaimName] := FCustomClaims[lCustomClaimName];
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
lHeaderEncoded := URLSafeB64encode(lHeader.ToString, False, IndyTextEncoding_UTF8);
|
|
|
|
lPayloadEncoded := URLSafeB64encode(lPayload.ToString, False, IndyTextEncoding_UTF8);
|
2016-05-18 18:21:43 +02:00
|
|
|
lToken := lHeaderEncoded + '.' + lPayloadEncoded;
|
|
|
|
lBytes := HMAC(HMACAlgorithm, lToken, FSecretKey);
|
2019-07-19 19:12:31 +02:00
|
|
|
lHash := URLSafeB64encode(lBytes, False);
|
2016-05-18 18:21:43 +02:00
|
|
|
Result := lToken + '.' + lHash;
|
|
|
|
finally
|
|
|
|
lPayload.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lHeader.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
function TJWT.IsValidToken(const Token: string; out Header, Payload: TJDOJSONObject; out Error: string): Boolean;
|
2016-05-18 18:21:43 +02:00
|
|
|
var
|
2017-07-12 00:31:49 +02:00
|
|
|
lPieces: TArray<string>;
|
2016-05-18 18:21:43 +02:00
|
|
|
lAlgName: string;
|
|
|
|
begin
|
2017-10-09 16:17:12 +02:00
|
|
|
Result := False;
|
2016-06-23 11:42:16 +02:00
|
|
|
Error := '';
|
2016-05-18 18:21:43 +02:00
|
|
|
lPieces := Token.Split(['.']);
|
|
|
|
if Length(lPieces) <> 3 then
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
|
|
|
Error := 'Invalid Token';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-06-23 11:42:16 +02:00
|
|
|
end;
|
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
Header := TJDOJsonBaseObject.Parse(URLSafeB64Decode(lPieces[0], IndyTextEncoding_UTF8)) as TJDOJSONObject;
|
2016-05-18 18:21:43 +02:00
|
|
|
try
|
2017-09-22 09:33:21 +02:00
|
|
|
if not Assigned(Header) then
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
|
|
|
Error := 'Invalid Token';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-06-23 11:42:16 +02:00
|
|
|
end;
|
2016-05-18 18:21:43 +02:00
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
Payload := TJDOJsonBaseObject.Parse(URLSafeB64Decode(lPieces[1], IndyTextEncoding_UTF8)) as TJDOJSONObject;
|
2016-05-18 18:21:43 +02:00
|
|
|
try
|
2017-09-22 09:33:21 +02:00
|
|
|
if not Assigned(Payload) then
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
|
|
|
Error := 'Invalid Token';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-06-23 11:42:16 +02:00
|
|
|
end;
|
2016-05-18 18:21:43 +02:00
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
if not Header.Contains('alg') then
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
|
|
|
Error := 'Invalid Token';
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-06-23 11:42:16 +02:00
|
|
|
end;
|
2016-05-18 18:21:43 +02:00
|
|
|
|
2019-07-19 19:12:31 +02:00
|
|
|
lAlgName := Header.S['alg'];
|
2016-05-18 19:09:57 +02:00
|
|
|
Result := Token = lPieces[0] + '.' + lPieces[1] + '.' +
|
2019-07-19 19:12:31 +02:00
|
|
|
URLSafeB64encode(HMAC(lAlgName, lPieces[0] + '.' + lPieces[1], FSecretKey), False);
|
2016-05-21 21:56:12 +02:00
|
|
|
|
|
|
|
// if the token is correctly signed and has not been tampered,
|
|
|
|
// let's check it's validity usinf nbf, exp, iat as configured in
|
|
|
|
// the RegClaimsToCheck property
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
if TJWTCheckableClaim.ExpirationTime in RegClaimsToChecks then
|
|
|
|
begin
|
2017-09-22 09:33:21 +02:00
|
|
|
if not CheckExpirationTime(Payload, Error) then
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-06-23 11:42:16 +02:00
|
|
|
end;
|
|
|
|
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
if TJWTCheckableClaim.NotBefore in RegClaimsToChecks then
|
|
|
|
begin
|
2017-09-22 09:33:21 +02:00
|
|
|
if not CheckNotBefore(Payload, Error) then
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-06-23 11:42:16 +02:00
|
|
|
end;
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
if TJWTCheckableClaim.IssuedAt in RegClaimsToChecks then
|
|
|
|
begin
|
2017-09-22 09:33:21 +02:00
|
|
|
if not CheckIssuedAt(Payload, Error) then
|
2016-06-23 11:42:16 +02:00
|
|
|
begin
|
2016-12-30 20:41:55 +01:00
|
|
|
Exit(False);
|
2016-06-23 11:42:16 +02:00
|
|
|
end;
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
finally
|
2017-09-22 09:33:21 +02:00
|
|
|
if not Result then
|
|
|
|
FreeAndNil(Payload);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
finally
|
2017-09-22 09:33:21 +02:00
|
|
|
if not Result then
|
|
|
|
FreeAndNil(Header);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-09-22 09:33:21 +02:00
|
|
|
function TJWT.LoadToken(const Token: string; out Error: string): Boolean;
|
2016-05-18 18:21:43 +02:00
|
|
|
var
|
2017-07-12 00:31:49 +02:00
|
|
|
lPieces: TArray<string>;
|
2019-07-19 19:12:31 +02:00
|
|
|
lJHeader: TJDOJSONObject;
|
|
|
|
lJPayload: TJDOJSONObject;
|
2016-05-18 18:21:43 +02:00
|
|
|
i: Integer;
|
|
|
|
lName: string;
|
|
|
|
j: Integer;
|
|
|
|
lIsRegistered: Boolean;
|
|
|
|
lValue: string;
|
|
|
|
begin
|
2017-10-16 22:57:27 +02:00
|
|
|
lJHeader := nil;
|
|
|
|
lJPayload := nil;
|
2017-09-22 09:33:21 +02:00
|
|
|
Result := IsValidToken(Token, lJHeader, lJPayload, Error);
|
2016-05-18 18:21:43 +02:00
|
|
|
try
|
2017-09-22 09:33:21 +02:00
|
|
|
if not Result then
|
|
|
|
Exit(False);
|
|
|
|
|
|
|
|
lPieces := Token.Split(['.']);
|
|
|
|
// loading data from token into self
|
|
|
|
FHMACAlgorithm := lJHeader.Values['alg'].Value;
|
|
|
|
// registered claims
|
|
|
|
FRegisteredClaims.FClaims.Clear;
|
2016-05-18 18:21:43 +02:00
|
|
|
|
2017-09-22 09:33:21 +02:00
|
|
|
// custom claims
|
|
|
|
FCustomClaims.FClaims.Clear;
|
|
|
|
for i := 0 to lJPayload.Count - 1 do
|
|
|
|
begin
|
|
|
|
lIsRegistered := False;
|
2019-07-19 19:12:31 +02:00
|
|
|
|
2020-08-13 17:40:02 +02:00
|
|
|
lName := lJPayload.Names[i];
|
|
|
|
lValue := lJPayload.Items[i].Value;
|
2017-09-22 09:33:21 +02:00
|
|
|
|
|
|
|
// if is a registered claim, load it in the proper dictionary...
|
|
|
|
for j := 0 to high(TJWTRegisteredClaimNames.Names) do
|
|
|
|
begin
|
|
|
|
if lName = TJWTRegisteredClaimNames.Names[j] then
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
2017-09-22 09:33:21 +02:00
|
|
|
FRegisteredClaims.FClaims.AddOrSetValue(lName, lValue);
|
|
|
|
lIsRegistered := True;
|
|
|
|
Break;
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
end;
|
2017-09-22 09:33:21 +02:00
|
|
|
if not lIsRegistered then
|
|
|
|
FCustomClaims.FClaims.AddOrSetValue(lName, lValue);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
2017-09-22 09:33:21 +02:00
|
|
|
|
|
|
|
FCustomClaims.FClaims.TrimExcess;
|
|
|
|
FRegisteredClaims.FClaims.TrimExcess;
|
2016-05-18 18:21:43 +02:00
|
|
|
finally
|
2017-09-22 09:33:21 +02:00
|
|
|
FreeAndNil(lJHeader);
|
|
|
|
FreeAndNil(lJPayload);
|
2016-05-18 18:21:43 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-05-21 21:56:12 +02:00
|
|
|
procedure TJWT.SetChecks(const Value: TJWTCheckableClaims);
|
|
|
|
begin
|
|
|
|
FRegClaimsToChecks := Value;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWT.SetHMACAlgorithm(const Value: string);
|
2016-05-18 18:21:43 +02:00
|
|
|
begin
|
|
|
|
FHMACAlgorithm := Value;
|
|
|
|
end;
|
|
|
|
|
2017-07-12 00:31:49 +02:00
|
|
|
procedure TJWT.SetLiveValidityWindowInSeconds(const Value: Cardinal);
|
2016-05-21 21:56:12 +02:00
|
|
|
begin
|
2017-07-12 00:31:49 +02:00
|
|
|
FCustomClaims.Items['lvw'] := Value.ToString;
|
2016-05-21 21:56:12 +02:00
|
|
|
end;
|
|
|
|
|
2016-05-18 18:21:43 +02:00
|
|
|
end.
|