Skip to content

Commit 351f527

Browse files
authored
Merge pull request #5 from siemensikkema/feature/update-to-jwt3
Update to JWT3 and simplify
2 parents e44e03a + 4d48c49 commit 351f527

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
dependencies: [
1212
// 💧 A server-side Swift web framework.
1313
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
14-
.package(url: "https://github.com/vapor/jwt.git", from: "3.0.0-rc.2.1.2"),
14+
.package(url: "https://github.com/vapor/jwt.git", from: "3.0.0"),
1515
],
1616
targets: [
1717
.target(name: "FCM", dependencies: ["Vapor", "JWT"]),

Sources/FCM/GAuthPayload.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GAuthPayload: JWTPayload {
1414
static var expirationClaim: ExpirationClaim {
1515
return ExpirationClaim(value: Date().addingTimeInterval(3600))
1616
}
17-
17+
1818
init(iss: String, sub: String, scope: String, aud: String) {
1919
self.exp = GAuthPayload.expirationClaim
2020
self.iat = IssuedAtClaim(value: Date())
@@ -23,26 +23,20 @@ class GAuthPayload: JWTPayload {
2323
self.scope = scope
2424
self.aud = AudienceClaim(value: aud)
2525
}
26-
27-
func verify() throws {
28-
try exp.verify()
26+
27+
func verify(using signer: JWTSigner) throws {
28+
// not used
2929
}
30-
31-
var isValid: Bool {
30+
31+
var hasExpired: Bool {
3232
do {
33-
try verify()
34-
return true
35-
} catch {
33+
try exp.verifyNotExpired()
3634
return false
35+
} catch {
36+
return true
3737
}
3838
}
39-
40-
func updateIfNeeded() {
41-
if !isValid {
42-
update()
43-
}
44-
}
45-
39+
4640
func update() {
4741
self.exp = GAuthPayload.expirationClaim
4842
self.iat = IssuedAtClaim(value: Date())

Sources/FCM/Helpers/FCM+AccessToken.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Vapor
33

44
extension FCM {
55
func getAccessToken(_ client: Client) throws -> Future<String> {
6-
if gAuthPayload.isValid, let token = accessToken {
6+
if !gAuthPayload.hasExpired, let token = accessToken {
77
return client.container.eventLoop.newSucceededFuture(result: token)
88
}
99
var payload: [String: String] = [:]

Sources/FCM/Helpers/FCM+JWT.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import JWT
44

55
extension FCM {
66
func generateJWT() throws -> String {
7-
gAuthPayload.updateIfNeeded()
87
let pk = try RSAKey.private(pem: key)
98
let signer = JWTSigner.rs256(key: pk)
10-
var jwt = JWT<GAuthPayload>(payload: gAuthPayload)
9+
let jwt = JWT<GAuthPayload>(payload: gAuthPayload)
1110
let jwtData = try jwt.sign(using: signer)
1211
return String(data: jwtData, encoding: .utf8)!
1312
}
1413

1514
func getJWT() throws -> String {
16-
if gAuthPayload.isValid {
15+
if !gAuthPayload.hasExpired {
1716
return _jwt
1817
}
1918
_jwt = try generateJWT()

0 commit comments

Comments
 (0)