Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skelpo/JWTAuthExample
An example on how to authorize JWT in a micro-service.
https://github.com/skelpo/JWTAuthExample
Last synced: 2 months ago
JSON representation
An example on how to authorize JWT in a micro-service.
- Host: GitHub
- URL: https://github.com/skelpo/JWTAuthExample
- Owner: skelpo
- Created: 2018-05-29T09:20:36.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-09T10:32:32.000Z (over 6 years ago)
- Last Synced: 2024-08-02T00:22:19.643Z (6 months ago)
- Language: Swift
- Size: 12.7 KB
- Stars: 10
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- Awesome-Swift-Packages - JWTAuthExample - An example on how to authorize JWT in a micro-service. (Authentication)
README
# JWTAuthExample
In this project you can see an example implementation of [JWTMiddleware](https://github.com/skelpo/JWTMiddleware). The approach can be used in micro-services or webpages that need to verify incoming JWTs and use the data somehow.
## Install
You will need to add the following two packages to your `Package.swift`:
```swift
.package(url: "https://github.com/vapor/jwt.git", from: "3.1.0"),
.package(url: "https://github.com/skelpo/JWTMiddleware.git", from: "0.8.1"),
```And add `JWTMiddleware` as well as `JWT` to all the target dependency arrays you want to access the package in.
Complete the installation by running `vapor update` or `swift package update`.
## Configuration
You will need to register two services in your `configure.swift`:
```swift
/// We need this for the JWTProvider, coming from Vapor
try services.register(StorageProvider())
/// Adding the JWTProvider for us to validate JWTs
try services.register(JWTProvider { n, d in
guard let d = d else { throw Abort(.internalServerError, reason: "Could not find environment variable 'JWT_SECRET'", identifier: "missingEnvVar") }let headers = JWTHeader(alg: "RS256", crit: ["exp", "aud"], kid: "")
return try RSAService(n: n, e: "AQAB", d: d, header: headers)
})
```## Models
To work with the payload from the JWT you will need a payload model:
```swift
import Foundation
import JWT
import JWTMiddleware/// A representation of the payload used in the access tokens
/// for this service's authentication.
struct Payload: IdentifiableJWTPayload {
let exp: TimeInterval
let iat: TimeInterval
// These two are to be customized according to what is in the JWT
let email: String
let id: Int
func verify(using signer: JWTSigner) throws {
let expiration = Date(timeIntervalSince1970: self.exp)
try ExpirationClaim(value: expiration).verifyNotExpired()
}
}
```
## RoutesRoutes can look any way you like but here are two examples:
```swift
// This simple validates the request, no payload data is used
let protected = router.grouped(JWTVerificationMiddleware()).grouped("protected")
protected.get("test") { req in
return "You need to have a valid JWT for this!"
}// Here we are using the data
let user = router.grouped(JWTStorageMiddleware()).grouped("user")
user.get("me") { req -> String in
let payload:Payload = try req.get("skelpo-payload")!
// You can now use the payload and do with it whatever you like.
// For example you can use the user id to load related data or save data.
return "This is the ID from the JWT: \(payload.id)"
}
```