Skip to content

Commit

Permalink
Merge pull request #441 from auth0/rm-auth0-setters
Browse files Browse the repository at this point in the history
Remove timeouts and logging setters from Auth0 class
  • Loading branch information
lbalmaceda authored Jan 19, 2021
2 parents 0962a59 + d3721cf commit 1365c65
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 113 deletions.
5 changes: 4 additions & 1 deletion V2_MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ We will not provide support and will change these as required without any previo

- `setOIDCConformant(boolean enabled)` and `isOIDCConformant()` have been removed. The SDK now only supports OIDC-Conformant applications.
- `doNotSendTelemetry()` has been removed. There is no replacement.
- `setWriteTimeoutInSeconds(seconds)` and `getWriteTimeoutInSeconds(seconds)` have been removed. There is no replacement; only connect and read timeouts can be configured.
- `setWriteTimeoutInSeconds(seconds)` and `getWriteTimeoutInSeconds(seconds)` have been removed. There is no replacement.
- `setReadTimeoutInSeconds(seconds)` and `getReadTimeoutInSeconds(seconds)` have been removed. You can customize the read timeout directly on the `DefaultClient` class. Use `DefaultClient(readTimeout = 123)`.
- `setConnectTimeoutInSeconds(seconds)` and `getConnectTimeoutInSeconds(seconds)` have been removed. You can customize the connect timeout directly on the `DefaultClient` class. Use `DefaultClient(connectTimeout = 123)`.
- `setLoggingEnabled(boolean enabled)` and `isLoggingEnabled()` have been removed. You can enable the network traffic logger directly on the `DefaultClient` class. Use `DefaultClient(enableLogging = true)`. Use it for debugging purposes and never enable it on production environments.
- `setTLS12Enforced()` and `isTLS12Enforced()` have been removed. The SDK now supports modern TLS by default.

