Skip to content

A full-featured Swift library for JOSE standards with first-class support for CryptoKit keys, providing JWS, JWT, and JWE with signing, encryption, and JWK key management.

License

amosavian/JWSETKit

Repository files navigation

JWSETKit

A library for working with JSON Web Signature (JWS) and . A modern, type-safe Swift library for JSON Web Token (JWT), JSON Web Signature (JWS), and JSON Web Encryption (JWE) with first-class Apple's CryptoKit support

Swift CodeQL License Release version

Lines of Code Duplicated Lines

Quality Gate Status Technical Debt Maintainability Rating Coverage

Overview

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.

πŸ“– Table of Contents

πŸš€ Features

Core Capabilities

βœ… 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

βœ… JWK (JSON Web Keys)

  • Key generation and management
  • Key conversion and serialization
  • Support for key sets (JWKS)

Getting Started

Swift Package Manager

Add JWSETKit to your Package.swift:

dependencies: [
    .package(url: "https://github.com/amosavian/JWSETKit", from: "0.26.0")
]

Then add to your target:

dependencies: [
    .product(name: "JWSETKit", package: "JWSETKit"),
]

With X509 Support

For X509 certificate support (Swift 6.1+):

dependencies: [
    .package(url: "https://github.com/amosavian/JWSETKit", from: "0.26.0", traits: ["X509"])
]

Xcode

  1. File β†’ Add Package Dependencies
  2. Enter: https://github.com/amosavian/JWSETKit
  3. Select version and add to your target

Usage

For detailed usage and API documentation, check the documentation.

Creating and Verifying JWT Signature

import JWSETKit
import CryptoKit

// Create a JWT with claims
let key = SymmetricKey(size: .bits128)
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"

Basic JWT Authentication

// 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")

Working with JWS

// 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)

Encrypting with JWE

// 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)

Managing Keys with JWK

// 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)

πŸ“Š Comparison with Alternatives

Features

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) βœ… ❌ βœ… ❌
Support CommonCrypto Keys βœ… ❌ ❌ ❌
Support CryptoKit Keys βœ… ❌ ❌ ❌

Supported Algorithms

Signature/HMAC

JWSETKit jwt-kit JOSESwift Auth0's JWTDecode
HS256 βœ… βœ… βœ… ❌
HS384 βœ… βœ… βœ… ❌
HS512 βœ… βœ… βœ… ❌
RS256 βœ… βœ… βœ… ❌
RS384 βœ… βœ… βœ… ❌
RS512 βœ… βœ… βœ… ❌
ES256 βœ… βœ… βœ… ❌
ES384 βœ… βœ… βœ… ❌
ES512 βœ… βœ… βœ… ❌
PS256 βœ… βœ… βœ… ❌
PS384 βœ… βœ… βœ… ❌
PS512 βœ… βœ… βœ… ❌
PS512 βœ… βœ… βœ… ❌
EdDSA βœ… βœ… ❌ ❌
Ed25519 βœ… ❌ ❌ ❌
Ed448 ❌ ❌ ❌ ❌
E256K ❌ ❌ ❌ ❌
ML-DSA-44 ❌ ❌ ❌ ❌
ML-DSA-65 βœ… ❌ ❌ ❌
ML-DSA-87 βœ… ❌ ❌ ❌

Key Encryption

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 βœ… ❌
HPKE-0 (P256) βœ… ❌
HPKE-1 (P384) βœ… ❌
HPKE-2 (P521) βœ… ❌
HPKE-3 (X25519) βœ… ❌
HPKE-4 (X25519/ChaCha) βœ… ❌
HPKE-5 (X448) ❌ ❌
HPKE-6 (X448/ChaCha) ❌ ❌

Content Encryption

JWSETKit JOSESwift
A128CBC-HS256 βœ… βœ…
A192CBC-HS384 βœ… βœ…
A256CBC-HS512 βœ… βœ…
A128GCM βœ… βœ…
A192GCM βœ… βœ…
A256GCM βœ… βœ…

πŸ—οΈ Use Cases

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
  • πŸ”„ Microservices - Service-to-service authentication
  • 🎫 Session Management - Stateless session tokens
  • πŸ” Data Encryption - Protect sensitive data with JWE

πŸ“š Documentation

Browse our comprehensive guides:

🀝 Contributing

We welcome contributions!

How to Contribute

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development

# Clone the repository
git clone https://github.com/amosavian/JWSETKit.git

# Run tests
swift test

# Build the project
swift build

🌟 Support

πŸ“„ License

JWSETKit is released under the MIT License. See LICENSE for details.

πŸ™ Acknowledgments

This library implements the following JOSE standards:


Built with ❀️ using Swift

Star on GitHub

About

A full-featured Swift library for JOSE standards with first-class support for CryptoKit keys, providing JWS, JWT, and JWE with signing, encryption, and JWK key management.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 5

Languages