Latest0.0.2
Homepagehttps://github.com/Wstunes/SwiftyJWT
LicenseMIT
Platformsios 8.0
DependenciesSwiftyCrypto
Authors

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

Features

  • [x] Generate JWT with certain header, payload and algorithm.
  • [x] Decode a JWT String into JWT entity after mandatory checkings.
  • [x] Multiple-claims checking.
  • [x] Multiple-algorithms support.
  • [x] 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")

Latest podspec

{
    "name": "SwiftyJWT",
    "version": "0.0.2",
    "summary": "A library to generate JWT with Swift",
    "homepage": "https://github.com/Wstunes/SwiftyJWT",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Wstunes": "[email protected]"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/Wstunes/SwiftyJWT.git",
        "tag": "0.0.2"
    },
    "source_files": "Sources/**/*.swift",
    "dependencies": {
        "SwiftyCrypto": [
            "~> 0.0.1"
        ]
    },
    "swift_version": "4.0"
}

Pin It on Pinterest

Share This