{"id":13635366,"url":"https://github.com/skelpo/JWTAuthExample","last_synced_at":"2025-04-19T04:30:57.420Z","repository":{"id":93001936,"uuid":"135271968","full_name":"skelpo/JWTAuthExample","owner":"skelpo","description":"An example on how to authorize JWT in a micro-service.","archived":false,"fork":false,"pushed_at":"2018-10-09T10:32:32.000Z","size":13,"stargazers_count":10,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-09T05:34:04.626Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skelpo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-05-29T09:20:36.000Z","updated_at":"2019-04-06T11:04:17.000Z","dependencies_parsed_at":"2023-03-23T09:32:08.107Z","dependency_job_id":null,"html_url":"https://github.com/skelpo/JWTAuthExample","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skelpo%2FJWTAuthExample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skelpo%2FJWTAuthExample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skelpo%2FJWTAuthExample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skelpo%2FJWTAuthExample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skelpo","download_url":"https://codeload.github.com/skelpo/JWTAuthExample/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249606267,"owners_count":21298851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T00:00:44.567Z","updated_at":"2025-04-19T04:30:57.133Z","avatar_url":"https://github.com/skelpo.png","language":"Swift","funding_links":[],"categories":["Authentication"],"sub_categories":[],"readme":"# JWTAuthExample\n\nIn 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.\n\n## Install\n\nYou will need to add the following two packages to your `Package.swift`:\n\n```swift\n.package(url: \"https://github.com/vapor/jwt.git\", from: \"3.1.0\"),\n.package(url: \"https://github.com/skelpo/JWTMiddleware.git\", from: \"0.8.1\"),\n```\n\nAnd add `JWTMiddleware` as well as `JWT` to all the target dependency arrays you want to access the package in.\n\nComplete the installation by running `vapor update` or `swift package update`.\n\n## Configuration\n\nYou will need to register two services in your `configure.swift`:\n\n```swift\n/// We need this for the JWTProvider, coming from Vapor\ntry services.register(StorageProvider())\n/// Adding the JWTProvider for us to validate JWTs\ntry services.register(JWTProvider { n, d in\nguard let d = d else { throw Abort(.internalServerError, reason: \"Could not find environment variable 'JWT_SECRET'\", identifier: \"missingEnvVar\") }\n\nlet headers = JWTHeader(alg: \"RS256\", crit: [\"exp\", \"aud\"], kid: \"\")\nreturn try RSAService(n: n, e: \"AQAB\", d: d, header: headers)\n})\n```\n\n## Models\n\nTo work with the payload from the JWT you will need a payload model:\n```swift\nimport Foundation\nimport JWT\nimport JWTMiddleware\n\n/// A representation of the payload used in the access tokens\n/// for this service's authentication.\nstruct Payload: IdentifiableJWTPayload {\n    let exp: TimeInterval\n    let iat: TimeInterval\n    \n    // These two are to be customized according to what is in the JWT\n    let email: String\n    let id: Int\n    \n    \n    func verify(using signer: JWTSigner) throws {\n        let expiration = Date(timeIntervalSince1970: self.exp)\n        try ExpirationClaim(value: expiration).verifyNotExpired()\n    }\n}\n```\n## Routes\n\nRoutes can look any way you like but here are two examples:\n\n```swift\n// This simple validates the request, no payload data is used\nlet protected = router.grouped(JWTVerificationMiddleware()).grouped(\"protected\")\nprotected.get(\"test\") { req in\n    return \"You need to have a valid JWT for this!\"\n}\n\n// Here we are using the data\nlet user = router.grouped(JWTStorageMiddleware\u003cPayload\u003e()).grouped(\"user\")\nuser.get(\"me\") { req -\u003e String in\n    let payload:Payload = try req.get(\"skelpo-payload\")!\n    // You can now use the payload and do with it whatever you like.\n    // For example you can use the user id to load related data or save data.\n    return \"This is the ID from the JWT: \\(payload.id)\"\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskelpo%2FJWTAuthExample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskelpo%2FJWTAuthExample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskelpo%2FJWTAuthExample/lists"}