Skip to content

Commit 722dd0e

Browse files
committed
Identity Model Repo listens for JWT changes
* As a refresher, the Identity Model Repo holds all Identity Models present during an app run, which can include past users that have pending updates, while the Identity Model Store contains only the current user's Identity Model. * Let the OSIdentityModelRepo be the listener for changes to JWT token updates, and it needs to pass on that information to the User Manager who manages the JWT Config and fires other listeners of token changes.
1 parent afcfda4 commit 722dd0e

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalCommonDefines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ typedef enum {GET, POST, HEAD, PUT, DELETE, OPTIONS, CONNECT, TRACE, PATCH} HTTP
327327
#define OS_PUSH_SUBSCRIPTION_MODEL_STORE_KEY @"OS_PUSH_SUBSCRIPTION_MODEL_STORE_KEY"
328328
#define OS_SUBSCRIPTION_MODEL_STORE_KEY @"OS_SUBSCRIPTION_MODEL_STORE_KEY"
329329
#define OS_MODEL_STORE_LISTENER_POSTFIX @"_LISTENER"
330+
#define OS_IDENTITY_MODEL_REPO @"OS_IDENTITY_MODEL_REPO"
330331

331332
// Deltas
332333
#define OS_ADD_ALIAS_DELTA @"OS_ADD_ALIAS_DELTA"

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSIdentityModelRepo.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
import Foundation
29+
import OneSignalCore
2930
import OneSignalOSCore
3031

3132
/**
@@ -45,6 +46,8 @@ class OSIdentityModelRepo {
4546
func add(model: OSIdentityModel) {
4647
lock.withLock {
4748
models[model.modelId] = model
49+
// listen for changes to model's JWT Token
50+
model.changeNotifier.subscribe(self, key: OS_IDENTITY_MODEL_REPO)
4851
}
4952
}
5053

@@ -54,6 +57,55 @@ class OSIdentityModelRepo {
5457
}
5558
}
5659

60+
func get(externalId: String) -> OSIdentityModel? {
61+
lock.withLock {
62+
for model in models.values {
63+
if model.externalId == externalId {
64+
return model
65+
}
66+
}
67+
return nil
68+
}
69+
}
70+
71+
/**
72+
There may be multiple Identity Models with the same external ID, so update them all.
73+
This can be optimized in the future to re-use an Identity Model if multiple logins are made for the same user.
74+
*/
75+
func updateJwtToken(externalId: String, token: String) {
76+
var found = false
77+
lock.withLock {
78+
for model in models.values {
79+
if model.externalId == externalId {
80+
model.jwtBearerToken = token
81+
found = true
82+
}
83+
}
84+
}
85+
if !found {
86+
OneSignalLog.onesignalLog(ONE_S_LOG_LEVEL.LL_ERROR, message: "Update User JWT called for external ID \(externalId) that does not exist")
87+
}
88+
}
89+
}
90+
91+
extension OSIdentityModelRepo: OSModelChangedHandler {
92+
/**
93+
Listen for updates to the JWT Token and notify the User Manager of this change.
94+
*/
95+
public func onModelUpdated(args: OSModelChangedArgs, hydrating: Bool) {
96+
guard
97+
args.property == OS_JWT_BEARER_TOKEN,
98+
let model = args.model as? OSIdentityModel,
99+
let externalId = model.externalId,
100+
let token = args.newValue as? String
101+
else {
102+
return
103+
}
104+
print("❌ OSIdentityModelRepo onModelUpdated for \(externalId): \(token)")
105+
OneSignalUserManagerImpl.sharedInstance.jwtConfig.onJwtTokenChanged(externalId: externalId, to: token)
106+
}
107+
}
108+
57109
extension OSIdentityModelRepo: OSLoggable {
58110
func logSelf() {
59111
print(

0 commit comments

Comments
 (0)