TsgcHTTP_JWT_ClientMethods › Sign

Sign Method

Builds, signs and returns the encoded JWT (header.payload.signature) as a single compact-serialization string.

Syntax

function Sign: string;

Return Value

The signed JWT in RFC 7519 compact serialization (base64url(Header).base64url(Payload).base64url(Signature)) (string).

Remarks

Generates the token from the current JWTOptions: encodes Header and Payload as base64url-encoded JSON, signs the "header.payload" input with the algorithm selected in Header.alg (HMAC with Algorithms.HS.Secret, or RSA/ECDSA with Algorithms.RS.PrivateKey / Algorithms.ES.PrivateKey) and appends the signature. When RefreshTokenAfter > 0 the method also refreshes Payload.iat to the current Unix time and recomputes Payload.exp. Use Sign directly when you need the raw token string (for third-party libraries or manual headers); use Start when the component is attached to an sgc HTTP or WebSocket client.

Example


oJWT := TsgcHTTP_JWT_Client.Create(nil);
oJWT.JWTOptions.Header.alg := jwtHS256;
oJWT.JWTOptions.Algorithms.HS.Secret := '79F66F1E-E998-436B-8A0A-3E5DEFA4FD9E';
oJWT.JWTOptions.Payload.jti := '9B66FB94-B761-42B1-A1AF-3C44233DBE87';
oJWT.JWTOptions.Payload.iat := 1630925658;
oJWT.JWTOptions.Payload.iss := '2886EC7547B7BA6A9009';
oJWT.JWTOptions.Payload.exp := 1630933158;
oJWT.JWTOptions.Payload.AddKeyValue('origin', 'www.yourwebsite.com');
ShowMessage(oJWT.Sign);

Back to Methods