Swiftpack.co - eugeneego/EEJWT-Swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by eugeneego.
eugeneego/EEJWT-Swift 1.0.0
Lightweight JSON Web Tokens library for iOS/macOS written in Swift.
⭐️ 3
🕓 1 week ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/eugeneego/EEJWT-Swift.git", from: "1.0.0")


Lightweight JSON Web Tokens library for iOS/macOS written in Swift.

The main goal of the library to be simple as possible:

  • just one small file with source code
  • no external dependencies, only iOS/macOS API
  • no fixed header and claims for tokens

Suppoted JWT algorithms:

  • HS256, HS384, HS512
  • RS256, RS384, RS512
  • PS256, PS384, PS512
  • ES256, ES384, ES512


Swift Package Manager

Add EEJWT package to the dependencies in your Package.swift file.

.package(url: "https://github.com/eugeneego/EEJWT-Swift.git", from: "1.0.0")


Just copy Sources/EEJWT/EEJWT.swift file to your project.



let jwt = JWT()

struct Header: JWTHeader {
    var typ: String?
    var alg: String = ""

struct Claims: Encodable {
    var iss: String?
    var sub: String?
    var aud: [String]?
    var exp: Date?
    var nbf: Date?
    var iat: Date?
    var jti: String?

// HMAC and dictionary payload
let algHS = JWT.Algorithm.hs256(key: Data("test".utf8))
let tokenHS = try jwt.sign(header: ["test": "Test"], claims: ["iss": "Issuer", "sub": "123"], algorithm: algHS)

// RSA with encodable payload
let privateKeyRS =
    -----BEGIN PRIVATE KEY-----
    ...private key data here...
    -----END PRIVATE KEY-----
let algRS = JWT.Algorithm.rs384(privateKey: privateKeyRS, publicKey: "")
let tokenRS = try jwt.sign(header: Header(), claims: Claims(iss: "Issuer", sub: "123"), algorithm: algRS)

// Elliptic curve with raw data payload
let privateKeyES =
    -----BEGIN PRIVATE KEY-----
    ...private key data here...
    -----END PRIVATE KEY-----
let algES = JWT.Algorithm.es512(privateKey: privateKeyES, publicKey: "")
let tokenES = try jwt.sign(header: Data(), claims: Data(), algorithm: algES)


let jwt = JWT()
let token = "abc.def.ghi"

let algHS = JWT.Algorithm.hs256(key: Data("test".utf8))
let isValidHS = jwt.verify(token: token, algorithms: [algHS])

// RSA
let publicKeyRS =
    -----BEGIN PUBLIC KEY-----
    ...public key data here...
    -----END PUBLIC KEY-----
let algRS = JWT.Algorithm.rs256(privateKey: "", publicKey: publicKeyRS)
let isValidRS = jwt.verify(token: token, algorithms: [algRS])

Decoding payload

let jwt = JWT()
let token = "abc.def.ghi"

// As JSON data
let data = try jwt.decodeAsData(token: token)
print(data.header, data.claims)

// As JSON strings
let strings = try jwt.decodeAsString(token: token)
print(strings.header, strings.claims)

// As parsed dictionaries
let dictionaries = try jwt.decodeAsDictionary(token: token)
print(dictionaries.header, dictionaries.claims)

// As decodable objects
struct Header: Decodable { /* ... */ }
struct Claims: Decodable { /* ... */ }
let objects: (header: Header, claims: Claims) = try jwt.decodeAsObject(token: token)
print(objects.header, objects.claims)


This library is licensed under Apache 2.0. Full license text is available in LICENSE.

The library uses portions of modified code for private and public keys parsing from BlueRSA and BlueECC.


Stars: 3
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

First release
1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics