Skip to content
Permalink
Browse files

minor JWT refactoring - no functional change

  • Loading branch information
Arnaud Bouchez
Arnaud Bouchez committed Feb 6, 2020
1 parent 1d979f7 commit d6577b4788c7baaa8e5ed46397ee51a764a4e1ac
Showing with 14 additions and 9 deletions.
  1. +12 −7 SynCrypto.pas
  2. +1 −1 SynFPCLinux.pas
  3. +1 −1 SynopseCommit.inc
@@ -2594,15 +2594,15 @@ EJWTException = class(ESynException);
TJWTContent = record
/// store latest Verify() result
result: TJWTResult;
/// set of registered claims, as stored in the JWT payload
/// set of known/registered claims, as stored in the JWT payload
claims: TJWTClaims;
/// match TJWTAbstract.Audience[] indexes for reg[jrcAudience]
audience: set of 0..15;
/// registered claims UTF-8 values, as stored in the JWT payload
/// known/registered claims UTF-8 values, as stored in the JWT payload
// - e.g. reg[jrcSubject]='1234567890' and reg[jrcIssuer]='' for
// $ {"sub": "1234567890","name": "John Doe","admin": true}
reg: array[TJWTClaim] of RawUTF8;
/// unregistered public/private claim values, as stored in the JWT payload
/// custom/unregistered claim values, as stored in the JWT payload
// - registered claims will be available from reg[], not in this field
// - e.g. data.U['name']='John Doe' and data.B['admin']=true for
// $ {"sub": "1234567890","name": "John Doe","admin": true}
@@ -14564,16 +14564,19 @@ procedure TJWTAbstract.Parse(const Token: RawUTF8; var JWT: TJWTContent;
aud: TDocVariantData;
tok: PAnsiChar absolute Token;
begin
JWT.data.InitFast(0,dvObject);
// 0. initialize parsing
Finalize(JWT.reg);
JWT.data.InitFast(0,dvObject); // custom claims
byte(JWT.claims) := 0;
word(JWT.audience) := 0;
toklen := length(Token);
if (toklen=0) or (self=nil) then begin
JWT.result := jwtNoToken;
exit;
end;
// 1. validate the header (including algorithm "alg" verification)
JWT.result := jwtInvalidAlgorithm;
if joHeaderParse in fOptions then begin
if joHeaderParse in fOptions then begin // slower parsing
headerlen := PosExChar('.',Token);
if (headerlen=0) or (headerlen>512) then
exit;
@@ -14582,11 +14585,12 @@ procedure TJWTAbstract.Parse(const Token: RawUTF8; var JWT: TJWTContent;
if not head[0].Idem(fAlgorithm) or
((head[1].Value<>nil) and not head[1].Idem('JWT')) then
exit;
end else begin
headerlen := length(fHeaderB64); // fast direct compare of fHeaderB64 (including "alg")
end else begin // fast direct compare of fHeaderB64 (including "alg")
headerlen := length(fHeaderB64);
if (toklen<=headerlen) or not CompareMem(pointer(fHeaderB64),tok,headerlen) then
exit;
end;
// 2. extract the payload
JWT.result := jwtWrongFormat;
if toklen>JWT_MAXSIZE Then
exit;
@@ -14600,6 +14604,7 @@ procedure TJWTAbstract.Parse(const Token: RawUTF8; var JWT: TJWTContent;
Base64URIToBin(tok+headerlen,payloadend-headerlen-1,RawByteString(payload));
if payload='' then
exit;
// 3. decode the payload into JWT.reg[]/JWT.claims (known) and JWT.data (custom)
P := GotoNextNotSpace(pointer(payload));
if P^<>'{' then
exit;
@@ -412,7 +412,7 @@ procedure QueryPerformanceMicroSeconds(out Value: Int64);
var r : TTimeSpec;
begin
clock_gettime(CLOCK_MONOTONIC,@r);
value := r.tv_nsec div C_THOUSAND+r.tv_sec*C_MILLION; // as microseconds
value := PtrUInt(r.tv_nsec) div C_THOUSAND+r.tv_sec*C_MILLION; // as microseconds
end;

procedure GetNowUTCSystem(out result: TSystemTime);
@@ -1 +1 @@
'1.18.5449'
'1.18.5450'

0 comments on commit d6577b4

Please sign in to comment.
You can’t perform that action at this time.