|
| 1 | +/* |
| 2 | + Modified MIT License |
| 3 | + |
| 4 | + Copyright 2026 OneSignal |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + 1. The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | + |
| 16 | + 2. All copies of substantial portions of the Software may only be used in connection |
| 17 | + with services provided by OneSignal. |
| 18 | + |
| 19 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +import Foundation |
| 29 | +import OneSignalCore |
| 30 | + |
| 31 | +/** |
| 32 | + Identity Verification token access, keyed by `external_id`. Executors depend on this rather than on |
| 33 | + the User Manager so that no JWT lookup reaches for a singleton. |
| 34 | + */ |
| 35 | +protocol OSUserJwtProviding: AnyObject { |
| 36 | + /// The token this user can sign with, or nil if the SDK holds none. |
| 37 | + func validJwt(externalId: String) -> String? |
| 38 | + |
| 39 | + /** |
| 40 | + Asks the app for a token for `externalId`. |
| 41 | + |
| 42 | + Returns `true` if this call is the one that asked, which happens at most once per external ID |
| 43 | + per session so a burst of concurrent callers does not fire the event repeatedly. |
| 44 | + */ |
| 45 | + @discardableResult |
| 46 | + func askForToken(externalId: String) -> Bool |
| 47 | + |
| 48 | + /// Parks the rejected token and asks the app for a replacement. Returns `true` if this call asked. |
| 49 | + @discardableResult |
| 50 | + func invalidateJwt(externalId: String, rejectedToken: String) -> Bool |
| 51 | +} |
| 52 | + |
| 53 | +final class OSUserJwtRepo: OSUserJwtProviding { |
| 54 | + private let identityModelRepo: OSIdentityModelRepo |
| 55 | + private let notifyInvalidated: (String) -> Void |
| 56 | + |
| 57 | + let lock = NSLock() |
| 58 | + /** |
| 59 | + External IDs the app has already been asked to re-sign. |
| 60 | + |
| 61 | + In memory only. A model decoded at launch can already hold the invalid sentinel, leaving nothing |
| 62 | + to transition, so a fresh session has to be able to ask again — otherwise an app that was asked |
| 63 | + in a previous run is never told it still owes a token. |
| 64 | + */ |
| 65 | + var askedForToken: Set<String> = [] |
| 66 | + |
| 67 | + init(identityModelRepo: OSIdentityModelRepo, notifyInvalidated: @escaping (String) -> Void) { |
| 68 | + self.identityModelRepo = identityModelRepo |
| 69 | + self.notifyInvalidated = notifyInvalidated |
| 70 | + } |
| 71 | + |
| 72 | + func validJwt(externalId: String) -> String? { |
| 73 | + return identityModelRepo.validJwt(externalId: externalId) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + Stores a token supplied by the app, and lets this user be asked again if it is ever rejected. |
| 78 | + Returns `false` for a token that was not stored, so callers do not go looking for held work to release. |
| 79 | + |
| 80 | + An unusable token is ignored rather than stored: it would replace a good token with nothing to sign |
| 81 | + with, and clearing the ask for it would have the SDK and the app trade asks and replies on every flush. |
| 82 | + A token for an external ID the SDK has no Identity Model for lands nowhere, so it is not treated as an |
| 83 | + answer either. |
| 84 | + */ |
| 85 | + @discardableResult |
| 86 | + func updateJwt(externalId: String, token: String) -> Bool { |
| 87 | + guard !token.isEmpty, token != OS_JWT_TOKEN_INVALID else { |
| 88 | + OneSignalLog.onesignalLog(.LL_ERROR, message: "OSUserJwtRepo.updateJwt ignored an unusable token for \(externalId)") |
| 89 | + return false |
| 90 | + } |
| 91 | + guard identityModelRepo.updateJwtToken(externalId: externalId, token: token) else { |
| 92 | + return false |
| 93 | + } |
| 94 | + lock.withLock { _ = askedForToken.remove(externalId) } |
| 95 | + return true |
| 96 | + } |
| 97 | + |
| 98 | + @discardableResult |
| 99 | + func askForToken(externalId: String) -> Bool { |
| 100 | + guard lock.withLock({ askedForToken.insert(externalId).inserted }) else { |
| 101 | + return false |
| 102 | + } |
| 103 | + notifyInvalidated(externalId) |
| 104 | + return true |
| 105 | + } |
| 106 | + |
| 107 | + /// External IDs already asked this session; cleared when a usable token is stored. |
| 108 | + func pendingTokenAsks() -> [String] { |
| 109 | + return lock.withLock { Array(askedForToken) } |
| 110 | + } |
| 111 | + |
| 112 | + @discardableResult |
| 113 | + func invalidateJwt(externalId: String, rejectedToken: String) -> Bool { |
| 114 | + // No model for this user means the token could not have come from here. A Request stamped |
| 115 | + // with an owner whose model was cleared for hydration lands here, and it retries once the |
| 116 | + // aliases come back. |
| 117 | + guard identityModelRepo.invalidateJwtToken(externalId: externalId, rejectedToken: rejectedToken) else { |
| 118 | + return false |
| 119 | + } |
| 120 | + OneSignalLog.onesignalLog(.LL_DEBUG, message: "OSUserJwtRepo invalidated JWT for externalId: \(externalId)") |
| 121 | + // A replacement that landed while the rejected Request was in flight leaves the token above |
| 122 | + // untouched, and the retry signs with it, so there is nothing to ask the app for. |
| 123 | + guard identityModelRepo.validJwt(externalId: externalId) == nil else { |
| 124 | + return false |
| 125 | + } |
| 126 | + return askForToken(externalId: externalId) |
| 127 | + } |
| 128 | +} |
0 commit comments