Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ android {
}
}

testOptions {
// AuthenticationException calls android.text.TextUtils, which plain JVM tests only have as a stub.
unitTests.returnDefaultValues = true
}

lintOptions {
disable "GradleCompatible"
abortOnError false
Expand Down
24 changes: 21 additions & 3 deletions android/src/main/java/com/auth0/react/A0Auth0Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeArray
import com.facebook.react.bridge.WritableNativeMap
import com.google.gson.Gson
Expand Down Expand Up @@ -116,6 +117,23 @@ class A0Auth0Module(private val reactContext: ReactApplicationContext) : A0Auth0
return builder.build()
}

/**
* The Auth0 error behind a failed renewal or exchange, for the rejection's userInfo, so apps can tell a refusal
* (for example access_denied) from a server error, which Auth0.Android both reports as API_ERROR.
* Null when the cause is not an Auth0 API error. statusCode is 0 when there was no HTTP response.
*/
internal fun authenticationErrorInfo(
cause: Throwable?,
newMap: () -> WritableMap = Arguments::createMap
): WritableMap? {
if (cause !is AuthenticationException) return null
return newMap().apply {
putString("error", cause.getCode())
putString("error_description", cause.getDescription())
putInt("statusCode", cause.statusCode)
}
}

// An unknown value falls back to the server default.
internal fun deliveryMethodFrom(value: String?): DeliveryMethod? =
DeliveryMethod.values().firstOrNull { it.value == value?.trim() }
Expand Down Expand Up @@ -517,7 +535,7 @@ class A0Auth0Module(private val reactContext: ReactApplicationContext) : A0Auth0
override fun onFailure(e: CredentialsManagerException) {
if (promise.rejectIfMultifactorRequired(e.mfaRequiredErrorPayload, e)) return
val errorCode = deduceErrorCode(e)
promise.reject(errorCode, credentialsMessage(e), e)
promise.reject(errorCode, credentialsMessage(e), e, authenticationErrorInfo(e.cause))
}
}
)
Expand Down Expand Up @@ -593,7 +611,7 @@ class A0Auth0Module(private val reactContext: ReactApplicationContext) : A0Auth0
override fun onFailure(e: CredentialsManagerException) {
if (promise.rejectIfMultifactorRequired(e.mfaRequiredErrorPayload, e)) return
val errorCode = deduceErrorCode(e)
promise.reject(errorCode, credentialsMessage(e), e)
promise.reject(errorCode, credentialsMessage(e), e, authenticationErrorInfo(e.cause))
}
}
)
Expand Down Expand Up @@ -764,7 +782,7 @@ class A0Auth0Module(private val reactContext: ReactApplicationContext) : A0Auth0

override fun onFailure(error: CredentialsManagerException) {
val errorCode = deduceErrorCode(error)
promise.reject(errorCode, credentialsMessage(error), error)
promise.reject(errorCode, credentialsMessage(error), error, authenticationErrorInfo(error.cause))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.auth0.react

import com.auth0.android.authentication.AuthenticationException
import com.facebook.react.bridge.JavaOnlyMap
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import java.io.IOException

class A0Auth0ModuleCredentialsErrorTest {

@Test
fun `authenticationErrorInfo passes the Auth0 error, its description and the status code`() {
val cause = AuthenticationException(
mapOf("error" to "access_denied", "error_description" to "Denied by a post-login Action"),
403
)

val info = A0Auth0Module.authenticationErrorInfo(cause) { JavaOnlyMap() }!!

assertEquals("access_denied", info.getString("error"))
assertEquals("Denied by a post-login Action", info.getString("error_description"))
assertEquals(403, info.getInt("statusCode"))
}

@Test
fun `authenticationErrorInfo is null when the cause is not an Auth0 API error`() {
assertNull(A0Auth0Module.authenticationErrorInfo(IOException("offline")) { JavaOnlyMap() })
assertNull(A0Auth0Module.authenticationErrorInfo(null) { JavaOnlyMap() })
}
}
5 changes: 5 additions & 0 deletions src/core/models/CredentialsManagerError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ const ERROR_CODE_MAP: Record<string, CredentialsManagerErrorCode> = {
* - `NO_NETWORK`: Network connectivity issue
* - `API_ERROR`: Generic API error
*
* On Android, a rejection caused by an Auth0 error also has `json.userInfo` with its `error`,
* `error_description` and `statusCode` (0 without an HTTP response). This tells a refusal such as
* `access_denied` from a server error, which both have the type `API_ERROR`. iOS puts the Auth0 error
* of a failed renewal in `code` instead.
*
* ### DPoP Credential State:
* - `DPOP_KEY_MISSING`: DPoP key pair no longer available in keystore/keychain
* - `DPOP_NOT_CONFIGURED`: Credentials are DPoP-bound but client not configured with DPoP
Expand Down