diff --git a/Sources/VaporNotifications/Apple/APNSErrors.swift b/Sources/VaporNotifications/Apple/APNSErrors.swift index 072774f..85843b0 100644 --- a/Sources/VaporNotifications/Apple/APNSErrors.swift +++ b/Sources/VaporNotifications/Apple/APNSErrors.swift @@ -8,7 +8,7 @@ import Foundation public struct APNSError: Codable { - let reason: APNSErrors + public let reason: APNSErrors } public enum APNSErrors: String, Codable { case BadCollapseId = "BadCollapseId" diff --git a/Sources/VaporNotifications/Apple/APNSMessage.swift b/Sources/VaporNotifications/Apple/APNSMessage.swift index 7e583a5..5f4be86 100644 --- a/Sources/VaporNotifications/Apple/APNSMessage.swift +++ b/Sources/VaporNotifications/Apple/APNSMessage.swift @@ -54,7 +54,11 @@ public struct ApplePushMessage { if let threadId = threadIdentifier { headers.add(name: HTTPHeaderName("thread-id"), value: threadId) } - headers.add(name: HTTPHeaderName("authorization"), value: "bearer \(profile.Token)") + if profile.tokenExpiration <= Date() { + try? profile.generateToken() + } + + headers.add(name: HTTPHeaderName("authorization"), value: "bearer \(profile.Token ?? "")") return headers } @@ -98,7 +102,7 @@ public struct ApplePushMessage { struct APNSJWTPayload: JWTPayload { let iss: String let iat = IssuedAtClaim(value: Date()) - let exp = ExpirationClaim(value: Date(timeInterval: 10000, since: Date())) + let exp = ExpirationClaim(value: Date(timeInterval: 3500, since: Date())) func verify() throws { } } diff --git a/Sources/VaporNotifications/Apple/APNSProfile.swift b/Sources/VaporNotifications/Apple/APNSProfile.swift index f8000d3..a7a9f00 100644 --- a/Sources/VaporNotifications/Apple/APNSProfile.swift +++ b/Sources/VaporNotifications/Apple/APNSProfile.swift @@ -8,7 +8,7 @@ import Foundation import JWT -public struct APNSProfile { +public class APNSProfile { public enum Port: Int { case p443 = 443, p2197 = 2197 } @@ -17,8 +17,8 @@ public struct APNSProfile { public var port: Port = .p443 public var teamId: String public var keyId: String - public let Token: String - + public var Token: String? + public var tokenExpiration: Date = Date() var privateKey: Data var publicKey: Data @@ -53,10 +53,13 @@ public struct APNSProfile { let (priv, pub) = KeyUtilities.generateKeys(Path: keyPath) self.publicKey = pub self.privateKey = priv + try generateToken() + } + + func generateToken() throws { let JWTheaders = JWTHeader(alg: "ES256", cty: nil, crit: nil, kid: keyId) let payload = APNSJWTPayload(iss: teamId) - let signer = JWTSigner.es256(key: privateKey) var jwt = JWT(header: JWTheaders, payload: payload) @@ -66,6 +69,7 @@ public struct APNSProfile { guard let token = stringToken else { throw TokenError.tokenWasNotGeneratedCorrectly } + tokenExpiration = Date(timeInterval: 3500, since: Date()) self.Token = token }