TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | BSD |
ReleasedLast Release | Sep 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✓ |
Maintained by Kyle Fuller.
Swift implementation of JSON Web Token.
CocoaPods is the recommended installation method.
pod 'JSONWebToken'
import JWT
JWT.encode(claims: ["my": "payload"], algorithm: .hs256("secret".data(using: .utf8)!))
var claims = ClaimSet()
claims.issuer = "fuller.li"
claims.issuedAt = Date()
claims["custom"] = "Hi"
JWT.encode(claims: claims, algorithm, algorithm: .hs256("secret".data(using: .utf8)))
JWT.encode(.hs256("secret".data(using: .utf8))) { builder in
builder.issuer = "fuller.li"
builder.issuedAt = Date()
builder["custom"] = "Hi"
}
When decoding a JWT, you must supply one or more algorithms and keys.
do {
let claims: ClaimSet = try JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w", algorithm: .hs256("secret".data(using: .utf8)!))
print(claims)
} catch {
print("Failed to decode JWT: \(error)")
}
When the JWT may be signed with one out of many algorithms or keys:
try JWT.decode("eyJh...5w", algorithms: [
.hs256("secret".data(using: .utf8)!),
.hs256("secret2".data(using: .utf8)!),
.hs512("secure".data(using: .utf8)!)
])
The library supports validating the following claims:
iss
) Claimexp
) Claimnbf
) Claimiat
) Claimaud
) ClaimThis library supports the following algorithms:
none
- Unsecured JWTshs256
- HMAC using SHA-256 hash algorithm (default)hs384
- HMAC using SHA-384 hash algorithmhs512
- HMAC using SHA-512 hash algorithmJSONWebToken is licensed under the BSD license. See LICENSE for more info.