JWT Client
TsgcHTTP_JWT_Client — JSON Web Token client helper for any sgcWebSockets HTTP component.
TsgcHTTP_JWT_Client — JSON Web Token client helper for any sgcWebSockets HTTP component.
Configure which openSSL libraries you will use when using JWT client.
TsgcHTTP_JWT_Client| Standards & specs | JSON Web Token — RFC 7519 · JSON Web Signature — RFC 7515 · JSON Web Encryption — RFC 7516 |
| Component class | TsgcHTTP_JWT_Client (unit sgcHTTP_JWT_Client) |
| Frameworks | VCL, FireMonkey, Lazarus / FPC |
| Platforms | Windows, macOS, Linux, iOS, Android |
The principal published / public properties used to configure and drive the component. Consult the online help for the full list.
JWTOptions | Token configuration: algorithm, signing key material, standard claims and custom header/payload entries. |
Version | Read-only string exposing the sgcWebSockets library version. |
The principal public methods exposed by the component.
Start() | Signs the configured JWT and delivers it as a Bearer token to the host HTTP / WebSocket client. |
Sign() | Builds, signs and returns the encoded JWT (header.payload.signature) as a single compact-serialization string. |
Drop the component on a form, configure the properties below and activate it. The snippet that follows shows the typical WebSocket Client and JWT configuration sourced from the online help.
oClient := TsgcWebSocketClient.Create(nil); oClient.URL := 'ws://www.esegece.com:2052'; <br/> oJWT := TsgcHTTP_JWT_Client.Create(nil); oJWT.JWTOptions.Header.alg := jwtRS256; oJWT.JWTOptions.Payload.sub := '1234567890'; oJWT.JWTOptions.Payload.iat := 1516239022; <br/> oClient.Authentication.Enabled := True; oClient.Authentication.URL.Enabled := False; oClient.Authentication.Token.Enabled := True; oClient.Authentication.Token.JWT := oJWT; oClient.Active := True;
TsgcWebSocketClient oClient = new TsgcWebSocketClient(); oClient->URL = "ws://www.esegece.com:2052"; <br/> TsgcHTTP_JWT_Client oJWT = new TsgcHTTP_JWT_Client(); oJWT->JWTOptions->Header.alg = jwtRS256; oJWT->JWTOptions->Payload.sub = "1234567890"; oJWT->JWTOptions->Payload.iat = 1516239022; <br/> oClient->Authentication->Enabled = true; oClient->Authentication->URL->Enabled = false; oClient->Authentication->Token->Enabled = true; oClient->Authentication->Token->JWT = oJWT; oClient->Active = true;
TsgcWebSocketClient oClient = new TsgcWebSocketClient(); oClient.URL = "ws://www.esegece.com:2052"; <br/> TsgcHTTP_JWT_Client oJWT = new TsgcHTTP_JWT_Client(); oJWT.JWTOptions.Header.alg = jwtRS256; oJWT.JWTOptions.Payload.sub = "1234567890"; oJWT.JWTOptions.Payload.iat = 1516239022; <br/> oClient.Authentication->Enabled = true; oClient.Authentication.URL.Enabled = false; oClient.Authentication.Token.Enabled = true; oClient.Authentication.Token.JWT = oJWT; oClient.Active = true;
The following scenarios are lifted verbatim from the online help. Each shows the configuration and method calls needed to drive the component through a specific real-world flow.
The Header and Payload properties contains the most common headers used to generate a JWT, but you can add more headers calling the method AddKeyValue and passing the Key and Value as parameters.
Header.AddKeyValue('name', 'John Smith');
Header->AddKeyValue("name", "John Smith");
Header.AddKeyValue("name", "John Smith");
You can create JWT Signatures manually to use on applications that doesn't make use of WebSocket or HTTP Protocol, or if you are using components from third-parties applications and you only need the JWT Token.
oJWT := TsgcHTTP_JWT_Client.Create(nil); // ... header oJWT.JWTOptions.Header.alg := jwtHS256; oJWT.JWTOptions.Algorithms.HS.Secret := '79F66F1E-E998-436B-8A0A-3E5DEFA4FD9E'; // ... payload oJWT.JWTOptions.Payload.jti := '9B66FB94-B761-42B1-A1AF-3C44233DBE87'; oJWT.JWTOptions.Payload.iat := 1630925658; oJWT.JWTOptions.Payload.iss := '2886EC7547B7BA6A9009'; oJWT.JWTOptions.Payload.exp := 1630933158; // ... custom payload values oJWT.JWTOptions.Payload.ClearKeyValues; oJWT.JWTOptions.Payload.AddKeyValue('origin', 'www.yourwebsite.com'); oJWT.JWTOptions.Payload.AddKeyValue('ip', '69.39.46.178'); // ... get JWT Token ShowMessage(oJWT.Sign);
TsgcHTTP_JWT_Client oJWT = new TsgcHTTP_JWT_Client(this); // ... header oJWT->JWTOptions->Header->alg = jwtHS256; oJWT->JWTOptions->Algorithms->HS->Secret = "79F66F1E-E998-436B-8A0A-3E5DEFA4FD9E"; // ... payload oJWT->JWTOptions->Payload->jti = "9B66FB94-B761-42B1-A1AF-3C44233DBE87"; oJWT->JWTOptions->Payload->iat = 1630925658; oJWT->JWTOptions->Payload->iss = "2886EC7547B7BA6A9009"; oJWT->JWTOptions->Payload->exp = 1630933158; // ... custom payload values oJWT->JWTOptions->Payload->ClearKeyValues; oJWT->JWTOptions->Payload->AddKeyValue("origin", "www->yourwebsite->com"); oJWT->JWTOptions->Payload->AddKeyValue("ip", "69.39.46.178"); // ... get JWT Token ShowMessage(oJWT->Sign());
TsgcHTTP_JWT_Client oJWT = new TsgcHTTP_JWT_Client(); // ... header oJWT.JWTOptions.Header.alg = jwtHS256; oJWT.JWTOptions.Algorithms.HS.Secret = "79F66F1E-E998-436B-8A0A-3E5DEFA4FD9E"; // ... payload oJWT.JWTOptions.Payload.jti = "9B66FB94-B761-42B1-A1AF-3C44233DBE87"; oJWT.JWTOptions.Payload.iat = 1630925658; oJWT.JWTOptions.Payload.iss = "2886EC7547B7BA6A9009"; oJWT.JWTOptions.Payload.exp = 1630933158; // ... custom payload values oJWT.JWTOptions.Payload.ClearKeyValues; oJWT.JWTOptions.Payload.AddKeyValue("origin", "www.yourwebsite.com"); oJWT.JWTOptions.Payload.AddKeyValue("ip", "69.39.46.178"); // ... get JWT Token MessageBox.Show(oJWT.Sign());
TsgcHTTP2Client and TsgcHTTP1Client allows the use of JWT when connecting to HTTP/2 servers, just create a new JWT client and assign to Authentication.Token.JWT property.
oHTTP := TsgcHTTP2Client.Create(nil); <br/> oJWT := TsgcHTTP_JWT_Client.Create(nil); oJWT.JWTOptions.Header.alg := jwtRS256; oJWT.JWTOptions.Payload.sub := '1234567890'; oJWT.JWTOptions.Payload.iat := 1516239022; <br/> oHTTP.Authentication.Token.JWT := oHTTP; oHTTP.Get('https://your.api.com');
TsgcHTTP2Client oHTTP = new TsgcHTTP2Client(); <br/> TsgcHTTP_JWT_Client oJWT = new TsgcHTTP_JWT_Client(); oJWT->JWTOptions->Header->alg = jwtRS256; oJWT->JWTOptions->Payload->sub = "1234567890"; oJWT->JWTOptions->Payload->iat = 1516239022; <br/> oHTTP->Authentication->Token->JWT = oHTTP; oHTTP->Get("https://your.api.com");
TsgcHTTP2Client oHTTP = new TsgcHTTP2Client(); <br/> TsgcHTTP_JWT_Client oJWT = new TsgcHTTP_JWT_Client(); oJWT.JWTOptions.Header.alg = jwtRS256; oJWT.JWTOptions.Payload.sub = "1234567890"; oJWT.JWTOptions.Payload.iat = 1516239022; <br/> oHTTP.Authentication.Token.JWT = oHTTP; oHTTP.Get("https://your.api.com");
Every external claim links back to a primary source. The online-help references decode the canonical deep-link the company maintains for this component.
Demos\20.HTTP_Protocol\05.JWT