Swiftpack.co - polarcop/SwiftyJWT as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
polarcop/SwiftyJWT
A flexible JWT library for Swift.
.package(url: "https://github.com/polarcop/SwiftyJWT.git", from: "1.0.2")

SwiftyJWT

Heavily based on Kylef's implementation, this library has been refactored for Swift 5.0 and enables Swift Package.

SwiftJWT is a simple Swift library to encode and decode JWT tokens. This library confirms to the IEFT specification.

Installation

With Package Manager

.package(name: "SwiftyJWT", url: "https://github.com/polarcop/SwiftyJWT.git", .upToNextMajor(from: "1.0.0"))

Usage

Encoding

Basic Encoding

let secret = "secret".data(using: .utf8)!
SwiftyJWT.encode(claims: ["key": "value"], algorithm: .hs256(secret))

Advanced Encoding (Claim Set)

let secret = "secret".data(using: .utf8)!
var claims = ClaimSet()
claims.issuer = "www.polarcop.com"
claims.issuedAt = Date()
claims["key"] = "value"
SwiftyJWT.encode(claims: claims, algorithm: .hs256(secret))

Advanced Encoding (Builder)

let secret = "secret".data(using: .utf8)!
SwiftyJWT.encode(.hs256(secret)) { builder in
    builder.issuer = "www.polarcop.com"
    builder.issuedAt = Date()
    builder["key"] = "value"
}

Decoding

Basic Decoding

do {
    let secret = "secret".data(using: .utf8)!
    let jwtString: String = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwb2xhciI6dHJ1ZX0.ip-a_End-3GHD4NSwxhk3wCnMBexxPaxlzWRtJrh3NY"
    let claims: ClaimSet = try SwiftyJWT.decode(jwtString, algorithm: .hs256(secret))
    print(claims)
} catch let error as SwiftyJWT.JWTErrors.InvalidToken {
    print("JWT Token is invalid: \(error)")
} catch {
    print("Failed to decode JWT: \(error)")
}

License:

SwiftyJWT is maintained by Polar and released under the Apache 2.0 license. See LICENSE for details

GitHub

link
Stars: 3
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Release Notes

Updated Crypto Module + Fixed Exclude Warning
1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics