Remove deprecated encode/decode methods, fix warnings and other… (#112) Remove deprecated encode/decode methods, fix warnings and other stuff
Remove deprecated encode/decode methods, fix warnings and other… (#112)
Remove deprecated encode/decode methods, fix warnings and other stuff
Swift implementation of JSON Web Token.
Swift Pacakage Manager is the recommended installation method for JSONWebToken, CocoaPods is also supported.
pod 'JSONWebToken'
NOTE: Carthage may be supported, however support will not be provided for this installation method, use at your own risk if you know how it works.
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: .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)!) ])
You might also want to give your iat, exp and nbf checks some kind of leeway to account for skewed clocks. You can do this by passing a leeway parameter like this:
leeway
try JWT.decode("eyJh...5w", algorithm: .hs256("secret".data(using: .utf8)!), leeway: 10)
The library supports validating the following claims:
iss
exp
nbf
iat
aud
This library supports the following algorithms:
none
hs256
hs384
hs512
JSONWebToken is licensed under the BSD license. See LICENSE for more info.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
JSON Web Token
Swift implementation of JSON Web Token.
Installation
Swift Pacakage Manager is the recommended installation method for JSONWebToken, CocoaPods is also supported.
NOTE: Carthage may be supported, however support will not be provided for this installation method, use at your own risk if you know how it works.
Usage
Encoding a claim
Encoding a claim set
Building a JWT with the builder pattern
Decoding a JWT
When decoding a JWT, you must supply one or more algorithms and keys.
When the JWT may be signed with one out of many algorithms or keys:
You might also want to give your iat, exp and nbf checks some kind of leeway to account for skewed clocks. You can do this by passing a
leeway
parameter like this:Supported claims
The library supports validating the following claims:
iss
) Claimexp
) Claimnbf
) Claimiat
) Claimaud
) ClaimAlgorithms
This 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 algorithmLicense
JSONWebToken is licensed under the BSD license. See LICENSE for more info.