-
Notifications
You must be signed in to change notification settings - Fork 265
feat: [JWT-4] identity model JWT, the JWT repo, and the public API #1709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nan-li
wants to merge
2
commits into
nan/jwt-pr3-iv-gate
Choose a base branch
from
nan/jwt-pr4-identity-api
base: nan/jwt-pr3-iv-gate
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
48 changes: 48 additions & 0 deletions
48
iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSUserJwtInvalidatedEvent.swift
This file contains hidden or 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,48 @@ | ||
| /* | ||
| Modified MIT License | ||
|
|
||
| Copyright 2026 OneSignal | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| 1. The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| 2. All copies of substantial portions of the Software may only be used in connection | ||
| with services provided by OneSignal. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| */ | ||
|
|
||
| /** | ||
| Tells the app that the JWT it supplied for `externalId` is no longer accepted, so it should mint a | ||
| fresh one and hand it back through `OneSignal.updateUserJwt(externalId:token:)`. | ||
| */ | ||
| @objc public class OSUserJwtInvalidatedEvent: NSObject { | ||
| @objc public let externalId: String | ||
|
|
||
| init(externalId: String) { | ||
| self.externalId = externalId | ||
| } | ||
|
|
||
| @objc public func jsonRepresentation() -> NSDictionary { | ||
| return [ | ||
| "externalId": externalId | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| @objc public protocol OSUserJwtInvalidatedListener { | ||
| @objc func onUserJwtInvalidated(event: OSUserJwtInvalidatedEvent) | ||
| } |
128 changes: 128 additions & 0 deletions
128
iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSUserJwtRepo.swift
This file contains hidden or 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,128 @@ | ||
| /* | ||
| Modified MIT License | ||
|
|
||
| Copyright 2026 OneSignal | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| 1. The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| 2. All copies of substantial portions of the Software may only be used in connection | ||
| with services provided by OneSignal. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| */ | ||
|
|
||
| import Foundation | ||
| import OneSignalCore | ||
|
|
||
| /** | ||
| Identity Verification token access, keyed by `external_id`. Executors depend on this rather than on | ||
| the User Manager so that no JWT lookup reaches for a singleton. | ||
| */ | ||
| protocol OSUserJwtProviding: AnyObject { | ||
| /// The token this user can sign with, or nil if the SDK holds none. | ||
| func validJwt(externalId: String) -> String? | ||
|
|
||
| /** | ||
| Asks the app for a token for `externalId`. | ||
|
|
||
| Returns `true` if this call is the one that asked, which happens at most once per external ID | ||
| per session so a burst of concurrent callers does not fire the event repeatedly. | ||
| */ | ||
| @discardableResult | ||
| func askForToken(externalId: String) -> Bool | ||
|
|
||
| /// Parks the rejected token and asks the app for a replacement. Returns `true` if this call asked. | ||
| @discardableResult | ||
| func invalidateJwt(externalId: String, rejectedToken: String) -> Bool | ||
| } | ||
|
|
||
| final class OSUserJwtRepo: OSUserJwtProviding { | ||
| private let identityModelRepo: OSIdentityModelRepo | ||
| private let notifyInvalidated: (String) -> Void | ||
|
|
||
| let lock = NSLock() | ||
| /** | ||
| External IDs the app has already been asked to re-sign. | ||
|
|
||
| In memory only. A model decoded at launch can already hold the invalid sentinel, leaving nothing | ||
| to transition, so a fresh session has to be able to ask again — otherwise an app that was asked | ||
| in a previous run is never told it still owes a token. | ||
| */ | ||
| var askedForToken: Set<String> = [] | ||
|
|
||
| init(identityModelRepo: OSIdentityModelRepo, notifyInvalidated: @escaping (String) -> Void) { | ||
| self.identityModelRepo = identityModelRepo | ||
| self.notifyInvalidated = notifyInvalidated | ||
| } | ||
|
|
||
| func validJwt(externalId: String) -> String? { | ||
| return identityModelRepo.validJwt(externalId: externalId) | ||
| } | ||
|
|
||
| /** | ||
| Stores a token supplied by the app, and lets this user be asked again if it is ever rejected. | ||
| Returns `false` for a token that was not stored, so callers do not go looking for held work to release. | ||
|
|
||
| An unusable token is ignored rather than stored: it would replace a good token with nothing to sign | ||
| with, and clearing the ask for it would have the SDK and the app trade asks and replies on every flush. | ||
| A token for an external ID the SDK has no Identity Model for lands nowhere, so it is not treated as an | ||
| answer either. | ||
| */ | ||
| @discardableResult | ||
| func updateJwt(externalId: String, token: String) -> Bool { | ||
| guard !token.isEmpty, token != OS_JWT_TOKEN_INVALID else { | ||
| OneSignalLog.onesignalLog(.LL_ERROR, message: "OSUserJwtRepo.updateJwt ignored an unusable token for \(externalId)") | ||
| return false | ||
| } | ||
| guard identityModelRepo.updateJwtToken(externalId: externalId, token: token) else { | ||
| return false | ||
| } | ||
| lock.withLock { _ = askedForToken.remove(externalId) } | ||
| return true | ||
| } | ||
|
|
||
| @discardableResult | ||
| func askForToken(externalId: String) -> Bool { | ||
| guard lock.withLock({ askedForToken.insert(externalId).inserted }) else { | ||
| return false | ||
| } | ||
| notifyInvalidated(externalId) | ||
| return true | ||
| } | ||
|
|
||
| /// External IDs already asked this session; cleared when a usable token is stored. | ||
| func pendingTokenAsks() -> [String] { | ||
| return lock.withLock { Array(askedForToken) } | ||
| } | ||
|
|
||
| @discardableResult | ||
| func invalidateJwt(externalId: String, rejectedToken: String) -> Bool { | ||
| // No model for this user means the token could not have come from here. A Request stamped | ||
| // with an owner whose model was cleared for hydration lands here, and it retries once the | ||
| // aliases come back. | ||
| guard identityModelRepo.invalidateJwtToken(externalId: externalId, rejectedToken: rejectedToken) else { | ||
| return false | ||
| } | ||
| OneSignalLog.onesignalLog(.LL_DEBUG, message: "OSUserJwtRepo invalidated JWT for externalId: \(externalId)") | ||
| // A replacement that landed while the rejected Request was in flight leaves the token above | ||
| // untouched, and the retry signs with it, so there is nothing to ask the app for. | ||
| guard identityModelRepo.validJwt(externalId: externalId) == nil else { | ||
| return false | ||
| } | ||
| return askForToken(externalId: externalId) | ||
|
nan-li marked this conversation as resolved.
|
||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.