swi

SwiftyJWT

A library to generate JWT with Swift

Showing:

Popularity

Downloads/wk

0

GitHub Stars

45

Maintenance

Last Commit

1yr ago

Contributors

6

Package

Dependencies

1

License

MIT

Categories

Readme

SwiftyJWT is a lightweight, pure-Swift library to generate JWT in a flexible way.

Features

  • Generate JWT with certain header, payload and algorithm.
  • Decode a JWT String into JWT entity after mandatory checkings.
  • Multiple-claims checking.
  • Multiple-algorithms support.
  • HS, RSA algorithm key management.

How to integrate

pod 'SwiftyJWT'

Available Algorithms

The library implements JWT Verification and Signing using the following algorithms:

JWSAlgorithmDescription
HS256HMAC256HMAC with SHA-256
HS384HMAC384HMAC with SHA-384
HS512HMAC512HMAC with SHA-512
RS256RSA256RSASSA-PKCS1-v1_5 with SHA-256
RS384RSA384RSASSA-PKCS1-v1_5 with SHA-384
RS512RSA512RSASSA-PKCS1-v1_5 with SHA-512

Usage

Generate JWT with certain header, payload and algorithm

HS

let alg = JWTAlgorithm.hs256("secret")
let headerWithKeyId = JWTHeader.init(keyId: "testKeyId")
var payload = JWTPayload()
payload.expiration = 515616187993
payload.issuer = "yufu"
payload.subject = "shuo"
payload.customFields = ["name": EncodableValue(value: "wang"),
            "isAdmin": EncodableValue(value: true),
            "age": EncodableValue(value: 125),
            "height": EncodableValue(value: 121.5)]

let jwtWithKeyId = try? JWT.init(payload: payload, algorithm: alg, header: headerWithKeyId)

RSA

let privateKey = try! RSAKey.init(base64String: "keyString", keyType: .PRIVATE)
let alg = JWTAlgorithm.rs256(privateKey)
let jwtWithKeyId = try? JWT.init(payload: payload, algorithm: alg, header: headerWithKeyId)

Decode JWT String into JWT entity

HS

let jwtString = "ey....."
let alg = JWTAlgorithm.hs256("secret")
let jwt = try? JWT.init(algorithm: alg, rawString: jwtString)

RSA

let jwtString = "ey....."
let publicKey = try! RSAKey.init(base64String: "keyString", keyType: .PUBLIC)
let alg = JWTAlgorithm.rs256(publicKey)
let jwt1 = try? JWT.init(algorithm: alg, rawString: jwtString)

JWTPayload claims checking

let payload = JWTPayload()
try payload.checkExpiration()
try payload.checkIssueAt(allowNil: false)
try payload.checkSubject(expected: "shuo")
try payload.checkIssuer(expected: "yufu")
try payload.checkAudience(expected: "here")

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100