@@ -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
0 commit comments