Delphi implementation of JWT (JSON Web Token) and the JOSE (JSON Object Signing and Encryption) specification suite. This library supports the JWS (JWE is support planned) compact serializations with several JOSE algorithms.
The HMAC algorithm uses OpenSSL through the Indy library, so in order to generate the token you should have the OpenSSL DLLs in your server system. The client doesn't have to generate or verify the token (using HMAC) so on the client-side there's no need for the OpenSSL DLLs. You can download the OpenSSL package at the fulgan website (keep in mind to always update to the latest version)
JOSE is a standard that provides a general approach to signing and encryption of any content. JOSE consists of several RFC:
- JWT (JSON Web Token) - describes representation of claims encoded in JSON
- JWS (JSON Web Signature) - describes producing and handling signed messages
- JWE (JSON Web Encryption) - describes producing and handling encrypted messages
- JWA (JSON Web Algorithms) - describes cryptographic algorithms used in JOSE
- JWK (JSON Web Key) - describes format and handling of cryptographic keys in JOSE
- Token serialization
- Token deserialization
- Claims validation
exp
,iat
,nbf
claims validatation - supportedaud
,iss
,sub
claims validatation - planned
- Sign algorithms
NONE
,HS256
,HS384
,HS512
algorithms - supported
- Encryption algorithms
RS256
,RS384
,RS512
algorithms - plannedES256
,ES384
,ES512
,PS256
,PS384
,PS512
algorithms - not (yet) planned- Not affected by the
None
algorithm vulnerability
This library is not susceptible to the recently discussed encryption vulnerability.
- TMS XData and TMS Sparkle. Read the blog post by Wagner R. Landgraf (he is also a contributor of this project)
- The WiRL RESTful Library for Delphi
- Token validation:
aud
,iss
,sub
- RSA algorithms implementation
- Easy creation of
TJWTClaims
derived classes
- D2010+ porting
- Unit Tests
- More examples
This library has been tested with Delphi 10.1 Berlin, Delphi 10 Seattle and Delphi XE8 but with a minimum amount of work it should compile with D2010 and higher
This library has no dependencies on external libraries/units.
Delphi units used:
- System.JSON (DXE6+) (available on earlier Delphi versions as Data.DBXJSON)
- System.Rtti (D2010+)
- System.Generics.Collections (D2009+)
- System.NetEncoding (DXE7+)
- Indy units: IdHMAC, IdHMACSHA1, IdSSLOpenSSL, IdHash (please use latest version from svn)
Simply add the source path "Source/Common" and Source/JOSE" to your Delphi project path and.. you are good to go!
To create a token simple create an instance of the TJWT
class and set the properties (claims).
The easiest way to serialize, deserialize, verify a token is to use the TJOSE
utility class.
var
LToken: TJWT;
begin
LToken := TJWT.Create;
try
// Token claims
LToken.Claims.IssuedAt := Now;
LToken.Claims.Expiration := Now + 1;
LToken.Claims.Issuer := 'WiRL REST Library';
// Signing and Compact format creation
mmoCompact.Lines.Add(TJOSE.SHA256CompactToken('secret', LToken));
// Header and Claims JSON representation
mmoJSON.Lines.Add(LToken.Header.JSON.ToJSON);
mmoJSON.Lines.Add(LToken.Claims.JSON.ToJSON);
finally
LToken.Free;
end;
Unpacking and verifying tokens is simple. You have to pass the key (secret) and the token compact format to the TJOSE.Verify
class function
var
LKey: TJWK;
LToken: TJWT;
begin
LKey := TJWK.Create('secret');
// Unpack and verify the token
LToken := TJOSE.Verify(LKey, 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJXaVJMIn0.w3BAZ_GwfQYY6dkS8xKUNZ_sOnkDUMELxBN0mKKNhJ4');
if Assigned(LToken) then
begin
try
if LToken.Verified then
mmoJSON.Lines.Add('Token signature is verified')
else
mmoJSON.Lines.Add('Token signature is not verified')
finally
LToken.Free;
end;
end;
end;