Skip to content

Commit

Permalink
Merge branch 'v3' into fix-broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda authored Apr 28, 2021
2 parents 15953ca + 7b7166f commit 353cb67
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 11 deletions.
8 changes: 7 additions & 1 deletion MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ The core SDK has been updated to the version 2+. Since this is exposed as an API

## Changes in behavior

### Lock lifecycle

The widget registers a Broadcast Listener to expect and handle the different lifecycle events. The listener is registered as soon as a new instance of `Lock` or `PasswordlessLock` is created with the corresponding Builder class, and the listener is unregistered when the `onDestroy` method is invoked. Forgetting to call this method would retain unnecessary resources after the authentication is complete and the widget is no longer required, or cause the callback to receive duplicated calls.

In case you are not currently calling it, make sure to update your code adding the `lock?.onDestroy(this)` call.
Expand Down Expand Up @@ -103,4 +105,8 @@ class MyActivity : AppCompatActivity() {
lock?.onDestroy(this)
}
}
```
```

### Non-recoverable errors

The `LockCallback` will get its `onError` method invoked when an [Auth0 Rule](https://auth0.com/docs/rules) returns an `Error` or `UnauthorizedError`. This was previously handled internally by Lock, causing it to display an orange toast with a generic failure message. From this release on, if you are using Auth0 Rules and throwing custom errors, you should obtain the _cause_ of the exception and read the code or description values to understand what went wrong.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import androidx.annotation.NonNull;

import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.lock.utils.LockException;
import com.auth0.android.result.Credentials;

import java.util.Date;
Expand Down Expand Up @@ -75,6 +77,11 @@ public void onEvent(@LockEvent int event, @NonNull Intent data) {
* @param data the intent received at the end of the login process.
*/
private void parseAuthentication(Intent data) {
if (data.hasExtra(Constants.EXCEPTION_EXTRA)) {
AuthenticationException error = (AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA);
onError(new LockException(error));
return;
}
String idToken = data.getStringExtra(Constants.ID_TOKEN_EXTRA);
String accessToken = data.getStringExtra(Constants.ACCESS_TOKEN_EXTRA);
String tokenType = data.getStringExtra(Constants.TOKEN_TYPE_EXTRA);
Expand Down
1 change: 1 addition & 0 deletions lib/src/main/java/com/auth0/android/lock/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class Constants {
static final String CANCELED_ACTION = "com.auth0.android.lock.action.Canceled";
static final String INVALID_CONFIGURATION_ACTION = "com.auth0.android.lock.action.InvalidConfiguration";

static final String EXCEPTION_EXTRA = "com.auth0.android.lock.extra.Exception";
static final String ERROR_EXTRA = "com.auth0.android.lock.extra.Error";
static final String ID_TOKEN_EXTRA = "com.auth0.android.lock.extra.IdToken";
static final String ACCESS_TOKEN_EXTRA = "com.auth0.android.lock.extra.AccessToken";
Expand Down
5 changes: 3 additions & 2 deletions lib/src/main/java/com/auth0/android/lock/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.auth0.android.Auth0;
import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.lock.LockCallback.LockEvent;
import com.auth0.android.lock.internal.configuration.Options;
import com.auth0.android.lock.internal.configuration.Theme;
Expand Down Expand Up @@ -151,8 +152,8 @@ private void processEvent(@NonNull Intent data) {
switch (action) {
case Constants.AUTHENTICATION_ACTION:
Log.v(TAG, "AUTHENTICATION action received in our BroadcastReceiver");
if (data.hasExtra(Constants.ERROR_EXTRA)) {
callback.onError(new LockException(data.getStringExtra(Constants.ERROR_EXTRA)));
if (data.hasExtra(Constants.EXCEPTION_EXTRA)) {
callback.onError(new LockException((AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA)));
} else {
callback.onEvent(LockEvent.AUTHENTICATION, data);
}
Expand Down
26 changes: 21 additions & 5 deletions lib/src/main/java/com/auth0/android/lock/LockActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ private void deliverAuthenticationResult(Credentials credentials) {
finish();
}

private void deliverAuthenticationError(AuthenticationException exception) {
Intent intent = new Intent(Constants.AUTHENTICATION_ACTION);
intent.putExtra(Constants.EXCEPTION_EXTRA, exception);

LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
finish();
}

private void deliverSignUpResult(DatabaseUser result) {
Intent intent = new Intent(Constants.SIGN_UP_ACTION);
intent.putExtra(Constants.EMAIL_EXTRA, result.getEmail());
Expand Down Expand Up @@ -484,9 +492,13 @@ public void onFailure(@NonNull final Dialog dialog) {

@Override
public void onFailure(@NonNull final AuthenticationException exception) {
Log.e(TAG, "Failed to authenticate the user: " + exception.getCode(), exception);
if (exception.isRuleError() || exception.isAccessDenied()) {
deliverAuthenticationError(exception);
return;
}
final AuthenticationError authError = loginErrorBuilder.buildFrom(exception);
final String message = authError.getMessage(LockActivity.this);
Log.e(TAG, "Failed to authenticate the user: " + message, exception);
handler.post(() -> showErrorMessage(message));
}

Expand All @@ -506,12 +518,16 @@ public void onSuccess(@Nullable Credentials credentials) {

@Override
public void onFailure(@NonNull final AuthenticationException error) {
Log.e(TAG, "Failed to authenticate the user: " + error.getMessage(), error);
final AuthenticationError authError = loginErrorBuilder.buildFrom(error);
Log.e(TAG, "Failed to authenticate the user: " + error.getCode(), error);
if (error.isRuleError() || error.isAccessDenied()) {
deliverAuthenticationError(error);
return;
}
if (error.isVerificationRequired()) {
completeDatabaseAuthenticationOnBrowser();
return;
}
final AuthenticationError authError = loginErrorBuilder.buildFrom(error);

handler.post(() -> {
lockView.showProgress(false);
Expand Down Expand Up @@ -542,7 +558,7 @@ public void onSuccess(@Nullable final DatabaseUser user) {

@Override
public void onFailure(@NonNull final AuthenticationException error) {
Log.e(TAG, "Failed to create the user: " + error.getMessage(), error);
Log.e(TAG, "Failed to create the user: " + error.getCode(), error);
if (error.isVerificationRequired()) {
completeDatabaseAuthenticationOnBrowser();
return;
Expand All @@ -568,7 +584,7 @@ public void onSuccess(@Nullable Void payload) {

@Override
public void onFailure(@NonNull AuthenticationException error) {
Log.e(TAG, "Failed to reset the user password: " + error.getMessage(), error);
Log.e(TAG, "Failed to reset the user password: " + error.getCode(), error);
handler.post(() -> {
String message = new AuthenticationError(R.string.com_auth0_lock_db_message_change_password_error).getMessage(LockActivity.this);
showErrorMessage(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.auth0.android.Auth0;
import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.lock.LockCallback.LockEvent;
import com.auth0.android.lock.internal.configuration.Options;
import com.auth0.android.lock.internal.configuration.Theme;
Expand Down Expand Up @@ -150,8 +151,8 @@ private void processEvent(Intent data) {
switch (action) {
case Constants.AUTHENTICATION_ACTION:
Log.v(TAG, "AUTHENTICATION action received in our BroadcastReceiver");
if (data.getExtras().containsKey(Constants.ERROR_EXTRA)) {
callback.onError(new LockException(data.getStringExtra(Constants.ERROR_EXTRA)));
if (data.hasExtra(Constants.EXCEPTION_EXTRA)) {
callback.onError(new LockException((AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA)));
} else {
callback.onEvent(LockEvent.AUTHENTICATION, data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ private void deliverAuthenticationResult(Credentials credentials) {
finish();
}

private void deliverAuthenticationError(AuthenticationException exception) {
Intent intent = new Intent(Constants.AUTHENTICATION_ACTION);
intent.putExtra(Constants.EXCEPTION_EXTRA, exception);

LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
finish();
}

private void showErrorMessage(String message) {
resultMessage.setBackgroundColor(ContextCompat.getColor(this, R.color.com_auth0_lock_result_message_error_background));
resultMessage.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -494,6 +502,10 @@ public void onSuccess(@Nullable Credentials credentials) {
@Override
public void onFailure(@NonNull final AuthenticationException error) {
Log.e(TAG, "Failed to authenticate the user: " + error.getMessage(), error);
if (error.isRuleError() || error.isAccessDenied()) {
deliverAuthenticationError(error);
return;
}
handler.post(() -> showErrorMessage(loginErrorBuilder.buildFrom(error).getMessage(PasswordlessLockActivity.this)));
}
};
Expand All @@ -507,9 +519,13 @@ public void onFailure(@NonNull final Dialog dialog) {

@Override
public void onFailure(@NonNull final AuthenticationException exception) {
Log.e(TAG, "Failed to authenticate the user: " + exception.getCode(), exception);
if (exception.isRuleError() || exception.isAccessDenied()) {
deliverAuthenticationError(exception);
return;
}
final AuthenticationError authError = loginErrorBuilder.buildFrom(exception);
final String message = authError.getMessage(PasswordlessLockActivity.this);
Log.e(TAG, "Failed to authenticate the user: " + message, exception);
handler.post(() -> showErrorMessage(message));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@

import androidx.annotation.NonNull;

import com.auth0.android.authentication.AuthenticationException;


public class LockException extends Exception {

public LockException(@NonNull String message) {
super(message);
}

public LockException(@NonNull AuthenticationException exception) {
super(exception);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import android.content.Intent;

import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.lock.LockCallback.LockEvent;
import com.auth0.android.lock.utils.MockLockCallback;
import com.auth0.android.result.Credentials;
Expand All @@ -39,6 +40,7 @@
import java.util.Date;

import static com.auth0.android.lock.utils.AuthenticationCallbackMatcher.hasAuthentication;
import static com.auth0.android.lock.utils.AuthenticationCallbackMatcher.hasError;
import static com.auth0.android.lock.utils.AuthenticationCallbackMatcher.hasNoError;
import static com.auth0.android.lock.utils.AuthenticationCallbackMatcher.isCanceled;
import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -80,6 +82,16 @@ public void shouldReturnAuthentication() {
assertThat(callback, hasNoError());
}

@Test
public void shouldReturnAuthenticationError() {
Intent data = new Intent();
AuthenticationException error = new AuthenticationException("err_code", "err description");
data.putExtra(Constants.EXCEPTION_EXTRA, error);
callback.onEvent(LockEvent.AUTHENTICATION, data);

assertThat(callback, hasError());
}

@Test
public void shouldCallOnCanceled() {
Intent data = new Intent();
Expand Down

0 comments on commit 353cb67

Please sign in to comment.