Skip to content

Commit

Permalink
Merge pull request #233 from Simperium/add-account-verification
Browse files Browse the repository at this point in the history
Add account verification response handling during login
  • Loading branch information
AmandaRiu authored Aug 23, 2021
2 parents dee4cb9 + ca095b0 commit c0bbbb8
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 43 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ Trying to make using [Simperium][Simperium.com] in your Android app dead simple.

Simperium for Android is configured as an [Android Library Project][].

TODO: brief code example/tutorial :)

## Contributing

To get started first clone the project:
Expand All @@ -28,6 +26,23 @@ Please provide unit tests for your contributions. Run tests with gradle:

Unit tests use a mock networking and storage stack so that different components can be tested in isolation. The unit tests should not connect to any external services.

## Publish to S3

A new version of this library will be automatically published to S3 by CI in the following scenarios:

**Note**: `sha1` corresponds to the commit hash.

* For all tags -> Version: `{tag-name}`
* For all commits in `develop` (so PR merges) -> Version: `develop-{sha1}`
* For all commits for open PRs - you can open a draft PR to get it to publish -> Version: `{prNumber}-{sha1}`

## Usage

### Adding simperium as dependency

```groovy
implementation 'com.automattic:simperium:<version>'
```

[Android Studio]: http://developer.android.com/sdk/installing/studio.html
[Gradle]: http://www.gradleware.com
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.simperium.android;

import static com.simperium.android.AuthenticationActivity.EXTRA_IS_LOGIN;
import static org.apache.http.protocol.HTTP.UTF_8;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
Expand Down Expand Up @@ -43,9 +46,6 @@
import java.net.URLEncoder;
import java.util.regex.Pattern;

import static com.simperium.android.AuthenticationActivity.EXTRA_IS_LOGIN;
import static org.apache.http.protocol.HTTP.UTF_8;

