Skip to content

Commit 0498227

Browse files
committed
Added test cases for the DPoPProvider class
1 parent e1e7033 commit 0498227

File tree

8 files changed

+552
-11
lines changed

8 files changed

+552
-11
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import com.auth0.android.Auth0Exception
44

55
public class DPoPException : Auth0Exception {
66

7-
public enum class Code {
7+
internal enum class Code {
88
KEY_GENERATION_ERROR,
99
KEY_STORE_ERROR,
10+
SIGNING_FAILURE,
1011
UNKNOWN,
1112
}
12-
13+
1314
private var code: Code? = null
1415

1516
internal constructor(
@@ -33,14 +34,21 @@ public class DPoPException : Auth0Exception {
3334
}
3435

3536

36-
private companion object {
37+
public companion object {
38+
39+
public val KEY_GENERATION_ERROR: DPoPException = DPoPException(Code.KEY_GENERATION_ERROR)
40+
public val KEY_STORE_ERROR: DPoPException = DPoPException(Code.KEY_STORE_ERROR)
41+
public val SIGNING_FAILURE: DPoPException = DPoPException(Code.SIGNING_FAILURE)
42+
public val UNKNOWN: DPoPException = DPoPException(Code.UNKNOWN)
43+
3744
private const val DEFAULT_MESSAGE =
3845
"An unknown error has occurred. Please check the error cause for more details."
3946

4047
private fun getMessage(code: Code): String {
4148
return when (code) {
4249
Code.KEY_GENERATION_ERROR -> "Error generating DPoP key pair."
4350
Code.KEY_STORE_ERROR -> "Error while accessing the key pair in the keystore."
51+
Code.SIGNING_FAILURE -> "Error while signing the DPoP proof."
4452
Code.UNKNOWN -> DEFAULT_MESSAGE
4553
}
4654
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.auth0.android.dpop
33
import android.content.Context
44
import android.util.Base64
55
import android.util.Log
6+
import androidx.annotation.VisibleForTesting
67
import com.auth0.android.request.getErrorBody
78
import okhttp3.Response
89
import org.json.JSONObject
@@ -34,13 +35,16 @@ public object DPoPProvider {
3435
private const val NONCE_HEADER = "dpop-nonce"
3536
public const val DPOP_HEADER: String = "DPoP"
3637

37-
private val keyStore = DPoPKeyStore()
3838

3939
public const val MAX_RETRY_COUNT: Int = 1
4040

4141
public var auth0Nonce: String? = null
4242
private set
4343

44+
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
45+
@Volatile
46+
internal var keyStore = DPoPKeyStore()
47+
4448
/**
4549
* This method constructs a DPoP proof JWT that includes the HTTP method, URL, and an optional access token and nonce.
4650
*
@@ -153,7 +157,7 @@ public object DPoPProvider {
153157
@Throws(DPoPException::class)
154158
public fun getPublicKeyJWK(): String? {
155159
if (!keyStore.hasKeyPair()) {
156-
Log.d(TAG, "getPublicKeyJWK: Key pair is not present to generate JWK")
160+
Log.e(TAG, "getPublicKeyJWK: Key pair is not present to generate JWK")
157161
return null
158162
}
159163

@@ -324,8 +328,8 @@ public object DPoPProvider {
324328
return encodeBase64Url(convertDerToRawSignature(signatureBytes))
325329
} catch (e: Exception) {
326330
Log.e(TAG, "Error signing data: ${e.stackTraceToString()}")
331+
throw DPoPException(DPoPException.Code.SIGNING_FAILURE, e)
327332
}
328-
return null
329333
}
330334

331335
private fun convertDerToRawSignature(derSignature: ByteArray): ByteArray {

auth0/src/main/java/com/auth0/android/request/internal/Jwt.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import java.util.*
1010
*/
1111
internal class Jwt(rawToken: String) {
1212

13-
private val decodedHeader: Map<String, Any>
14-
private val decodedPayload: Map<String, Any>
13+
val decodedHeader: Map<String, Any>
14+
val decodedPayload: Map<String, Any>
1515
val parts: Array<String>
1616

1717
// header

auth0/src/test/java/com/auth0/android/authentication/AuthenticationAPIClientTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ public class AuthenticationAPIClientTest {
592592
public fun shouldFetchUserInfo() {
593593
mockAPI.willReturnUserInfo()
594594
val callback = MockAuthenticationCallback<UserProfile>()
595-
client.userInfo("ACCESS_TOKEN")
595+
client.userInfo("ACCESS_TOKEN","Bearer")
596596
.start(callback)
597597
ShadowLooper.idleMainLooper()
598598
assertThat(
@@ -617,7 +617,7 @@ public class AuthenticationAPIClientTest {
617617
public fun shouldFetchUserInfoSync() {
618618
mockAPI.willReturnUserInfo()
619619
val profile = client
620-
.userInfo("ACCESS_TOKEN")
620+
.userInfo("ACCESS_TOKEN","Bearer")
621621
.execute()
622622
assertThat(profile, Matchers.`is`(Matchers.notNullValue()))
623623
val request = mockAPI.takeRequest()
@@ -638,7 +638,7 @@ public class AuthenticationAPIClientTest {
638638
public fun shouldAwaitFetchUserInfo(): Unit = runTest {
639639
mockAPI.willReturnUserInfo()
640640
val profile = client
641-
.userInfo("ACCESS_TOKEN")
641+
.userInfo("ACCESS_TOKEN","Bearer")
642642
.await()
643643
assertThat(profile, Matchers.`is`(Matchers.notNullValue()))
644644
val request = mockAPI.takeRequest()

auth0/src/test/java/com/auth0/android/authentication/request/AuthenticationRequestMock.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.auth0.android.authentication.AuthenticationException;
77
import com.auth0.android.callback.Callback;
88
import com.auth0.android.request.AuthenticationRequest;
9+
import com.auth0.android.request.HttpMethod;
910
import com.auth0.android.request.Request;
1011
import com.auth0.android.result.Credentials;
1112

@@ -112,4 +113,16 @@ public AuthenticationRequest withIdTokenVerificationLeeway(int leeway) {
112113
public AuthenticationRequest withIdTokenVerificationIssuer(@NonNull String issuer) {
113114
return this;
114115
}
116+
117+
@NonNull
118+
@Override
119+
public String getUrl() {
120+
return "";
121+
}
122+
123+
@NonNull
124+
@Override
125+
public HttpMethod getHttpMethod() {
126+
return HttpMethod.GET.INSTANCE;
127+
}
115128
}

auth0/src/test/java/com/auth0/android/authentication/request/RequestMock.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.auth0.android.Auth0Exception;
66
import com.auth0.android.callback.Callback;
7+
import com.auth0.android.request.HttpMethod;
78
import com.auth0.android.request.Request;
89

910
import java.util.Map;
@@ -40,6 +41,18 @@ public Request<T, U> addHeader(@NonNull String name, @NonNull String value) {
4041
return this;
4142
}
4243

44+
@NonNull
45+
@Override
46+
public String getUrl() {
47+
return "";
48+
}
49+
50+
@NonNull
51+
@Override
52+
public HttpMethod getHttpMethod() {
53+
return HttpMethod.GET.INSTANCE;
54+
}
55+
4356
@Override
4457
public void start(@NonNull Callback<T, U> callback) {
4558
started = true;

0 commit comments

Comments
 (0)