|
1 | 1 | # swift-jwt |
2 | 2 |
|
3 | | -A Swift package for creating, signing, and verifying JSON Web Tokens (JWTs) using Apple's Crypto framework. This package provides a convenient Swift wrapper around RFC 7519 JWT implementation with built-in cryptographic support. |
| 3 | +[](https://github.com/coenttb/swift-jwt/actions/workflows/ci.yml) |
| 4 | + |
| 5 | + |
| 6 | +A Swift package for creating, signing, and verifying JSON Web Tokens (JWTs) using Apple's Crypto framework. |
4 | 7 |
|
5 | 8 | ## Features |
6 | 9 |
|
7 | | -- **Multiple Signing Algorithms**: HMAC-SHA256/384/512 and ECDSA-SHA256 support |
8 | | -- **Built on Standards**: Uses `swift-rfc-7519` for RFC compliance and `swift-crypto` for cryptography |
9 | | -- **Convenience Methods**: Easy-to-use static methods for common JWT operations |
10 | | -- **Flexible Configuration**: Full control over JWT headers, claims, and timing |
11 | | -- **Type Safety**: Leverages Swift's type system for secure JWT handling |
12 | | -- **Comprehensive Validation**: Signature verification with timing validation |
| 10 | +- HMAC-SHA256/384/512 and ECDSA-SHA256 signing algorithms |
| 11 | +- RFC 7519 compliant JWT implementation via `swift-rfc-7519` |
| 12 | +- Apple Crypto framework integration via `swift-crypto` |
| 13 | +- Static methods for HMAC and ECDSA JWT creation |
| 14 | +- JWT header, claims, and timing configuration |
| 15 | +- Type-safe JWT handling via Swift's type system |
| 16 | +- Signature verification with timing validation (exp, nbf, iat) |
13 | 17 |
|
14 | 18 | ## Requirements |
15 | 19 |
|
@@ -92,15 +96,26 @@ let isFullyValid = try jwt.verifyAndValidate(with: verificationKey) |
92 | 96 |
|
93 | 97 | ```swift |
94 | 98 | import JWT |
| 99 | +import Crypto |
95 | 100 |
|
96 | | -// Create verification key from public key |
| 101 | +// Create verification key from signing key |
| 102 | +let privateKey = P256.Signing.PrivateKey() |
97 | 103 | let verificationKey = VerificationKey.ecdsa(from: .ecdsa(privateKey))! |
98 | 104 |
|
99 | | -// Or from raw public key data |
| 105 | +// Verify the JWT |
| 106 | +let isValid = try jwt.verifyAndValidate(with: verificationKey) |
| 107 | +``` |
| 108 | + |
| 109 | +Alternative - using raw public key data: |
| 110 | + |
| 111 | +```swift |
| 112 | +import JWT |
| 113 | +import Crypto |
| 114 | + |
| 115 | +let privateKey = P256.Signing.PrivateKey() |
100 | 116 | let publicKeyData = privateKey.publicKey.rawRepresentation |
101 | 117 | let verificationKey = try VerificationKey.ecdsa(rawRepresentation: publicKeyData) |
102 | 118 |
|
103 | | -// Verify the JWT |
104 | 119 | let isValid = try jwt.verifyAndValidate(with: verificationKey) |
105 | 120 | ``` |
106 | 121 |
|
@@ -234,23 +249,10 @@ The package includes comprehensive tests covering: |
234 | 249 | - Edge cases and error conditions |
235 | 250 | - Key management operations |
236 | 251 |
|
237 | | -## Related projects |
238 | | - |
239 | | -### The coenttb stack |
240 | | - |
241 | | -* [swift-css](https://www.github.com/coenttb/swift-css): A Swift DSL for type-safe CSS. |
242 | | -* [swift-html](https://www.github.com/coenttb/swift-html): A Swift DSL for type-safe HTML & CSS, integrating [swift-css](https://www.github.com/coenttb/swift-css) and [pointfree-html](https://www.github.com/coenttb/pointfree-html). |
243 | | -* [swift-web](https://www.github.com/coenttb/swift-web): Foundational tools for web development in Swift. |
244 | | -* [coenttb-html](https://www.github.com/coenttb/coenttb-html): Builds on [swift-html](https://www.github.com/coenttb/swift-html), and adds functionality for HTML, Markdown, Email, and printing HTML to PDF. |
245 | | -* [coenttb-web](https://www.github.com/coenttb/coenttb-web): Builds on [swift-web](https://www.github.com/coenttb/swift-web), and adds functionality for web development. |
246 | | -* [coenttb-server](https://www.github.com/coenttb/coenttb-server): Build fast, modern, and safe servers that are a joy to write. `coenttb-server` builds on [coenttb-web](https://www.github.com/coenttb/coenttb-web), and adds functionality for server development. |
247 | | -* [coenttb-vapor](https://www.github.com/coenttb/coenttb-server-vapor): `coenttb-server-vapor` builds on [coenttb-server](https://www.github.com/coenttb/coenttb-server), and adds functionality and integrations with Vapor and Fluent. |
248 | | -* [coenttb-com-server](https://www.github.com/coenttb/coenttb-com-server): The backend server for coenttb.com, written entirely in Swift and powered by [coenttb-server-vapor](https://www.github.com/coenttb-server-vapor). |
| 252 | +## Related Packages |
249 | 253 |
|
250 | | -### PointFree foundations |
251 | | -* [coenttb/pointfree-html](https://www.github.com/coenttb/pointfree-html): A Swift DSL for type-safe HTML, forked from [pointfreeco/swift-html](https://www.github.com/pointfreeco/swift-html) and updated to the version on [pointfreeco/pointfreeco](https://github.com/pointfreeco/pointfreeco). |
252 | | -* [coenttb/pointfree-web](https://www.github.com/coenttb/pointfree-html): Foundational tools for web development in Swift, forked from [pointfreeco/swift-web](https://www.github.com/pointfreeco/swift-web). |
253 | | -* [coenttb/pointfree-server](https://www.github.com/coenttb/pointfree-html): Foundational tools for server development in Swift, forked from [pointfreeco/swift-web](https://www.github.com/pointfreeco/swift-web). |
| 254 | +* [swift-html](https://www.github.com/coenttb/swift-html): The Swift library for domain-accurate and type-safe HTML & CSS. |
| 255 | +* [swift-web](https://www.github.com/coenttb/swift-web): A Swift package with tools to simplify web development. |
254 | 256 |
|
255 | 257 | ## Feedback is Much Appreciated! |
256 | 258 |
|
|
0 commit comments