-
Notifications
You must be signed in to change notification settings - Fork 82
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
Raise Unauthorized and Access Denied errors through callback [SDK-2480] #618
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -150,8 +151,8 @@ private void processEvent(@NonNull Context context, @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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The former was not used from the code. Replaced with a more meaningful extra that can hold the Exception instance with all the details. |
||
callback.onError(new LockException((AuthenticationException) data.getSerializableExtra(Constants.EXCEPTION_EXTRA))); | ||
Widcket marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
callback.onEvent(LockEvent.AUTHENTICATION, data); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
|
@@ -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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. covers the webauth flow for Lock |
||
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)); | ||
} | ||
|
||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. covers the database/enterprise flow for Lock |
||
if (error.isRuleError() || error.isAccessDenied()) { | ||
deliverAuthenticationError(error); | ||
return; | ||
} | ||
if (error.isVerificationRequired()) { | ||
completeDatabaseAuthenticationOnBrowser(); | ||
return; | ||
} | ||
final AuthenticationError authError = loginErrorBuilder.buildFrom(error); | ||
|
||
handler.post(() -> { | ||
lockView.showProgress(false); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed a few log lines, moved them up in the code and used the error code instead. These might be removed before the release. |
||
if (error.isVerificationRequired()) { | ||
completeDatabaseAuthenticationOnBrowser(); | ||
return; | ||
|
@@ -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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. covers the passwordless flow for PasswordlessLock |
||
deliverAuthenticationError(error); | ||
return; | ||
} | ||
handler.post(() -> showErrorMessage(loginErrorBuilder.buildFrom(error).getMessage(PasswordlessLockActivity.this))); | ||
} | ||
}; | ||
|
@@ -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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. covers the webauth flow for PasswordlessLock |
||
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)); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. checks that when an "exception extra" is present, the |
||
} | ||
|
||
@Test | ||
public void shouldCallOnCanceled() { | ||
Intent data = new Intent(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if an exception is present after an authentication event, raise it through the
onError
method