Skip to content

Commit a8eb5db

Browse files
committed
cleanup
renaming a variable and cleaning up comments
1 parent d1b065b commit a8eb5db

3 files changed

Lines changed: 13 additions & 25 deletions

File tree

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Modeling/OSIdentityModel.swift

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,17 @@ class OSIdentityModel: OSModel {
4444

4545
// MARK: - JWT
4646

47-
private var _jwtBearerToken: String?
47+
private var jwtBearerTokenLocked: String? // only read/write under self.lock
4848
public var jwtBearerToken: String? {
4949
get {
50-
lock.withLock { _jwtBearerToken }
50+
lock.withLock { jwtBearerTokenLocked }
5151
}
5252
set {
5353
// Lock only the storage write. The change notifier fires synchronously
54-
// to listeners that may take other locks; firing under our lock would
55-
// risk deadlock (NSRecursiveLock only saves same-thread re-entry).
56-
let changed: Bool = lock.withLock {
57-
guard newValue != _jwtBearerToken else { return false }
58-
_jwtBearerToken = newValue
54+
// to listeners that may take other locks
55+
let changed = lock.withLock {
56+
guard newValue != jwtBearerTokenLocked else { return false }
57+
jwtBearerTokenLocked = newValue
5958
return true
6059
}
6160
if changed {
@@ -64,10 +63,7 @@ class OSIdentityModel: OSModel {
6463
}
6564
}
6665

67-
/// Returns the bearer token if it is non-nil, non-empty, and not the
68-
/// `OS_JWT_TOKEN_INVALID` sentinel — otherwise nil. Snapshots once so the
69-
/// caller cannot split a read-then-check across two reads of a property
70-
/// that other threads can mutate.
66+
/// Returns the bearer token if it is valid, otherwise nil, snapshots once
7167
func getValidJwt() -> String? {
7268
let token = jwtBearerToken
7369
guard let token = token, !token.isEmpty, token != OS_JWT_TOKEN_INVALID else {
@@ -78,15 +74,13 @@ class OSIdentityModel: OSModel {
7874

7975
/**
8076
Atomically transition the JWT token to `OS_JWT_TOKEN_INVALID`. Returns
81-
`true` if the transition occurred, `false` if the token was already
82-
invalid. Used by `invalidateJwtForExternalId` so only the thread that
83-
actually invalidated fires `fireJwtExpired`.
77+
`true` if the transition occurred, `false` if the token was already invalid.
8478
*/
8579
@discardableResult
8680
func invalidateJwtBearerToken() -> Bool {
87-
let changed: Bool = lock.withLock {
88-
guard _jwtBearerToken != OS_JWT_TOKEN_INVALID else { return false }
89-
_jwtBearerToken = OS_JWT_TOKEN_INVALID
81+
let changed = lock.withLock {
82+
guard jwtBearerTokenLocked != OS_JWT_TOKEN_INVALID else { return false }
83+
jwtBearerTokenLocked = OS_JWT_TOKEN_INVALID
9084
return true
9185
}
9286
if changed {
@@ -107,7 +101,7 @@ class OSIdentityModel: OSModel {
107101
lock.withLock {
108102
super.encode(with: coder)
109103
coder.encode(aliases, forKey: "aliases")
110-
coder.encode(_jwtBearerToken, forKey: OS_JWT_BEARER_TOKEN)
104+
coder.encode(jwtBearerTokenLocked, forKey: OS_JWT_BEARER_TOKEN)
111105
}
112106
}
113107

@@ -117,7 +111,7 @@ class OSIdentityModel: OSModel {
117111
// log error
118112
return nil
119113
}
120-
self._jwtBearerToken = coder.decodeObject(forKey: OS_JWT_BEARER_TOKEN) as? String
114+
self.jwtBearerTokenLocked = coder.decodeObject(forKey: OS_JWT_BEARER_TOKEN) as? String
121115
self.aliases = aliases
122116
}
123117

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,10 +737,6 @@ extension OneSignalUserManagerImpl {
737737
return
738738
}
739739

740-
// Atomic compare-and-set on the model. Only the thread that actually
741-
// transitioned the token to INVALID fires the expired event — avoids
742-
// a needless re-auth round trip if a concurrent valid-token write
743-
// landed between a TOCTOU read/write pair.
744740
if identityModel.invalidateJwtBearerToken() {
745741
fireJwtExpired(externalId: externalId)
746742
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/Requests/OSUserRequest.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ internal extension OneSignalRequest {
7070
| --------------- | -------------- | ------- | ------- |
7171
*/
7272
func addJWTHeaderIsValid(identityModel: OSIdentityModel) -> Bool {
73-
// Snapshot once via getValidJwt() to avoid split read-then-check races
74-
// between concurrent writers (login/setUserJwtToken/invalidate).
7573
let validToken = identityModel.getValidJwt()
7674
let required = OneSignalUserManagerImpl.sharedInstance.jwtConfig.isRequired
7775
let canBeSent = (required == false) || (required == true && validToken != nil)

0 commit comments

Comments
 (0)