From 19ed92c5a53a7fb0758d91da783fbcf43a8dab0a Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 26 Jan 2019 11:18:59 +0000 Subject: [PATCH] Prevent changing oauth2 token, cleanup --- .../eu/faircode/email/FragmentAccount.java | 47 ++++++++----- .../eu/faircode/email/FragmentIdentity.java | 67 ++++++++++--------- 2 files changed, 65 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index b04cf7b2e0..f37cff6a89 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -138,8 +138,8 @@ public class FragmentAccount extends FragmentBase { private Group grpFolders; private long id = -1; + private int auth_type = Helper.AUTH_TYPE_PASSWORD; private int color = Color.TRANSPARENT; - private String authorized = null; @Override public void onCreate(Bundle savedInstanceState) { @@ -233,13 +233,18 @@ public class FragmentAccount extends FragmentBase { return; adapterView.setTag(position); + auth_type = Helper.AUTH_TYPE_PASSWORD; + etHost.setText(provider.imap_host); etPort.setText(provider.imap_host == null ? null : Integer.toString(provider.imap_port)); cbStartTls.setChecked(provider.imap_starttls); + etUser.setTag(null); etUser.setText(null); tilPassword.getEditText().setText(null); etRealm.setText(null); + tilPassword.setEnabled(true); + etRealm.setEnabled(true); etName.setText(position > 1 ? provider.name : null); etPrefix.setText(provider.prefix); @@ -282,15 +287,20 @@ public class FragmentAccount extends FragmentBase { } }); - tilPassword.getEditText().addTextChangedListener(new TextWatcher() { + etUser.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { - if (authorized != null && !authorized.equals(s.toString())) - authorized = null; + 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 @@ -478,10 +488,9 @@ public class FragmentAccount extends FragmentBase { } private void onCheck() { - EmailProvider provider = (EmailProvider) spProvider.getSelectedItem(); - Bundle args = new Bundle(); args.putLong("id", id); + args.putInt("auth_type", auth_type); args.putString("host", etHost.getText().toString()); args.putBoolean("starttls", cbStartTls.isChecked()); args.putBoolean("insecure", cbInsecure.isChecked()); @@ -489,7 +498,6 @@ public class FragmentAccount extends FragmentBase { args.putString("user", etUser.getText().toString()); args.putString("password", tilPassword.getEditText().getText().toString()); args.putString("realm", etRealm.getText().toString()); - args.putInt("auth_type", authorized == null ? Helper.AUTH_TYPE_PASSWORD : provider.getAuthType()); new SimpleTask() { @Override @@ -515,6 +523,7 @@ public class FragmentAccount extends FragmentBase { @Override protected CheckResult onExecute(Context context, Bundle args) throws Throwable { long id = args.getLong("id"); + int auth_type = args.getInt("auth_type"); String host = args.getString("host"); boolean starttls = args.getBoolean("starttls"); boolean insecure = args.getBoolean("insecure"); @@ -522,7 +531,6 @@ public class FragmentAccount extends FragmentBase { String user = args.getString("user"); String password = args.getString("password"); String realm = args.getString("realm"); - int auth_type = args.getInt("auth_type"); if (TextUtils.isEmpty(host)) throw new IllegalArgumentException(context.getString(R.string.title_no_host)); @@ -693,8 +701,6 @@ public class FragmentAccount extends FragmentBase { } private void onSave() { - EmailProvider provider = (EmailProvider) spProvider.getSelectedItem(); - EntityFolder drafts = (EntityFolder) spDrafts.getSelectedItem(); EntityFolder sent = (EntityFolder) spSent.getSelectedItem(); EntityFolder all = (EntityFolder) spAll.getSelectedItem(); @@ -721,7 +727,7 @@ public class FragmentAccount extends FragmentBase { Bundle args = new Bundle(); 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.putBoolean("starttls", cbStartTls.isChecked()); args.putBoolean("insecure", cbInsecure.isChecked()); @@ -1049,7 +1055,7 @@ public class FragmentAccount extends FragmentBase { public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("provider", spProvider.getSelectedItemPosition()); - outState.putString("authorized", authorized); + outState.putInt("auth_type", auth_type); outState.putString("password", tilPassword.getEditText().getText().toString()); outState.putInt("advanced", grpAdvanced.getVisibility()); outState.putInt("color", color); @@ -1082,6 +1088,8 @@ public class FragmentAccount extends FragmentBase { spProvider.setAdapter(aaProvider); if (savedInstanceState == null) { + auth_type = (account == null ? Helper.AUTH_TYPE_PASSWORD : account.auth_type); + if (account != null) { boolean found = false; for (int pos = 2; pos < providers.size(); pos++) { @@ -1105,7 +1113,7 @@ public class FragmentAccount extends FragmentBase { cbStartTls.setChecked(account == null ? false : account.starttls); 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); tilPassword.getEditText().setText(account == null ? null : account.password); etRealm.setText(account == null ? null : account.realm); @@ -1143,7 +1151,7 @@ public class FragmentAccount extends FragmentBase { spProvider.setTag(provider); spProvider.setSelection(provider); - authorized = savedInstanceState.getString("authorized"); + auth_type = savedInstanceState.getInt("auth_type"); tilPassword.getEditText().setText(savedInstanceState.getString("password")); grpAdvanced.setVisibility(savedInstanceState.getInt("advanced")); color = savedInstanceState.getInt("color"); @@ -1151,6 +1159,9 @@ public class FragmentAccount extends FragmentBase { Helper.setViewsEnabled(view, true); + tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); + etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); + setColor(color); cbPrimary.setEnabled(cbSynchronize.isChecked()); @@ -1305,8 +1316,10 @@ public class FragmentAccount extends FragmentBase { String token = bundle.getString(AccountManager.KEY_AUTHTOKEN); Log.i("Got token"); - authorized = token; + auth_type = Helper.AUTH_TYPE_GMAIL; + etUser.setTag(account.name); etUser.setText(account.name); + etUser.setTag(account.name); tilPassword.getEditText().setText(token); etRealm.setText(null); } catch (Throwable ex) { @@ -1320,8 +1333,8 @@ public class FragmentAccount extends FragmentBase { } finally { btnAuthorize.setEnabled(true); etUser.setEnabled(true); - tilPassword.setEnabled(true); - etRealm.setEnabled(true); + tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); + etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); btnCheck.setEnabled(true); btnSave.setEnabled(true); new Handler().postDelayed(new Runnable() { diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index d0b58a7884..d92816bdfd 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -19,18 +19,12 @@ package eu.faircode.email; Copyright 2018-2019 by Marcel Bokhorst (M66B) */ -import android.Manifest; import android.content.Context; import android.content.DialogInterface; -import android.content.pm.PackageManager; -import android.database.Cursor; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; -import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.os.Handler; -import android.provider.ContactsContract; import android.text.Editable; import android.text.Html; import android.text.Spanned; @@ -71,7 +65,6 @@ import javax.mail.Transport; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.constraintlayout.widget.Group; -import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentTransaction; public class FragmentIdentity extends FragmentBase { @@ -121,6 +114,7 @@ public class FragmentIdentity extends FragmentBase { private Group grpAdvanced; private long id = -1; + private int auth_type = Helper.AUTH_TYPE_PASSWORD; private int color = Color.TRANSPARENT; @Override @@ -200,6 +194,7 @@ public class FragmentIdentity extends FragmentBase { adapterView.setTag(position); EntityAccount account = (EntityAccount) adapterView.getAdapter().getItem(position); + auth_type = account.auth_type; // Select associated provider if (position == 0) @@ -228,9 +223,12 @@ public class FragmentIdentity extends FragmentBase { // Copy account credentials etEmail.setText(account.user); + etUser.setTag(auth_type == Helper.AUTH_TYPE_PASSWORD ? null : account.user); etUser.setText(account.user); tilPassword.getEditText().setText(account.password); etRealm.setText(account.realm); + tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); + etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); } @Override @@ -238,29 +236,26 @@ public class FragmentIdentity extends FragmentBase { } }); - // READ_PROFILE was removed with SDK 23 - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && - ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_CONTACTS) - == 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(); + etUser.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { } - } + + @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); btnColor.setOnClickListener(new View.OnClickListener() { @@ -472,7 +467,7 @@ public class FragmentIdentity extends FragmentBase { args.putBoolean("read_receipt", cbReadReceipt.isChecked()); args.putBoolean("store_sent", cbStoreSent.isChecked()); 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.putBoolean("starttls", cbStartTls.isChecked()); args.putBoolean("insecure", cbInsecure.isChecked()); @@ -529,7 +524,6 @@ public class FragmentIdentity extends FragmentBase { boolean read_receipt = args.getBoolean("read_receipt"); boolean store_sent = args.getBoolean("store_sent"); - if (TextUtils.isEmpty(name)) throw new IllegalArgumentException(context.getString(R.string.title_no_name)); if (TextUtils.isEmpty(email)) @@ -574,7 +568,7 @@ public class FragmentIdentity extends FragmentBase { boolean check = (synchronize && (identity == null || !host.equals(identity.host) || Integer.parseInt(port) != identity.port || !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); Long last_connected = null; @@ -683,6 +677,7 @@ public class FragmentIdentity extends FragmentBase { super.onSaveInstanceState(outState); outState.putInt("account", spAccount.getSelectedItemPosition()); outState.putInt("provider", spProvider.getSelectedItemPosition()); + outState.putInt("auth_type", auth_type); outState.putString("password", tilPassword.getEditText().getText().toString()); outState.putInt("advanced", grpAdvanced.getVisibility()); outState.putInt("color", color); @@ -705,6 +700,8 @@ public class FragmentIdentity extends FragmentBase { @Override protected void onExecuted(Bundle args, final EntityIdentity identity) { if (savedInstanceState == null) { + auth_type = (identity == null ? Helper.AUTH_TYPE_PASSWORD : identity.auth_type); + etName.setText(identity == null ? null : identity.name); etEmail.setText(identity == null ? null : identity.email); @@ -716,6 +713,7 @@ public class FragmentIdentity extends FragmentBase { cbStartTls.setChecked(identity == null ? false : identity.starttls); cbInsecure.setChecked(identity == null ? false : identity.insecure); 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); tilPassword.getEditText().setText(identity == null ? null : identity.password); etRealm.setText(identity == null ? null : identity.realm); @@ -750,6 +748,7 @@ public class FragmentIdentity extends FragmentBase { } }.execute(FragmentIdentity.this, new Bundle(), "identity:count"); } else { + auth_type = savedInstanceState.getInt("auth_type"); tilPassword.getEditText().setText(savedInstanceState.getString("password")); grpAdvanced.setVisibility(savedInstanceState.getInt("advanced")); color = savedInstanceState.getInt("color"); @@ -757,6 +756,9 @@ public class FragmentIdentity extends FragmentBase { Helper.setViewsEnabled(view, true); + tilPassword.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); + etRealm.setEnabled(auth_type == Helper.AUTH_TYPE_PASSWORD); + setColor(color); cbPrimary.setEnabled(cbSynchronize.isChecked()); @@ -776,6 +778,7 @@ public class FragmentIdentity extends FragmentBase { EntityAccount unselected = new EntityAccount(); unselected.id = -1L; + unselected.auth_type = Helper.AUTH_TYPE_PASSWORD; unselected.name = getString(R.string.title_select); unselected.primary = false; accounts.add(0, unselected);