public class CredentialsActivity extends AppCompatActivity {
private static final Pattern PATTERN_NEWLINES_RETURNS_TABS = Pattern.compile("[\n\r\t]");
private static final String EXTRA_AUTOMATE_LOGIN = "EXTRA_AUTOMATE_LOGIN";
Expand All @@ -62,9 +62,9 @@ public class CredentialsActivity extends AppCompatActivity {
private Simperium mSimperium;
private TextInputLayout mInputEmail;
private TextInputLayout mInputPassword;
private boolean mIsLogin;
protected boolean mIsLogin;

private AuthResponseListener mAuthListener = new AuthResponseListener() {
protected AuthResponseListener mAuthListener = new AuthResponseListener() {
@Override
public void onFailure(final User user, final AuthException error) {
runOnUiThread(
Expand Down Expand Up @@ -95,38 +95,11 @@ public void run() {

@Override
public void onSuccess(final User user, final String userId, final String token, final AuthProvider provider) {
runOnUiThread(
new Runnable() {
@Override
public void run() {
hideDialogProgress();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

if (inputMethodManager != null) {
inputMethodManager.hideSoftInputFromWindow(mButton.getWindowToken(), 0);
}

// Use isValidPasswordLength(false) to check if password meets PASSWORD_LENGTH_MINIMUM.
if (isValidPassword(user.getEmail(), user.getPassword()) && isValidPasswordLength(false)) {
user.setStatus(User.Status.AUTHORIZED);
user.setAccessToken(token);
user.setUserId(userId);
provider.saveUser(user);
setResult(RESULT_OK);
finish();
} else {
user.setStatus(User.Status.NOT_AUTHORIZED);
user.setAccessToken("");
user.setUserId("");
provider.saveUser(user);
showDialogErrorLoginReset();
}
}
}
);
handleResponseSuccess(user, userId, token, provider);
}
};


@Override
public void onBackPressed() {
startActivity(new Intent(CredentialsActivity.this, AuthenticationActivity.class));
Expand Down Expand Up @@ -309,6 +282,39 @@ public void run() {
}
}

protected void handleResponseSuccess(final User user, final String userId, final String token, final AuthProvider provider) {
runOnUiThread(
new Runnable() {
@Override
public void run() {
hideDialogProgress();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

if (inputMethodManager != null) {
inputMethodManager.hideSoftInputFromWindow(mButton.getWindowToken(), 0);
}

// Use isValidPasswordLength(false) to check if password meets PASSWORD_LENGTH_MINIMUM.
if (isValidPassword(user.getEmail(), user.getPassword()) && isValidPasswordLength(false)) {
user.setStatus(User.Status.AUTHORIZED);
user.setAccessToken(token);
user.setUserId(userId);
provider.saveUser(user);
setResult(RESULT_OK);
finish();
} else {
user.setStatus(User.Status.NOT_AUTHORIZED);
user.setAccessToken("");
user.setUserId("");
provider.saveUser(user);
showDialogErrorLoginReset();
}
}
}
);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Expand Down Expand Up @@ -354,7 +360,7 @@ private String getEditTextString(@NonNull TextInputLayout inputLayout) {
return inputLayout.getEditText() != null ? inputLayout.getEditText().getText().toString() : "";
}

private void hideDialogProgress() {
protected void hideDialogProgress() {
if (mProgressDialogFragment != null && !mProgressDialogFragment.isHidden()) {
mProgressDialogFragment.dismiss();
mProgressDialogFragment = null;
Expand Down Expand Up @@ -407,7 +413,7 @@ private void setEditTextString(@NonNull TextInputLayout inputLayout, String text
}
}

private void showDialogError(String message) {
protected void showDialogError(String message) {
hideDialogProgress();
Context context = new ContextThemeWrapper(CredentialsActivity.this, getTheme());
new AlertDialog.Builder(context)
Expand All @@ -417,7 +423,7 @@ private void showDialogError(String message) {
.show();
}

private void showDialogErrorExistingAccount() {
protected void showDialogErrorExistingAccount() {
hideDialogProgress();
Context context = new ContextThemeWrapper(CredentialsActivity.this, getTheme());
new AlertDialog.Builder(context)
Expand All @@ -441,7 +447,7 @@ public void onClick(DialogInterface dialog, int which) {
.show();
}

private void showDialogErrorLoginReset() {
void showDialogErrorLoginReset() {
hideDialogProgress();
final Context context = new ContextThemeWrapper(CredentialsActivity.this, getTheme());
new AlertDialog.Builder(context)
Expand Down Expand Up @@ -487,7 +493,7 @@ public void onClick(DialogInterface dialog, int which) {
.show();
}

private void showCompromisedPasswordDialog() {
protected void showCompromisedPasswordDialog() {
hideDialogProgress();
final Context context = new ContextThemeWrapper(CredentialsActivity.this, getTheme());
new AlertDialog.Builder(context)
Expand Down Expand Up @@ -524,12 +530,16 @@ private void startLogin() {
mProgressDialogFragment = ProgressDialogFragment.newInstance(getString(R.string.simperium_dialog_progress_logging_in));
mProgressDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Simperium);
mProgressDialogFragment.show(getSupportFragmentManager(), ProgressDialogFragment.TAG);
mSimperium.authorizeUser(email, password, mAuthListener);
mSimperium.authorizeUser(email, password, getAuthListener());
} else {
showDialogError(getString(R.string.simperium_dialog_message_password_login, PASSWORD_LENGTH_LOGIN));
}
}

protected AuthResponseListener getAuthListener() {
return mAuthListener;
}

private void startSignup() {
final String email = getEditTextString(mInputEmail);
final String password = getEditTextString(mInputPassword);
Expand All @@ -543,4 +553,8 @@ private void startSignup() {
showDialogError(getString(R.string.simperium_dialog_message_password, PASSWORD_LENGTH_MINIMUM));
}
}

protected String getEmail() {
return getEditTextString(mInputEmail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class AuthException extends SimperiumException {

static public final String GENERIC_FAILURE_MESSAGE = "Invalid username or password";
static public final String EXISTING_USER_FAILURE_MESSAGE = "Account already exists";
static public final String UNVERIFIED_ACCOUNT_MESSAGE = "Account verification required";
static public final String COMPROMISED_PASSWORD_MESSAGE = "Password has been compromised";
static public final String COMPROMISED_PASSWORD_BODY = "compromised password";

Expand All @@ -16,7 +17,7 @@ public class AuthException extends SimperiumException {
public final FailureType failureType;

public enum FailureType {
INVALID_ACCOUNT, EXISTING_ACCOUNT, COMPROMISED_PASSWORD
INVALID_ACCOUNT, EXISTING_ACCOUNT, COMPROMISED_PASSWORD, UNVERIFIED_ACCOUNT
}

public AuthException(FailureType code, String message){
Expand All @@ -41,6 +42,8 @@ public static AuthException exceptionForStatusCode(int statusCode, Throwable cau
switch (statusCode) {
case 409:
return new AuthException(FailureType.EXISTING_ACCOUNT, EXISTING_USER_FAILURE_MESSAGE, cause);
case 403:
return new AuthException(FailureType.UNVERIFIED_ACCOUNT, UNVERIFIED_ACCOUNT_MESSAGE, cause);
case 401:
// Code 401 can be obtain because credentials are wrong or the user's password has been compromised
// To differentiate both responses, we check the response's body
Expand Down
3 changes: 3 additions & 0 deletions Simperium/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@
<string name="simperium_change_password">Change Password</string>
<string name="simperium_title">@string/app_name</string>
<string name="simperium_url">https://simperium.com/</string>
<string name="simperium_account_verification">Account Verification Required</string>
<string name="simperium_account_verification_message">You must verify your email before logging in to your account.</string>
<string name="simperium_okay">Okay</string>
</resources>

0 comments on commit c0bbbb8

Please sign in to comment.