Skip to content

Commit 640acee

Browse files
authored
refactor: Replace synchronized block with AtomicReference for DPoP nonce management (#895)
1 parent 78c9ef2 commit 640acee

File tree

3 files changed

+6
-9
lines changed

3 files changed

+6
-9
lines changed

auth0/src/main/java/com/auth0/android/dpop/DPoP.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.auth0.android.request.HttpMethod
99
import com.auth0.android.request.getErrorBody
1010
import okhttp3.Response
1111
import java.lang.reflect.Modifier.PRIVATE
12+
import java.util.concurrent.atomic.AtomicReference
1213

1314

1415
/**
@@ -102,12 +103,11 @@ public class DPoP(context: Context) {
102103
private const val AUTHORIZATION_HEADER = "Authorization"
103104
private const val NONCE_HEADER = "DPoP-Nonce"
104105

105-
@Volatile
106106
@VisibleForTesting(otherwise = PRIVATE)
107-
internal var _auth0Nonce: String? = null
107+
internal val _auth0Nonce: AtomicReference<String?> = AtomicReference(null)
108108

109109
public val auth0Nonce: String?
110-
get() = _auth0Nonce
110+
get() = _auth0Nonce.get()
111111

112112
/**
113113
* Stores the nonce value from the Okhttp3 [Response] headers.
@@ -127,9 +127,7 @@ public class DPoP(context: Context) {
127127
@JvmStatic
128128
internal fun storeNonce(response: Response) {
129129
response.headers[NONCE_HEADER]?.let {
130-
synchronized(this) {
131-
_auth0Nonce = it
132-
}
130+
_auth0Nonce.set(it)
133131
}
134132
}
135133

@@ -220,7 +218,7 @@ public class DPoP(context: Context) {
220218
@JvmStatic
221219
public fun clearKeyPair() {
222220
DPoPUtil.clearKeyPair()
223-
_auth0Nonce = null
221+
_auth0Nonce.set(null)
224222
}
225223
}
226224
}

auth0/src/main/java/com/auth0/android/dpop/DPoPUtil.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ internal object DPoPUtil {
2929

3030

3131
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
32-
@Volatile
3332
internal var keyStore = DPoPKeyStore()
3433

3534

auth0/src/test/java/com/auth0/android/dpop/DPoPTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class DPoPTest {
5151
whenever(mockContext.applicationContext).thenReturn(mockContext)
5252
dPoP = DPoP(mockContext)
5353

54-
DPoP._auth0Nonce = null
54+
DPoP._auth0Nonce.set(null)
5555

5656
DPoPUtil.keyStore = mockKeyStore
5757
}

0 commit comments

Comments
 (0)