A modern, type-safe Swift library for JSON Web Token (JWT), JSON Web Signature (JWS), and JSON Web Encryption (JWE) with first-class Apple CryptoKit support.
Building secure authentication in Swift? JWSETKit is your complete solution for working with JSON Web Tokens (JWT), JSON Web Signatures (JWS), and JSON Web Encryption (JWE) with native Apple CryptoKit integration.
This module makes it possible to serialize, deserialize, create, and verify JWS/JWT messages.
β JWT (JSON Web Tokens)
- Create, sign, verify, and decode JWT tokens
- Support for standard and custom claims
- Expiration and validation handling
β JWS (JSON Web Signature)
- Digital signatures with multiple algorithms
- Message authentication codes (MACs)
- Detached signature support
β JWE (JSON Web Encryption)
- Content encryption with various algorithms
- Key wrapping and management
- Compact and JSON serialization
β SD-JWT (Selective Disclosure)
- RFC 9901 compliant selective disclosure
- Privacy-preserving credential presentations
- Key binding for holder authentication
- SD-JWT VC support for Verifiable Credentials
β JWK (JSON Web Keys)
- Key generation and management
- Key conversion and serialization
- Support for key sets (JWKS)
β
Server-Side Swift (requires the HTTP trait)
- Drop-in Vapor and Hummingbird integration via
HTTPHeaders/HTTPFieldsextensions - Verify
Authorizationtokens directly from request headers - Remote JWKS fetching for Apple, Google, Firebase, Microsoft, and any OpenID Connect issuer
- DPoP (RFC 9449) proof creation and verification
β CryptoKit Integration
- JWK encoding/decoding for all CryptoKit key types (P256, P384, P521, Curve25519, RSA)
- SPKI (SubjectPublicKeyInfo) and PKCS#8 import/export for CryptoKit keys
- ML-DSA (Dilithium) post-quantum keys with SPKI/PKCS#8 support
β secp256k1 (P-256K) Support
- ES256K signature algorithm for Bitcoin/Ethereum compatibility
- ECDSA signing and verification
- Schnorr signature support
- ECDH key agreement
Add JWSETKit to your Package.swift:
dependencies: [
.package(url: "https://github.com/amosavian/JWSETKit", from: "2.0.0")
]Then add to your target:
dependencies: [
.product(name: "JWSETKit", package: "JWSETKit"),
]For X509 certificate support (Swift 6.1+):
dependencies: [
.package(url: "https://github.com/amosavian/JWSETKit", from: "2.0.0", traits: ["X509"])
]For secp256k1 (ES256K) support (Swift 6.1+):
dependencies: [
.package(url: "https://github.com/amosavian/JWSETKit", from: "2.0.0", traits: ["P256K"])
]The HTTP trait enables networking helpers used for server-side integration:
remote JWKS fetching, AsyncHTTPClient / swift-http-types support, and the
DPoP request extensions. Enable it when integrating with Vapor or Hummingbird:
dependencies: [
.package(url: "https://github.com/amosavian/JWSETKit", from: "2.0.0", traits: ["HTTP"])
]- File β Add Package Dependencies
- Enter:
https://github.com/amosavian/JWSETKit - Select version and add to your target
For detailed usage and API documentation, check the documentation.
import JWSETKit
import CryptoKit
// Create a JWT with claims
let key = SymmetricKey(size: .bits256)
let payload = try JSONWebTokenClaims {
$0.issuedAt = .init()
$0.expiry = .init(timeIntervalSinceNow: 3600)
$0.jwtUUID = .init()
$0.subject = "user123"
}
let jwt = try JSONWebToken(payload: payload, algorithm: .hmacSHA256, using: key)
// Verify and decode
let decodedJWT = try JSONWebToken(from: jwtString)
try decodedJWT.verifySignature(using: key)
print(decodedJWT.payload.subject) // "user123"// Initialize key
let key = try P256.Signing.PublicKey(pemRepresentation: publicKeyPEM)
// Verify incoming JWT
let token = try JSONWebToken(from: request.headers["Authorization"])
try token.verify(using: key, for: "audience-name")// Sign arbitrary data with JWS
let payload = "Important message"
let jws = try JSONWebSignaturePlain(
payload: payload.utf8,
algorithm: .ecdsaSignatureP256SHA256,
using: key
)
try print(String(jws))
// Verify JWS signature
let verified = try JSONWebSignaturePlain(from: String(jws))
try verified.verifySignature(using: key)
let message = String(decoding: verified.payload, as: UTF8.self)// Encrypt sensitive data
let sensitiveData = Data("Secret information".utf8)
let encryptionKey = JSONWebRSAPrivateKey(keySize: .bits2048)
let jwe = try JSONWebEncryption(
content: sensitiveData,
keyEncryptingAlgorithm: .rsaEncryptionOAEP,
keyEncryptionKey: encryptionKey.publicKey,
contentEncryptionAlgorithm: .aesEncryptionGCM128
)
try print(String(jwe))
// Decrypt JWE
let jwe = try JSONWebEncryption(from: jweString)
let decrypted = jwe.decrypt(using: encryptionKey)
let secret = String(decoding: decrypted, as: UTF8.self)// Create CryptoKit key
let privateKey = P256.Signing.PrivateKey()
// Import and Export as JWK data
let jwkJSON = try JSONEncoder().encode(privateKey)
let importedJWK = try JSONDecoder().decode(P256.Signing.PrivateKey.self, from: jwkJSON)
// Import PKCS#8
let importedKey = try P256.Signing.PrivateKey(importing: pkcs8Data, format: .pkcs8)// Issuer: Create SD-JWT with selective claims
let claims = try JSONWebTokenClaims {
$0.issuer = "https://issuer.example.com"
$0.subject = "user123"
$0.giveName = "John"
$0.familyName = "Doe"
$0.email = "john@example.com"
}
let sdJWT = try JSONWebSelectiveDisclosureToken(
claims: claims,
policy: .standard, // Standard claims visible, others disclosable
using: issuerKey
)
// Holder: Create presentation with selected disclosures
let presentation = try sdJWT.presenting(paths: ["/email"])
// Verifier: Validate and access disclosed claims
try presentation.validate()
try presentation.verifySignature(using: issuerPublicKey)
let disclosed = try presentation.disclosedPayload()
print(disclosed.email) // "john@example.com"| JWSETKit | jwt-kit | JOSESwift | Auth0's JWTDecode | |
|---|---|---|---|---|
| JSON Web Signature (JWS) | β | β | β | β |
| JWS Multiple Signatures | β | β | β | β |
| JWS Unencoded/Detached Payload | β | β | β | β |
| JSON Web Token (JWT) | β | β | β | β |
| JWT Signature Verification | β | β | β | β |
| JWT Expire/NotBefore Validity | β | β | β | β |
| JSON Web Encryption (JWE) | β | β | β | β |
| SD-JWT (RFC 9901) | β | β | β | β |
| DPoP (RFC 9449) | β | β | β | β |
| Support CommonCrypto Keys | β | β | β | β |
| Support CryptoKit Keys | β | β | β | β |
| JWSETKit | jwt-kit | JOSESwift | Auth0's JWTDecode | |
|---|---|---|---|---|
| HS256 | β | β | β | β |
| HS384 | β | β | β | β |
| HS512 | β | β | β | β |
| RS256 | β | β | β | β |
| RS384 | β | β | β | β |
| RS512 | β | β | β | β |
| ES256 | β | β | β | β |
| ES384 | β | β | β | β |
| ES512 | β | β | β | β |
| PS256 | β | β | β | β |
| PS384 | β | β | β | β |
| PS512 | β | β | β | β |
| EdDSA | β | β | β | β |
| Ed25519 | β | β | β | β |
| Ed448 | β | β | β | β |
| ES256K | β | β | β | β |
| ML-DSA-44 | β | β | β | β |
| ML-DSA-65 | β | β | β | β |
| ML-DSA-87 | β | β | β | β |
| JWSETKit | JOSESwift | |
|---|---|---|
| RSA1_5 | β | β |
| RSA-OAEP | β | β |
| RSA-OAEP-256 | β | β |
| A128KW | β | β |
| A192KW | β | β |
| A256KW | β | β |
| dir | β | β |
| ECDH-ES | β | β |
| ECDH-ES+A128KW | β | β |
| ECDH-ES+A192KW | β | β |
| ECDH-ES+A256KW | β | β |
| A128GCMKW | β | β |
| A192GCMKW | β | β |
| A256GCMKW | β | β |
| PBES2-HS256+A128KW | β | β |
| PBES2-HS384+A192KW | β | β |
| PBES2-HS512+A256KW | β | β |
| HPKE-0 (P256/AES128) | β | β |
| HPKE-1 (P384/AES256) | β | β |
| HPKE-2 (P521/AES256) | β | β |
| HPKE-3 (X25519/AES256) | β | β |
| HPKE-4 (X25519/ChaCha) | β | β |
| HPKE-5 (X448/AES256) | β | β |
| HPKE-6 (X448/ChaCha) | β | β |
| HPKE-7 (P256/AES256) | β | β |
| JWSETKit | JOSESwift | |
|---|---|---|
| A128CBC-HS256 | β | β |
| A192CBC-HS384 | β | β |
| A256CBC-HS512 | β | β |
| A128GCM | β | β |
| A192GCM | β | β |
| A256GCM | β | β |
JWSETKit is perfect for:
- π API Authentication - Secure REST API authentication with JWT tokens
- π OAuth 2.0 / OpenID Connect - Implement modern authentication flows
- π± Mobile App Security - Token-based auth for iOS/macOS apps
- π₯οΈ Server-Side Swift - Drop-in Vapor and Hummingbird request authentication
- π Microservices - Service-to-service authentication
- π« Session Management - Stateless session tokens
- π Data Encryption - Protect sensitive data with JWE
- πͺͺ Verifiable Credentials - Privacy-preserving identity with SD-JWT
π Full Documentation
Browse our comprehensive guides:
- Getting Started
- Cryptography Guide
- Security Guidelines & Algorithm Selection
- Extending Containers
- Using JWSETKit with Vapor
- Migrating from Vapor JWTKit
- Using JWSETKit with Hummingbird
- Using DPoP (RFC 9449)
- API Reference
We welcome contributions!
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
# Clone the repository
git clone https://github.com/amosavian/JWSETKit.git
# Run tests
swift test
# Build the project
swift buildJWSETKit ships a performance benchmark suite built on
package-benchmark, plus an opt-in head-to-head comparison against other Swift JOSE
libraries:
swift package --package-path Benchmarks/Regression benchmark- π Report Issues
- π¬ Discussions
- π§ Contact
JWSETKit is released under the MIT License. See LICENSE for details.
This library implements the following JOSE standards:
- RFC 7515 - JSON Web Signature (JWS)
- RFC 7516 - JSON Web Encryption (JWE)
- RFC 7517 - JSON Web Key (JWK)
- RFC 7518 - JSON Web Algorithms (JWA)
- RFC 7519 - JSON Web Token (JWT)
- RFC 7520 - Examples of Protecting Content Using JSON Object Signing and Encryption (JOSE)
- RFC 7797 - JSON Web Signature (JWS) Unencoded Payload Option
- RFC 7800 - Proof-of-Possession Key Semantics for JSON Web Tokens (JWTs)
- RFC 9864 - Fully-Specified Algorithms for JOSE and COSE
- RFC 9901 - SD-JWT: Selective Disclosure for JWTs
- draft-ietf-jose-hpke-encrypt - Use of Hybrid Public Key Encryption (HPKE) with JSON Object Signing and Encryption (JOSE)
- draft-ietf-cose-dilithium - ML-DSA for JOSE and COSE
- OIDC Core - OpenID Connect Core 1.0 incorporating errata set 2