Prevent changing oauth2 token, cleanup

This commit is contained in:
M66B 2019-01-26 11:18:59 +00:00
parent fad122a705
commit 19ed92c5a5
2 changed files with 65 additions and 49 deletions

View File

@ -138,8 +138,8 @@ public class FragmentAccount extends FragmentBase {
private Group grpFolders; private Group grpFolders;
private long id = -1; private long id = -1;
private int auth_type = Helper.AUTH_TYPE_PASSWORD;
private int color = Color.TRANSPARENT; private int color = Color.TRANSPARENT;
private String authorized = null;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -233,13 +233,18 @@ public class FragmentAccount extends FragmentBase {
return; return;
adapterView.setTag(position); adapterView.setTag(position);
auth_type = Helper.AUTH_TYPE_PASSWORD;
etHost.setText(provider.imap_host); etHost.setText(provider.imap_host);
etPort.setText(provider.imap_host == null ? null : Integer.toString(provider.imap_port)); etPort.setText(provider.imap_host == null ? null : Integer.toString(provider.imap_port));
cbStartTls.setChecked(provider.imap_starttls); cbStartTls.setChecked(provider.imap_starttls);
etUser.setTag(null);
etUser.setText(null); etUser.setText(null);
tilPassword.getEditText().setText(null); tilPassword.getEditText().setText(null);
etRealm.setText(null); etRealm.setText(null);
tilPassword.setEnabled(true);
etRealm.setEnabled(true);
etName.setText(position > 1 ? provider.name : null); etName.setText(position > 1 ? provider.name : null);
etPrefix.setText(provider.prefix); etPrefix.setText(provider.prefix);
@ -282,15 +287,20 @@ public class FragmentAccount extends FragmentBase {
} }
}); });
tilPassword.getEditText().addTextChangedListener(new TextWatcher() { etUser.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} }
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, int count) { public void onTextChanged(CharSequence s, int start, int before, int count) {
if (authorized != null && !authorized.equals(s.toString())) String user = etUser.getText().toString();
authorized = null; if (auth_type != Helper.AUTH_TYPE_PASSWORD && !user.equals(etUser.getTag())) {
auth_type = Helper.AUTH_TYPE_PASSWORD;
tilPassword.getEditText().setText(null);
tilPassword.setEnabled(true);
etRealm.setEnabled(true);
}
} }
@Override @Override
@ -478,10 +488,9 @@ public class FragmentAccount extends FragmentBase {
} }
private void onCheck() { private void onCheck() {
EmailProvider provider = (EmailProvider) spProvider.getSelectedItem();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
args.putInt("auth_type", auth_type);
args.putString("host", etHost.getText().toString()); args.putString("host", etHost.getText().toString());
args.putBoolean("starttls", cbStartTls.isChecked()); args.putBoolean("starttls", cbStartTls.isChecked());
args.putBoolean("insecure", cbInsecure.isChecked()); args.putBoolean("insecure", cbInsecure.isChecked());
@ -489,7 +498,6 @@ public class FragmentAccount extends FragmentBase {
args.putString("user", etUser.getText().toString()); args.putString("user", etUser.getText().toString());
args.putString("password", tilPassword.getEditText().getText().toString()); args.putString("password", tilPassword.getEditText().getText().toString());
args.putString("realm", etRealm.getText().toString()); args.putString("realm", etRealm.getText().toString());
args.putInt("auth_type", authorized == null ? Helper.AUTH_TYPE_PASSWORD : provider.getAuthType());
new SimpleTask<CheckResult>() { new SimpleTask<CheckResult>() {
@Override @Override
@ -515,6 +523,7 @@ public class FragmentAccount extends FragmentBase {
@Override @Override
protected CheckResult onExecute(Context context, Bundle args) throws Throwable { protected CheckResult onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id"); long id = args.getLong("id");
int auth_type = args.getInt("auth_type");
String host = args.getString("host"); String host = args.getString("host");
boolean starttls = args.getBoolean("starttls"); boolean starttls = args.getBoolean("starttls");
boolean insecure = args.getBoolean("insecure"); boolean insecure = args.getBoolean("insecure");
@ -522,7 +531,6 @@ public class FragmentAccount extends FragmentBase {
String user = args.getString("user"); String user = args.getString("user");
String password = args.getString("password"); String password = args.getString("password");
String realm = args.getString("realm"); String realm = args.getString("realm");
int auth_type = args.getInt("auth_type");
if (TextUtils.isEmpty(host)) if (TextUtils.isEmpty(host))
throw new IllegalArgumentException(context.getString(R.string.title_no_host)); throw new IllegalArgumentException(context.getString(R.string.title_no_host));
@ -693,8 +701,6 @@ public class FragmentAccount extends FragmentBase {
} }
private void onSave() { private void onSave() {
EmailProvider provider = (EmailProvider) spProvider.getSelectedItem();
EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem(); EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem();
EntityFolder sent = (EntityFolder) spSent.getSelectedItem(); EntityFolder sent = (EntityFolder) spSent.getSelectedItem();
EntityFolder all = (EntityFolder) spAll.getSelectedItem(); EntityFolder all = (EntityFolder) spAll.getSelectedItem();
@ -721,7 +727,7 @@ public class FragmentAccount extends FragmentBase {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", id); args.putLong("id", id);
args.putInt("auth_type", authorized == null ? Helper.AUTH_TYPE_PASSWORD : provider.getAuthType()); args.putInt("auth_type", auth_type);
args.putString("host", etHost.getText().toString()); args.putString("host", etHost.getText().toString());
args.putBoolean("starttls", cbStartTls.isChecked()); args.putBoolean("starttls", cbStartTls.isChecked());
args.putBoolean("insecure", cbInsecure.isChecked()); args.putBoolean("insecure", cbInsecure.isChecked());
@ -1049,7 +1055,7 @@ public class FragmentAccount extends FragmentBase {
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putInt("provider", spProvider.getSelectedItemPosition()); outState.putInt("provider", spProvider.getSelectedItemPosition());
outState.putString("authorized", authorized); outState.putInt("auth_type", auth_type);
outState.putString("password", tilPassword.getEditText().getText().toString()); outState.putString("password", tilPassword.getEditText().getText().toString());
outState.putInt("advanced", grpAdvanced.getVisibility()); outState.putInt("advanced", grpAdvanced.getVisibility());
outState.putInt("color", color); outState.putInt("color", color);
@ -1082,6 +1088,8 @@ public class FragmentAccount extends FragmentBase {
spProvider.setAdapter(aaProvider); spProvider.setAdapter(aaProvider);
if (savedInstanceState == null) { if (savedInstanceState == null) {
auth_type = (account == null ? Helper.AUTH_TYPE_PASSWORD : account.auth_type);
if (account != null) { if (account != null) {
boolean found = false; boolean found = false;
for (int pos = 2; pos < providers.size(); pos++) { for (int pos = 2; pos < providers.size(); pos++) {
@ -1105,7 +1113,7 @@ public class FragmentAccount extends FragmentBase {
cbStartTls.setChecked(account == null ? false : account.starttls); cbStartTls.setChecked(account == null ? false : account.starttls);
cbInsecure.setChecked(account == null ? false : account.insecure); cbInsecure.setChecked(account == null ? false : account.insecure);
authorized = (account != null && account.auth_type != Helper.AUTH_TYPE_PASSWORD ? account.password : null); etUser.setTag(account == null || auth_type == Helper.AUTH_TYPE_PASSWORD ? null : account.user);
etUser.setText(account == null ? null : account.user); etUser.setText(account == null ? null : account.user);
tilPassword.getEditText().setText(account == null ? null : account.password); tilPassword.getEditText().setText(account == null ? null : account.password);
etRealm.setText(account == null ? null : account.realm); etRealm.setText(account == null ? null : account.realm);
@ -1143,7 +1151,7 @@ public class FragmentAccount extends FragmentBase {
spProvider.setTag(provider); spProvider.setTag(provider);
spProvider.setSelection(provider); spProvider.setSelection(provider);
authorized = savedInstanceState.getString("authorized"); auth_type = savedInstanceState.getInt("auth_type");
tilPassword.getEditText().setText(savedInstanceState.getString("password")); tilPassword.getEditText().setText(savedInstanceState.getString("password"));
grpAdvanced.setVisibility(savedInstanceState.getInt("advanced")); grpAdvanced.setVisibility(savedInstanceState.getInt("advanced"));
color = savedInstanceState.getInt("color"); color = savedInstanceState.getInt("color");
@ -1151,6 +1159,9 @@ public class FragmentAccount extends FragmentBase {
Helper.setViewsEnabled(view, true); Helper.setViewsEnabled(view, true);
tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
setColor(color); setColor(color);
cbPrimary.setEnabled(cbSynchronize.isChecked()); cbPrimary.setEnabled(cbSynchronize.isChecked());
@ -1305,8 +1316,10 @@ public class FragmentAccount extends FragmentBase {
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN); String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
Log.i("Got token"); Log.i("Got token");
authorized = token; auth_type = Helper.AUTH_TYPE_GMAIL;
etUser.setTag(account.name);
etUser.setText(account.name); etUser.setText(account.name);
etUser.setTag(account.name);
tilPassword.getEditText().setText(token); tilPassword.getEditText().setText(token);
etRealm.setText(null); etRealm.setText(null);
} catch (Throwable ex) { } catch (Throwable ex) {
@ -1320,8 +1333,8 @@ public class FragmentAccount extends FragmentBase {
} finally { } finally {
btnAuthorize.setEnabled(true); btnAuthorize.setEnabled(true);
etUser.setEnabled(true); etUser.setEnabled(true);
tilPassword.setEnabled(true); tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
etRealm.setEnabled(true); etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
btnCheck.setEnabled(true); btnCheck.setEnabled(true);
btnSave.setEnabled(true); btnSave.setEnabled(true);
new Handler().postDelayed(new Runnable() { new Handler().postDelayed(new Runnable() {

View File

@ -19,18 +19,12 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B) Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/ */
import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.provider.ContactsContract;
import android.text.Editable; import android.text.Editable;
import android.text.Html; import android.text.Html;
import android.text.Spanned; import android.text.Spanned;
@ -71,7 +65,6 @@ import javax.mail.Transport;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group; import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
public class FragmentIdentity extends FragmentBase { public class FragmentIdentity extends FragmentBase {
@ -121,6 +114,7 @@ public class FragmentIdentity extends FragmentBase {
private Group grpAdvanced; private Group grpAdvanced;
private long id = -1; private long id = -1;
private int auth_type = Helper.AUTH_TYPE_PASSWORD;
private int color = Color.TRANSPARENT; private int color = Color.TRANSPARENT;
@Override @Override
@ -200,6 +194,7 @@ public class FragmentIdentity extends FragmentBase {
adapterView.setTag(position); adapterView.setTag(position);
EntityAccount account = (EntityAccount) adapterView.getAdapter().getItem(position); EntityAccount account = (EntityAccount) adapterView.getAdapter().getItem(position);
auth_type = account.auth_type;
// Select associated provider // Select associated provider
if (position == 0) if (position == 0)
@ -228,9 +223,12 @@ public class FragmentIdentity extends FragmentBase {
// Copy account credentials // Copy account credentials
etEmail.setText(account.user); etEmail.setText(account.user);
etUser.setTag(auth_type == Helper.AUTH_TYPE_PASSWORD ? null : account.user);
etUser.setText(account.user); etUser.setText(account.user);
tilPassword.getEditText().setText(account.password); tilPassword.getEditText().setText(account.password);
etRealm.setText(account.realm); etRealm.setText(account.realm);
tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
} }
@Override @Override
@ -238,29 +236,26 @@ public class FragmentIdentity extends FragmentBase {
} }
}); });
// READ_PROFILE was removed with SDK 23 etUser.addTextChangedListener(new TextWatcher() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && @Override
ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS) public void beforeTextChanged(CharSequence s, int start, int count, int after) {
== PackageManager.PERMISSION_GRANTED) {
Cursor cursor = null;
try {
cursor = getContext().getContentResolver().query(
Uri.withAppendedPath(
ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
new String[]{
ContactsContract.Profile.DISPLAY_NAME
},
null, null, null);
if (cursor != null && cursor.moveToNext())
etName.setHint(cursor.getString(0));
} catch (SecurityException ex) {
Log.w(ex);
} finally {
if (cursor != null)
cursor.close();
} }
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String user = etUser.getText().toString();
if (auth_type != Helper.AUTH_TYPE_PASSWORD && !user.equals(etUser.getTag())) {
auth_type = Helper.AUTH_TYPE_PASSWORD;
tilPassword.getEditText().setText(null);
tilPassword.setEnabled(true);
etRealm.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
vwColor.setBackgroundColor(color); vwColor.setBackgroundColor(color);
btnColor.setOnClickListener(new View.OnClickListener() { btnColor.setOnClickListener(new View.OnClickListener() {
@ -472,7 +467,7 @@ public class FragmentIdentity extends FragmentBase {
args.putBoolean("read_receipt", cbReadReceipt.isChecked()); args.putBoolean("read_receipt", cbReadReceipt.isChecked());
args.putBoolean("store_sent", cbStoreSent.isChecked()); args.putBoolean("store_sent", cbStoreSent.isChecked());
args.putLong("account", account == null ? -1 : account.id); args.putLong("account", account == null ? -1 : account.id);
args.putInt("auth_type", account == null || account.auth_type == null ? Helper.AUTH_TYPE_PASSWORD : account.auth_type); args.putInt("auth_type", auth_type);
args.putString("host", etHost.getText().toString()); args.putString("host", etHost.getText().toString());
args.putBoolean("starttls", cbStartTls.isChecked()); args.putBoolean("starttls", cbStartTls.isChecked());
args.putBoolean("insecure", cbInsecure.isChecked()); args.putBoolean("insecure", cbInsecure.isChecked());
@ -529,7 +524,6 @@ public class FragmentIdentity extends FragmentBase {
boolean read_receipt = args.getBoolean("read_receipt"); boolean read_receipt = args.getBoolean("read_receipt");
boolean store_sent = args.getBoolean("store_sent"); boolean store_sent = args.getBoolean("store_sent");
if (TextUtils.isEmpty(name)) if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(context.getString(R.string.title_no_name)); throw new IllegalArgumentException(context.getString(R.string.title_no_name));
if (TextUtils.isEmpty(email)) if (TextUtils.isEmpty(email))
@ -574,7 +568,7 @@ public class FragmentIdentity extends FragmentBase {
boolean check = (synchronize && (identity == null || boolean check = (synchronize && (identity == null ||
!host.equals(identity.host) || Integer.parseInt(port) != identity.port || !host.equals(identity.host) || Integer.parseInt(port) != identity.port ||
!user.equals(identity.user) || !password.equals(identity.password) || !user.equals(identity.user) || !password.equals(identity.password) ||
realm == null ? identityRealm != null : !realm.equals(identityRealm))); (realm == null ? identityRealm != null : !realm.equals(identityRealm))));
boolean reload = (identity == null || identity.synchronize != synchronize || check); boolean reload = (identity == null || identity.synchronize != synchronize || check);
Long last_connected = null; Long last_connected = null;
@ -683,6 +677,7 @@ public class FragmentIdentity extends FragmentBase {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putInt("account", spAccount.getSelectedItemPosition()); outState.putInt("account", spAccount.getSelectedItemPosition());
outState.putInt("provider", spProvider.getSelectedItemPosition()); outState.putInt("provider", spProvider.getSelectedItemPosition());
outState.putInt("auth_type", auth_type);
outState.putString("password", tilPassword.getEditText().getText().toString()); outState.putString("password", tilPassword.getEditText().getText().toString());
outState.putInt("advanced", grpAdvanced.getVisibility()); outState.putInt("advanced", grpAdvanced.getVisibility());
outState.putInt("color", color); outState.putInt("color", color);
@ -705,6 +700,8 @@ public class FragmentIdentity extends FragmentBase {
@Override @Override
protected void onExecuted(Bundle args, final EntityIdentity identity) { protected void onExecuted(Bundle args, final EntityIdentity identity) {
if (savedInstanceState == null) { if (savedInstanceState == null) {
auth_type = (identity == null ? Helper.AUTH_TYPE_PASSWORD : identity.auth_type);
etName.setText(identity == null ? null : identity.name); etName.setText(identity == null ? null : identity.name);
etEmail.setText(identity == null ? null : identity.email); etEmail.setText(identity == null ? null : identity.email);
@ -716,6 +713,7 @@ public class FragmentIdentity extends FragmentBase {
cbStartTls.setChecked(identity == null ? false : identity.starttls); cbStartTls.setChecked(identity == null ? false : identity.starttls);
cbInsecure.setChecked(identity == null ? false : identity.insecure); cbInsecure.setChecked(identity == null ? false : identity.insecure);
etPort.setText(identity == null ? null : Long.toString(identity.port)); etPort.setText(identity == null ? null : Long.toString(identity.port));
etUser.setTag(identity == null || auth_type == Helper.AUTH_TYPE_PASSWORD ? null : identity.user);
etUser.setText(identity == null ? null : identity.user); etUser.setText(identity == null ? null : identity.user);
tilPassword.getEditText().setText(identity == null ? null : identity.password); tilPassword.getEditText().setText(identity == null ? null : identity.password);
etRealm.setText(identity == null ? null : identity.realm); etRealm.setText(identity == null ? null : identity.realm);
@ -750,6 +748,7 @@ public class FragmentIdentity extends FragmentBase {
} }
}.execute(FragmentIdentity.this, new Bundle(), "identity:count"); }.execute(FragmentIdentity.this, new Bundle(), "identity:count");
} else { } else {
auth_type = savedInstanceState.getInt("auth_type");
tilPassword.getEditText().setText(savedInstanceState.getString("password")); tilPassword.getEditText().setText(savedInstanceState.getString("password"));
grpAdvanced.setVisibility(savedInstanceState.getInt("advanced")); grpAdvanced.setVisibility(savedInstanceState.getInt("advanced"));
color = savedInstanceState.getInt("color"); color = savedInstanceState.getInt("color");
@ -757,6 +756,9 @@ public class FragmentIdentity extends FragmentBase {
Helper.setViewsEnabled(view, true); Helper.setViewsEnabled(view, true);
tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD);
setColor(color); setColor(color);
cbPrimary.setEnabled(cbSynchronize.isChecked()); cbPrimary.setEnabled(cbSynchronize.isChecked());
@ -776,6 +778,7 @@ public class FragmentIdentity extends FragmentBase {
EntityAccount unselected = new EntityAccount(); EntityAccount unselected = new EntityAccount();
unselected.id = -1L; unselected.id = -1L;
unselected.auth_type = Helper.AUTH_TYPE_PASSWORD;
unselected.name = getString(R.string.title_select); unselected.name = getString(R.string.title_select);
unselected.primary = false; unselected.primary = false;
accounts.add(0, unselected); accounts.add(0, unselected);