Skip to content

Commit

Permalink
Added method SetCustomClaim(const AName: string; const AValue: TValue…
Browse files Browse the repository at this point in the history
…) to the IJOSEProducerBuilder interface
  • Loading branch information
paolo-rossi committed Jul 9, 2022
1 parent 44c54d9 commit 2480e0b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Source/Common/JOSE.Types.JSON.pas
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ EJSONConversionException = class(Exception);
TJSONArray = System.JSON.TJSONArray;

TJSONUtils = class
private
class procedure SetJSONRttiValue(const AName: string; const AValue: TValue; AJSON: TJSONObject); overload;
public
class function ToJSON(AJSONValue: TJSONValue): string; static;
class function CheckPair(const AName: string; AJSON: TJSONObject): Boolean;
Expand All @@ -62,6 +60,7 @@ TJSONUtils = class
class function GetJSONValueAsEpoch(const AName: string; AJSON: TJSONObject): TDateTime;

class procedure SetJSONValue(const AName: string; AValue: TJSONValue; AJSON: TJSONObject); overload;
class procedure SetJSONRttiValue(const AName: string; const AValue: TValue; AJSON: TJSONObject); overload;
class procedure SetJSONValueFrom<T>(const AName: string; const AValue: T; AJSON: TJSONObject);

class procedure RemoveJSONNode(const AName: string; AJSON: TJSONObject);
Expand Down
6 changes: 6 additions & 0 deletions Source/JOSE/JOSE.Core.JWT.pas
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ TJWTClaims = class(TJOSEBase)
public
constructor Create; virtual;

procedure SetClaim(const AName: string; const AValue: TValue);
procedure SetClaimOfType<T>(const AName: string; const AValue: T);
function GenerateJWTId(ANumberOfBytes: Integer = 16): string;

Expand Down Expand Up @@ -221,6 +222,11 @@ destructor TJWT.Destroy;

{ TJWTClaims }

procedure TJWTClaims.SetClaim(const AName: string; const AValue: TValue);
begin
TJSONUtils.SetJSONRttiValue(AName, AValue, FJSON);
end;

procedure TJWTClaims.SetClaimOfType<T>(const AName: string; const AValue: T);
begin
AddPairOfType<T>(AName, AValue);
Expand Down
9 changes: 9 additions & 0 deletions Source/JOSE/JOSE.Producer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface

uses
System.SysUtils,
System.Rtti,
JOSE.Types.Bytes,
JOSE.Core.Base,
JOSE.Core.Parts,
Expand Down Expand Up @@ -67,6 +68,7 @@ TJOSEProducer = class(TInterfacedObject, IJOSEProducer)
function SetJWTId(const AValue: string): IJOSEProducerBuilder;
function SetNotBefore(AValue: TDateTime): IJOSEProducerBuilder;
function SetSubject(const AValue: string): IJOSEProducerBuilder;
function SetCustomClaim(const AName: string; const AValue: TValue): IJOSEProducerBuilder;

function SetKey(const AKey: TJOSEBytes): IJOSEProducerBuilder;
function SetKeyPair(const APublicKey, APrivateKey: TJOSEBytes): IJOSEProducerBuilder;
Expand Down Expand Up @@ -97,6 +99,7 @@ TJOSEProducerBuilder = class(TInterfacedObject, IJOSEProducerBuilder)
function SetJWTId(const AValue: string): IJOSEProducerBuilder;
function SetNotBefore(AValue: TDateTime): IJOSEProducerBuilder;
function SetSubject(const AValue: string): IJOSEProducerBuilder;
function SetCustomClaim(const AName: string; const AValue: TValue): IJOSEProducerBuilder;

function SetKey(const AKey: TJOSEBytes): IJOSEProducerBuilder;
function SetKeyPair(const APublicKey, APrivateKey: TJOSEBytes): IJOSEProducerBuilder;
Expand Down Expand Up @@ -217,6 +220,12 @@ function TJOSEProducerBuilder.SetAudience(const AValue: TArray<string>): IJOSEPr
Result := Self;
end;

function TJOSEProducerBuilder.SetCustomClaim(const AName: string; const AValue: TValue): IJOSEProducerBuilder;
begin
GetJWT.Claims.SetClaim(AName, AValue);
Result := Self;
end;

function TJOSEProducerBuilder.SetExpiration(AValue: TDateTime): IJOSEProducerBuilder;
begin
GetJWT.Claims.Expiration := AValue;
Expand Down

0 comments on commit 2480e0b

Please sign in to comment.