# Swift-JWT **Repository Path**: Loovemaker/Swift-JWT ## Basic Information - **Project Name**: Swift-JWT - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-09-19 - **Last Updated**: 2021-09-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
# SwiftJWT An implementation of [JSON Web Token](https://tools.ietf.org/html/rfc7519) using Swift. JWTs offer a lightweight and compact format for transmitting information between parties, and the information can be verified and trusted due to JWTs being digitally signed. For more information on JSON Web Tokens, their use cases and how they work, we recommend visiting [jwt.io](https://jwt.io/introduction/). **Reminder:** JWTs sent as JWS do **not** encrypt data, so never send anything sensitive or confidential in a JWT. This library does not currently support JWE. ## Swift version The latest version of Swift-JWT requires **Swift 4.0** or later. You can download this version of the Swift binaries by following this [link](https://swift.org/download/). Compatibility with other Swift versions is not guaranteed. ## Usage ### Swift Package Manager #### Add dependencies Add the `Swift-JWT` package to the dependencies within your application’s `Package.swift` file. Substitute `"x.x.x"` with the latest `Swift-JWT` [release](https://github.com/Kitura/Swift-JWT/releases). ```swift .package(url: "https://github.com/Kitura/Swift-JWT.git", from: "x.x.x") ``` Add `SwiftJWT` to your target's dependencies: ```swift .target(name: "example", dependencies: ["SwiftJWT"]), ``` #### Import package ```swift import SwiftJWT ``` ### Cocoapods To include `Swift-JWT` in a project using CocoaPods, add `SwiftJWT` to your Podfile: ``` pod 'SwiftJWT' ``` ## Getting Started ### The JWT model In its compact form, a JSON Web Tokens consist of three sections of Base64Url encoded JSON, separated by dots (.). These section are: Headers, Claims and the Signature. Therefore, a JWT typically looks like the following: xxxxx.yyyyy.zzzzz #### Header The Header struct contains the fields of the JSON Web Token header as defined by [RFC7515](https://tools.ietf.org/html/rfc7515#section-4). The "typ" header will default to "JWT". The "alg" header will be set to the algorithm name when you sign the JWT. The other Header fields can be set when initializing the Header or by changing them directly on the Header object. ```swift let myHeader = Header(kid: "KeyID1") ``` #### Claims Claims are statements about an entity (typically, the user) and additional data. The Claims are defined by creating a Swift type that conforms to the `Claims` protocol. The fields of this type represent the information that will be shared using the JWT. A list of recommended claims is defined in [RFC7519](https://tools.ietf.org/html/rfc7519#section-4.1). ```swift struct MyClaims: Claims { let iss: String let sub: String let exp: Date let admin: Bool } let myClaims = MyClaims(iss: "Kitura", sub: "John", exp: Date(timeIntervalSinceNow: 3600), admin: true) ``` ##### ClaimsExamples This library includes some example `Claims` structs as defined by their online specifications: - `ClaimsStandardJWT` as defined in [RFC7519](https://tools.ietf.org/html/rfc7519#section-4.1). - `ClaimsMicroProfile` as defined [here](http://microprofile.io/project/eclipse/microprofile-jwt-auth/spec/src/main/asciidoc/interoperability.asciidoc). - `ClaimsOpenID.swift` as defined [here](https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims). #### JWT The JWT struct represents the `Header` and `Claims` of a JSON Web Token. You can initialize a JWT by decoding a JWT String, or by providing the JWT Header and Claims. ```swift let myJWT = JWT(header: myHeader, claims: myClaims) ``` ### Signing and Verifying JSON web tokens #### Creating public and private keys To sign and verify a JWT using an RSA algorithm, you must provide a public and private key. This could be the contents of a .key file generated via the following Terminal commands: ``` $ ssh-keygen -t rsa -b 4096 -m PEM -f privateKey.key # Don't add a passphrase $ openssl rsa -in privateKey.key -pubout -outform PEM -out privateKey.key.pub ``` This will create a public and private key pair on your system, and the contents of the private key can be passed into a Swift variable using the following code: ```swift let privateKeyPath = URL(fileURLWithPath: getAbsolutePath(relativePath: "/path/to/privateKey.key")) let privateKey: Data = try Data(contentsOf: privateKeyPath, options: .alwaysMapped) let publicKeyPath = URL(fileURLWithPath: getAbsolutePath(relativePath: "/path/to/publicKey.key")) let publicKey: Data = try Data(contentsOf: publicKeyPath, options: .alwaysMapped) ``` For details on creating elliptic curve public and private keys, view the [BlueECC README.txt](https://github.com/Kitura/BlueECC). #### Sign a JWT using a JWTSigner The struct JWTSigner contains the algorithms that can be used to sign a JWT. Initialize a JWTSigner using the static function corresponding to the desired RSA algorithm: ```swift let jwtSigner = JWTSigner.rs256(privateKey: privateKey) ``` To generate a signed JWT string, call the `sign` function on your JWT instance, passing in a JWTSigner: ```swift let signedJWT = try myJWT.sign(using: jwtSigner) ``` The resulting `signedJWT` will be a `String` of the form: ```