在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:kylef/JSONWebToken.swift开源软件地址:https://github.com/kylef/JSONWebToken.swift开源编程语言:Swift 99.9%开源软件介绍:JSON Web TokenSwift implementation of JSON Web Token. InstallationSwift 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. Usageimport JWT Encoding a claimJWT.encode(claims: ["my": "payload"], algorithm: .hs256("secret".data(using: .utf8)!)) Encoding a claim setvar claims = ClaimSet()
claims.issuer = "fuller.li"
claims.issuedAt = Date()
claims["custom"] = "Hi"
JWT.encode(claims: claims, algorithm: .hs256("secret".data(using: .utf8)!)) Building a JWT with the builder patternJWT.encode(.hs256("secret".data(using: .utf8))) { builder in
builder.issuer = "fuller.li"
builder.issuedAt = Date()
builder["custom"] = "Hi"
} Decoding a JWTWhen 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 try JWT.decode("eyJh...5w", algorithm: .hs256("secret".data(using: .utf8)!), leeway: 10) Supported claimsThe library supports validating the following claims:
AlgorithmsThis library supports the following algorithms:
LicenseJSONWebToken is licensed under the BSD license. See LICENSE for more info. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论