#### `AuthenticationAPIClient` methods removed or changed
Expand Down
47 changes: 1 addition & 46 deletions auth0/src/main/java/com/auth0/android/Auth0.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,7 @@ public open class Auth0 @JvmOverloads constructor(
/**
* The networking client instance used to make HTTP requests.
*/
public var networkingClient: NetworkingClient = recreateNetworkingClient()

/**
* Whether HTTP request and response info should be logged.
* This should only be set to `true` for debugging purposes in non-production environments, as sensitive information is included in the logs.
* Defaults to `false`.
*/
@Deprecated(
"Create a DefaultClient and specify enableLogging = true|false instead."
)
public var isLoggingEnabled: Boolean = false
set(value) {
field = value
recreateNetworkingClient()
}

/**
* The connection timeout for network requests, in seconds. Defaults to 10 seconds.
*/
@Deprecated(
"Create a DefaultClient and specify the connectTimeout instead."
)
public var connectTimeoutInSeconds: Int = DefaultClient.DEFAULT_TIMEOUT_SECONDS
set(value) {
field = value
recreateNetworkingClient()
}

/**
* The read timeout, in seconds, to use when executing requests. Default is ten seconds.
*/
@Deprecated(
"Create a DefaultClient and specify the readTimeout instead."
)
public var readTimeoutInSeconds: Int = DefaultClient.DEFAULT_TIMEOUT_SECONDS
set(value) {
field = value
recreateNetworkingClient()
}
public var networkingClient: NetworkingClient = DefaultClient()

/**
* Creates a new Auth0 instance with the 'com_auth0_client_id' and 'com_auth0_domain' values
Expand Down Expand Up @@ -183,11 +145,4 @@ public open class Auth0 @JvmOverloads constructor(
configurationUrl = resolveConfiguration(configurationDomain, domainUrl)
auth0UserAgent = Auth0UserAgent()
}

private fun recreateNetworkingClient() = DefaultClient(
connectTimeout = connectTimeoutInSeconds,
readTimeout = readTimeoutInSeconds,
defaultHeaders = emptyMap(),
enableLogging = isLoggingEnabled
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public class AuthenticationAPIClient @VisibleForTesting(otherwise = VisibleForTe
* ```
* @param auth0 account information
*/
public constructor(
auth0: Auth0
) : this(
public constructor(auth0: Auth0) : this(
auth0,
RequestFactory<AuthenticationException>(auth0.networkingClient, createErrorAdapter()),
GsonProvider.gson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class UsersAPIClient @VisibleForTesting(otherwise = VisibleForTesting.PRI
*/
public constructor(
auth0: Auth0,
token: String,
token: String
) : this(
auth0,
factoryForToken(token, auth0.networkingClient),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class LogoutManager(
builder.appendQueryParameter(key, value)
}
val uri = builder.build()
logDebug("Using the following Logout URI: $uri")
Log.d(TAG, "Using the following Logout URI: $uri")
return uri
}

Expand All @@ -51,12 +51,6 @@ internal class LogoutManager(
parameters[KEY_CLIENT_ID] = account.clientId
}

private fun logDebug(message: String) {
if (account.isLoggingEnabled) {
Log.d(TAG, message)
}
}

companion object {
private val TAG = LogoutManager::class.java.simpleName
private const val KEY_CLIENT_ID = "client_id"
Expand Down
11 changes: 2 additions & 9 deletions auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal class OAuthManager(
Log.w(TAG, "The response didn't contain any of these values: code, state")
return false
}
logDebug("The parsed CallbackURI contains the following values: $values")
Log.d(TAG, "The parsed CallbackURI contains the following parameters: ${values.keys}")
try {
assertNoError(values[KEY_ERROR], values[KEY_ERROR_DESCRIPTION])
assertValidState(parameters[KEY_STATE]!!, values[KEY_STATE])
Expand Down Expand Up @@ -161,7 +161,6 @@ internal class OAuthManager(
options.clock = Date(currentTimeInMillis)
try {
IdTokenVerifier().verify(decodedIdToken, options)
logDebug("Authenticated using web flow")
validationCallback.onSuccess(null)
} catch (exc: TokenValidationException) {
validationCallback.onFailure(exc)
Expand Down Expand Up @@ -212,7 +211,7 @@ internal class OAuthManager(
builder.appendQueryParameter(key, value)
}
val uri = builder.build()
logDebug("Using the following Authorize URI: $uri")
Log.d(TAG, "Using the following Authorize URI: $uri")
return uri
}

Expand Down Expand Up @@ -260,12 +259,6 @@ internal class OAuthManager(
}
}

private fun logDebug(message: String) {
if (account.isLoggingEnabled) {
Log.d(TAG, message)
}
}

companion object {
private val TAG = OAuthManager::class.java.simpleName
const val KEY_RESPONSE_TYPE = "response_type"
Expand Down
38 changes: 0 additions & 38 deletions auth0/src/test/java/com/auth0/android/Auth0Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,6 @@ public void setUp() {
when(context.getString(eq(333))).thenReturn(DOMAIN);
}

@Test
public void shouldHaveLoggingEnabled() {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN);
auth0.setLoggingEnabled(true);

assertThat(auth0.isLoggingEnabled(), is(true));
}

@Test
public void shouldNotHaveLoggingEnabled() {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN);
auth0.setLoggingEnabled(false);

assertThat(auth0.isLoggingEnabled(), is(false));
}

@Test
public void shouldHaveConnectTimeout() {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN);
auth0.setConnectTimeoutInSeconds(5);

assertThat(auth0.getConnectTimeoutInSeconds(), is(5));
}

@Test
public void shouldReadHaveTimeout() {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN);
auth0.setReadTimeoutInSeconds(15);

assertThat(auth0.getReadTimeoutInSeconds(), is(15));
}

@Test
public void shouldNotHaveLoggingEnabledByDefault() {
Auth0 auth0 = new Auth0(CLIENT_ID, DOMAIN);
assertThat(auth0.isLoggingEnabled(), is(false));
}

@Test
public void shouldBuildFromResources() {
Resources resources = Mockito.mock(Resources.class);
Expand Down
48 changes: 42 additions & 6 deletions sample/src/main/java/com/auth0/sample/DatabaseLoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import com.auth0.android.Auth0
import com.auth0.android.authentication.AuthenticationAPIClient
import com.auth0.android.authentication.AuthenticationException
import com.auth0.android.callback.AuthenticationCallback
import com.auth0.android.callback.Callback
import com.auth0.android.provider.WebAuthProvider
import com.auth0.android.request.DefaultClient
import com.auth0.android.result.Credentials
import com.auth0.sample.databinding.FragmentDatabaseLoginBinding
import com.google.android.material.snackbar.Snackbar
Expand All @@ -18,9 +21,14 @@ import com.google.android.material.snackbar.Snackbar
*/
class DatabaseLoginFragment : Fragment() {

private val apiClient: AuthenticationAPIClient by lazy {
private val account: Auth0 by lazy {
val account = Auth0("esCyeleWIb1iKJUcz6fVR4e29mEHkn0O", "lbalmaceda.auth0.com")
account.isLoggingEnabled = true
// Only enable network traffic logging on production environments!
account.networkingClient = DefaultClient(enableLogging = true)
account
}

private val apiClient: AuthenticationAPIClient by lazy {
AuthenticationAPIClient(account)
}

Expand All @@ -32,22 +40,50 @@ class DatabaseLoginFragment : Fragment() {
binding.buttonLogin.setOnClickListener {
val email = binding.textEmail.text.toString()
val password = binding.textPassword.text.toString()
makeRequest(email, password)
dbLogin(email, password)
}
binding.buttonWebAuth.setOnClickListener {
webAuth()
}
return binding.root
}

private fun makeRequest(email: String, password: String) {
private fun dbLogin(email: String, password: String) {
apiClient.login(email, password, "Username-Password-Authentication")
//Additional customization to the request goes here
.start(object : AuthenticationCallback<Credentials> {
override fun onFailure(error: AuthenticationException) {
Snackbar.make(requireView(), "Failure :(", Snackbar.LENGTH_LONG).show()
Snackbar.make(requireView(), error.getDescription(), Snackbar.LENGTH_LONG)
.show()
}

override fun onSuccess(payload: Credentials?) {
Snackbar.make(
requireView(),
"Success: ${payload!!.accessToken}",
Snackbar.LENGTH_LONG
).show()
}
})
}

private fun webAuth() {
WebAuthProvider.login(account)
.start(requireContext(), object : Callback<Credentials, AuthenticationException> {
override fun onSuccess(payload: Credentials?) {
Snackbar.make(requireView(), "Success :D", Snackbar.LENGTH_LONG).show()
Snackbar.make(
requireView(),
"Success: ${payload!!.accessToken}",
Snackbar.LENGTH_LONG
).show()
}

override fun onFailure(error: AuthenticationException) {
val message =
if (error.isCanceled) "Browser was closed" else error.getDescription()
Snackbar.make(requireView(), message, Snackbar.LENGTH_LONG).show()
}

})
}
}
39 changes: 37 additions & 2 deletions sample/src/main/res/layout/fragment_database_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,54 @@
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:text="Log in using your email and password."
android:text="Log in using your email and password"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/textEmail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Log in using Auth0's Universal Login "
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Log in"
android:text="Log in with Username &amp; Password"
app:layout_constraintEnd_toEndOf="@+id/textPassword"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/textPassword"
app:layout_constraintTop_toBottomOf="@+id/textPassword" />

<Button
android:id="@+id/buttonWebAuth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Log in with Browser"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="or"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonLogin" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 1365c65

Please sign in to comment.