Heavily based on Kylef's implementation, this library has been refactored for Swift 5.0 and enables Swift Package.
SwiftJWT is a simple Swift library to encode and decode JWT tokens. This library confirms to the IEFT specification.
With Package Manager
.package(name: "SwiftyJWT", url: "https://github.com/polarcop/SwiftyJWT.git", .upToNextMajor(from: "1.0.0"))
let secret = "secret".data(using: .utf8)!
SwiftyJWT.encode(claims: ["key": "value"], algorithm: .hs256(secret))
let secret = "secret".data(using: .utf8)!
var claims = ClaimSet()
claims.issuer = "www.polarcop.com"
claims.issuedAt = Date()
claims["key"] = "value"
SwiftyJWT.encode(claims: claims, algorithm: .hs256(secret))
let secret = "secret".data(using: .utf8)!
SwiftyJWT.encode(.hs256(secret)) { builder in
builder.issuer = "www.polarcop.com"
builder.issuedAt = Date()
builder["key"] = "value"
}
do {
let secret = "secret".data(using: .utf8)!
let jwtString: String = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwb2xhciI6dHJ1ZX0.ip-a_End-3GHD4NSwxhk3wCnMBexxPaxlzWRtJrh3NY"
let claims: ClaimSet = try SwiftyJWT.decode(jwtString, algorithm: .hs256(secret))
print(claims)
} catch let error as SwiftyJWT.JWTErrors.InvalidToken {
print("JWT Token is invalid: \(error)")
} catch {
print("Failed to decode JWT: \(error)")
}
SwiftyJWT is maintained by Polar and released under the Apache 2.0 license. See LICENSE for details