JWTVerifier

public struct JWTVerifier

A struct that will be used to verify the signature of a JWT is valid for the provided Header and Claims. For RSA and ECDSA, the provided key should be a .utf8 encoded PEM String.

Usage Example:

let pemString = """
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdlatRjRjogo3WojgGHFHYLugd
UWAY9iR3fy4arWNA1KoS8kVw33cJibXr8bvwUAUparCwlvdbH6dvEOfou0/gCFQs
HUfQrSDv+MuSUMAe8jzKE4qW+jK+xQU9a03GUnKHkkle+Q0pX/g6jXZ7r1/xAK5D
o2kQ+X5xK9cipRgEKwIDAQAB
-----END PUBLIC KEY-----
"""
let signedJWT = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS2l0dXJhIn0.o2Rv_w1W6qfkldgb6FwzC3tAFEzo7WyYcLyykijCEqDbW8A7TwoFev85KGo_Bi7eNaSgZ6Q8jgkA31r8EDQWtSRg3_o5Zlq-ZCndyVeibgbyM2BMVUGcGzkUD2ikARfnb6GNGHr2waVeFSDehTN8WTLl0mGFxUE6wx5ZugR7My0"
struct MyClaims: Claims {
   var name: String
}
let jwt = JWT(claims: MyClaims(name: "Kitura"))
let publicKey = pemString.data(using: .utf8)!
let jwtVerifier = JWTVerifier.rs256(publicKey: publicKey)
let verified: Bool = jwt.verify(signedJWT, using: jwtVerifier)
  • Initialize a JWTVerifier using the RSA 256 bits algorithm and the provided publicKey.

    Declaration

    Swift

    public static func rs256(publicKey: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN PUBLIC KEY header.

  • Initialize a JWTVerifier using the RSA 384 bits algorithm and the provided publicKey.

    Declaration

    Swift

    public static func rs384(publicKey: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN PUBLIC KEY header.

  • Initialize a JWTVerifier using the RSA 512 bits algorithm and the provided publicKey.

    Declaration

    Swift

    public static func rs512(publicKey: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN PUBLIC KEY header.

  • Initialize a JWTVerifier using the RSA 256 bits algorithm and the provided certificate.

    Declaration

    Swift

    public static func rs256(certificate: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN CERTIFICATE header.

  • Initialize a JWTVerifier using the RSA 384 bits algorithm and the provided certificate.

    Declaration

    Swift

    public static func rs384(certificate: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN CERTIFICATE header.

  • Initialize a JWTVerifier using the RSA 512 bits algorithm and the provided certificate.

    Declaration

    Swift

    public static func rs512(certificate: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN CERTIFICATE header.

  • Initialize a JWTSigner using the HMAC 256 bits algorithm and the provided privateKey.

    Declaration

    Swift

    public static func hs256(key: Data) -> JWTVerifier

    Parameters

    key

    The HMAC symmetric password data.

  • Initialize a JWTSigner using the HMAC 384 bits algorithm and the provided privateKey.

    Declaration

    Swift

    public static func hs384(key: Data) -> JWTVerifier

    Parameters

    key

    The HMAC symmetric password data.

  • Initialize a JWTSigner using the HMAC 512 bits algorithm and the provided privateKey.

    Declaration

    Swift

    public static func hs512(key: Data) -> JWTVerifier

    Parameters

    key

    The HMAC symmetric password data.

  • Initialize a JWTVerifier using the ECDSA SHA 256 algorithm and the provided public key.

    Declaration

    Swift

    public static func es256(publicKey: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN PUBLIC KEY header.

  • Initialize a JWTVerifier using the ECDSA SHA 384 algorithm and the provided public key.

    Declaration

    Swift

    public static func es384(publicKey: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN PUBLIC KEY header.

  • Initialize a JWTVerifier using the ECDSA SHA 512 algorithm and the provided public key.

    Declaration

    Swift

    public static func es512(publicKey: Data) -> JWTVerifier

    Parameters

    publicKey

    The UTF8 encoded PEM public key, with a BEGIN PUBLIC KEY header.

  • Initialize a JWTVerifier that will always return true when verifying the JWT. This is equivelent to using the none alg header.

    Declaration

    Swift

    public static let none = JWTVerifier(verifierAlgorithm: NoneAlgorithm())