CompactJwt
public final class CompactJwt : Sendable
CompactJwt is a utility class responsible to perform simple, and specific JWT operation within MFA modules for JWT-related operations. Provides methods for generating and validating JWTs using the HS256 algorithm.
-
Signs given claims using the HS256 algorithm.
Throws
JwtError.invalidSecretif the secret is empty or invalid.JwtError.signingFailedif there is an error signing the JWT.Declaration
Swift
public static func signJwtClaims(base64Secret: String, claims: [String : Any]) throws -> StringParameters
base64SecretThe base64-encoded secret key.
claimsThe claims to include in the JWT.
Return Value
The JWT string.
-
Checks if a string is a valid JWT and contains the required fields in its payload.
Declaration
Swift
public static func canParseJwt(_ jwt: String, requiredFields: [String] = []) -> BoolParameters
jwtThe JWT string to validate.
requiredFieldsAn array of field names to check in the payload.
Return Value
trueif the JWT is valid and contains all required fields,falseotherwise. -
Verifies the signature of a JWT using the provided secret.
Throws
JwtError.invalidSecretif the secret is empty or invalid.JwtError.invalidFormatif the JWT format is invalid.JwtError.signingFailedif there is an error during verification.Declaration
Swift
public static func verifyJwtSignature(_ jwt: String, base64Secret: String) throws -> BoolParameters
jwtThe JWT string to verify.
base64SecretThe base64-encoded secret key used for verification.
Return Value
trueif the signature is valid,falseotherwise.
-
Parses a JWT string and extracts its payload claims.
Throws
JwtError.invalidFormatif the JWT format is invalid.JwtError.invalidPayloadif the payload cannot be parsed.Declaration
Swift
public static func parseJwtClaims(_ jwt: String) throws -> [String : Any]Parameters
jwtThe JWT string to parse.
Return Value
A dictionary containing the payload claims.
-
Signs given claims using an asymmetric key algorithm.
Throws
JwtError.signingFailedif there is an error signing the JWT.Declaration
Swift
public static func sign(claims: [String : Any], privateKey: SecKey, publicKey: SecKey?, algorithm: SecKeyAlgorithm, kid: String) throws -> StringParameters
claimsThe claims to include in the JWT.
privateKeyThe private key used to sign the JWT.
algorithmThe algorithm to use for signing (e.g., ES256).
kidThe key ID to include in the JWT header.
Return Value
The JWT string.
View on GitHub