paolo-rossi / delphi-jose-jwt

Delphi implementation of JOSE (JSON Object Signing and Encryption) and JWT (JSON Web Token)
Apache License 2.0
448 stars 176 forks source link

Shortcut to TJOSE class functions inside TJWT #56

Closed viniciussanchez closed 2 years ago

viniciussanchez commented 2 years ago

Today to generate a token, you have to make 2 uses, as the documentation says. And I have to do it like this:

function GetToken: string;
var
  LToken: TJWT;
begin
  LToken := TJWT.Create;
  try
    LToken.Claims.NotBefore := Now;
    LToken.Claims.SetClaimOfType('A', 'B');
    Result := TJOSE.SHA256CompactToken('key', LToken);
  finally
    LToken.Free;
  end;
end;

What do you think about creating a shortcut inside the TJWT class that calls TJOSE, looking like this:

function GetToken: string;
var
  LToken: TJWT;
begin
  LToken := TJWT.Create;
  try
    LToken.Claims.NotBefore := Now;
    LToken.Claims.SetClaimOfType('A', 'B');
    Result := LToken.SHA256CompactToken('key');
  finally
    LToken.Free;
  end;
end;

I could do this with all the functions of the TJOSE class. With that I would need to make just one uses to generate the token and it would be simpler and more practical.

paolo-rossi commented 2 years ago

Hello @viniciussanchez

I don't like adding a dependency to TJWT knowing signing algorithms, but now I introduced new interfaces to simplify the token creation:

var
  LResult: string;
begin
  LResult := TJOSEProcess.New
    .SetIssuer('Delphi JOSE Library')
    .SetIssuedAt(Now)
    .SetExpiration(Now + 1)
    .SetAlgorithm(LAlg)
    .SetKey(TJOSEAlgorithmId.HS256)
    .Build
    .GetCompactToken
  ;

  memoCompact.Lines.Add(LResult);
end;

I hope that this new commit solve the issue.

Paolo.