Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: incorporated changes planned for v3 #751

Merged
merged 5 commits into from
Jul 31, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.auth0.android.authentication
import androidx.annotation.VisibleForTesting
import com.auth0.android.Auth0
import com.auth0.android.Auth0Exception
import com.auth0.android.NetworkErrorException
import com.auth0.android.request.*
import com.auth0.android.request.internal.*
import com.auth0.android.request.internal.GsonAdapter.Companion.forMap
import com.auth0.android.request.internal.GsonAdapter.Companion.forMapOf
import com.auth0.android.request.internal.ResponseUtils.isNetworkError
import com.auth0.android.result.Challenge
import com.auth0.android.result.Credentials
import com.auth0.android.result.DatabaseUser
Expand Down Expand Up @@ -815,6 +817,12 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
}

override fun fromException(cause: Throwable): AuthenticationException {
if (isNetworkError(cause)) {
return AuthenticationException(
"Failed to execute the network request",
NetworkErrorException(cause)
)
}
return AuthenticationException(
"Something went wrong",
Auth0Exception("Something went wrong", cause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import android.text.TextUtils
import android.util.Log
import com.auth0.android.Auth0Exception
import com.auth0.android.NetworkErrorException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import com.auth0.android.provider.TokenValidationException
import java.net.SocketException

public class AuthenticationException : Auth0Exception {
private var code: String? = null
Expand Down Expand Up @@ -107,15 +104,8 @@ public class AuthenticationException : Auth0Exception {
}

// When the request failed due to network issues
// Currently [NetworkErrorException] is not properly thrown from [createErrorAdapter] in
// [AuthenticationAPIClient] and [UserAPIClient]. This will be fixed in the next major to avoid
// breaking change in the current major. We are not using IOException to check for the error
// since it is too broad.
public val isNetworkError: Boolean
get() = cause is NetworkErrorException
|| cause?.cause is UnknownHostException
|| cause?.cause is SocketTimeoutException
|| cause?.cause is SocketException

// When there is no Browser app installed to handle the web authentication
public val isBrowserAppNotAvailable: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.auth0.android.callback.Callback
import com.auth0.android.result.Credentials
import com.auth0.android.util.Clock
import java.util.*

/**
* Base class meant to abstract common logic across Credentials Manager implementations.
* The scope of this class is package-private, as it's not meant to be exposed
Expand Down Expand Up @@ -35,6 +36,65 @@ public abstract class BaseCredentialsManager internal constructor(
callback: Callback<Credentials, CredentialsManagerException>
)

public abstract fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
callback: Callback<Credentials, CredentialsManagerException>
)

public abstract fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
forceRefresh: Boolean,
callback: Callback<Credentials, CredentialsManagerException>
)

public abstract fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
headers: Map<String, String>,
forceRefresh: Boolean,
callback: Callback<Credentials, CredentialsManagerException>
)

@JvmSynthetic
@Throws(CredentialsManagerException::class)
public abstract suspend fun awaitCredentials(): Credentials

@JvmSynthetic
@Throws(CredentialsManagerException::class)
public abstract suspend fun awaitCredentials(scope: String?, minTtl: Int): Credentials

@JvmSynthetic
@Throws(CredentialsManagerException::class)
public abstract suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>
): Credentials

@JvmSynthetic
@Throws(CredentialsManagerException::class)
public abstract suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
forceRefresh: Boolean
): Credentials

@JvmSynthetic
@Throws(CredentialsManagerException::class)
public abstract suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
headers: Map<String, String>,
forceRefresh: Boolean
): Credentials

public abstract fun clearCredentials()
public abstract fun hasValidCredentials(): Boolean
public abstract fun hasValidCredentials(minTtl: Long): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(): Credentials {
override suspend fun awaitCredentials(): Credentials {
return awaitCredentials(null, 0)
}

Expand All @@ -76,7 +76,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(scope: String?, minTtl: Int): Credentials {
override suspend fun awaitCredentials(scope: String?, minTtl: Int): Credentials {
return awaitCredentials(scope, minTtl, emptyMap())
}

Expand All @@ -92,7 +92,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(
override suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>
Expand All @@ -113,7 +113,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(
override suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand All @@ -136,7 +136,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(
override suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand Down Expand Up @@ -200,7 +200,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
* @param parameters additional parameters to send in the request to refresh expired credentials
* @param callback the callback that will receive a valid [Credentials] or the [CredentialsManagerException].
*/
public fun getCredentials(
override fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand All @@ -220,7 +220,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
* @param forceRefresh this will avoid returning the existing credentials and retrieves a new one even if valid credentials exist.
* @param callback the callback that will receive a valid [Credentials] or the [CredentialsManagerException].
*/
public fun getCredentials(
override fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand All @@ -242,7 +242,7 @@ public class CredentialsManager @VisibleForTesting(otherwise = VisibleForTesting
* @param forceRefresh this will avoid returning the existing credentials and retrieves a new one even if valid credentials exist.
* @param callback the callback that will receive a valid [Credentials] or the [CredentialsManagerException].
*/
public fun getCredentials(
override fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(): Credentials {
override suspend fun awaitCredentials(): Credentials {
return awaitCredentials(null, 0)
}

Expand All @@ -157,7 +157,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(
override suspend fun awaitCredentials(
scope: String?,
minTtl: Int
): Credentials {
Expand All @@ -179,7 +179,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(
override suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>
Expand Down Expand Up @@ -208,7 +208,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(
override suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand Down Expand Up @@ -240,7 +240,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
*/
@JvmSynthetic
@Throws(CredentialsManagerException::class)
public suspend fun awaitCredentials(
override suspend fun awaitCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand Down Expand Up @@ -315,7 +315,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
* @param parameters additional parameters to send in the request to refresh expired credentials
* @param callback the callback to receive the result in.
*/
public fun getCredentials(
override fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand Down Expand Up @@ -345,7 +345,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
* @param forceRefresh this will avoid returning the existing credentials and retrieves a new one even if valid credentials exist.
* @param callback the callback to receive the result in.
*/
public fun getCredentials(
override fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand Down Expand Up @@ -377,7 +377,7 @@ public class SecureCredentialsManager @VisibleForTesting(otherwise = VisibleForT
* @param forceRefresh this will avoid returning the existing credentials and retrieves a new one even if valid credentials exist.
* @param callback the callback to receive the result in.
*/
public fun getCredentials(
override fun getCredentials(
scope: String?,
minTtl: Int,
parameters: Map<String, String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.auth0.android.management
import androidx.annotation.VisibleForTesting
import com.auth0.android.Auth0
import com.auth0.android.Auth0Exception
import com.auth0.android.NetworkErrorException
import com.auth0.android.authentication.ParameterBuilder
import com.auth0.android.request.ErrorAdapter
import com.auth0.android.request.JsonAdapter
Expand All @@ -14,6 +15,7 @@ import com.auth0.android.request.internal.GsonAdapter.Companion.forListOf
import com.auth0.android.request.internal.GsonAdapter.Companion.forMap
import com.auth0.android.request.internal.GsonProvider
import com.auth0.android.request.internal.RequestFactory
import com.auth0.android.request.internal.ResponseUtils.isNetworkError
import com.auth0.android.result.UserIdentity
import com.auth0.android.result.UserProfile
import com.google.gson.Gson
Expand Down Expand Up @@ -219,6 +221,12 @@ public class UsersAPIClient @VisibleForTesting(otherwise = VisibleForTesting.PRI
}

override fun fromException(cause: Throwable): ManagementException {
if (isNetworkError(cause)) {
return ManagementException(
"Failed to execute the network request",
NetworkErrorException(cause)
)
}
return ManagementException(
"Something went wrong",
Auth0Exception("Something went wrong", cause)
Expand Down
10 changes: 4 additions & 6 deletions auth0/src/main/java/com/auth0/android/request/DefaultClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class DefaultClient @VisibleForTesting(otherwise = VisibleForTesting.PRIV
readTimeout: Int,
private val defaultHeaders: Map<String, String>,
enableLogging: Boolean,
private val gson: Gson,
sslSocketFactory: SSLSocketFactory?,
trustManager: X509TrustManager?
) : NetworkingClient {
Expand All @@ -40,11 +41,8 @@ public class DefaultClient @VisibleForTesting(otherwise = VisibleForTesting.PRIV
connectTimeout: Int = DEFAULT_TIMEOUT_SECONDS,
readTimeout: Int = DEFAULT_TIMEOUT_SECONDS,
defaultHeaders: Map<String, String> = mapOf(),
enableLogging: Boolean = false
) : this(connectTimeout, readTimeout, defaultHeaders, enableLogging, null, null)

//TODO: receive this via internal constructor parameters
private val gson: Gson = GsonProvider.gson
enableLogging: Boolean = false,
) : this(connectTimeout, readTimeout, defaultHeaders, enableLogging, GsonProvider.gson, null, null)

@get:VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal val okHttpClient: OkHttpClient
Expand All @@ -71,6 +69,7 @@ public class DefaultClient @VisibleForTesting(otherwise = VisibleForTesting.PRIV
.map { urlBuilder.addQueryParameter(it.key, it.value as String) }
requestBuilder.method(options.method.toString(), null)
}

else -> {
// add parameters as body
val body = gson.toJson(options.parameters).toRequestBody(APPLICATION_JSON_UTF8)
Expand All @@ -90,7 +89,6 @@ public class DefaultClient @VisibleForTesting(otherwise = VisibleForTesting.PRIV
val builder = OkHttpClient.Builder()

// logging
//TODO: OFF by default!
if (enableLogging) {
val logger: Interceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
Expand Down
8 changes: 1 addition & 7 deletions auth0/src/main/java/com/auth0/android/request/Request.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,14 @@ public interface Request<T, U : Auth0Exception> {
* Performs an async HTTP request against Auth0 API inside a Coroutine
* This is a Coroutine that is exposed only for Kotlin.
*
* Note: This method was added after the interface was released.
* It is defined as a default method for compatibility reasons.
* From version 3.0 on, the method will be abstract and all implementations of this interface
* will have to provide their own implementation.
*
* The default implementation throws an [UnsupportedOperationException].
*
* @throws Auth0Exception on failure
*/
@JvmSynthetic
@Throws(Auth0Exception::class)
public suspend fun await(): T {
throw UnsupportedOperationException("await")
}
public suspend fun await(): T

/**
* Executes the HTTP request against Auth0 API (blocking the current thread)
Expand Down

This file was deleted.

Loading
Loading