-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new errors, generate new token only after hour, tests
- Loading branch information
Showing
12 changed files
with
275 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// APNSBearerToken.swift | ||
// SwiftyAPNS | ||
// | ||
// Created by Sergii Tkachenko on 08.06.2022. | ||
// Copyright © 2022 Sergii Tkachenko. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
internal class APNSBearerToken { | ||
|
||
private let keyId: String | ||
private let teamId: String | ||
private var issuedAt: Date | ||
|
||
private let p8: P8 | ||
private var jwtEx: APNSJwt | ||
|
||
private var bearer: String? | ||
private var expired: Bool { | ||
return Date().timeIntervalSince(issuedAt) >= TimeInterval(60 * 59) | ||
} | ||
|
||
init(p8: P8, keyId: String, teamId: String, issuedAt: Date = Date()) { | ||
self.p8 = p8; self.keyId = keyId; self.teamId = teamId; self.issuedAt = issuedAt | ||
self.jwtEx = APNSJwt(keyId: keyId, teamId: teamId, issuedAt: issuedAt) | ||
} | ||
|
||
func generateIfExpired() throws -> String { | ||
if bearer == nil || expired { | ||
try generate() | ||
} | ||
guard let bearer = bearer else { | ||
throw APNSProviderError.emptyData | ||
} | ||
return bearer | ||
} | ||
|
||
private func generate() throws { | ||
issuedAt = Date() | ||
jwtEx = APNSJwt(keyId: keyId, teamId: teamId, issuedAt: issuedAt) | ||
bearer = try jwtEx.sign(with: p8) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.