FairEmail/app/src/main/java/eu/faircode/email/FragmentOAuth.java

991 lines
42 KiB
Java
Raw Normal View History

2019-12-21 11:06:21 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2022-01-01 08:46:36 +00:00
Copyright 2018-2022 by Marcel Bokhorst (M66B)
2019-12-21 11:06:21 +00:00
*/
2021-07-31 07:09:35 +00:00
import static android.app.Activity.RESULT_OK;
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_OAUTH;
2021-12-24 07:18:25 +00:00
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_PASSWORD;
2021-07-31 07:09:35 +00:00
2019-12-21 20:52:23 +00:00
import android.content.ActivityNotFoundException;
2019-12-21 11:06:21 +00:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2020-06-14 16:34:13 +00:00
import android.content.pm.PackageManager;
2021-05-30 15:52:15 +00:00
import android.content.pm.ResolveInfo;
2021-06-26 07:05:25 +00:00
import android.graphics.Paint;
2020-07-29 12:06:40 +00:00
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
2019-12-21 11:06:21 +00:00
import android.net.Uri;
2021-05-30 15:52:15 +00:00
import android.os.Build;
2019-12-21 11:06:21 +00:00
import android.os.Bundle;
import android.text.TextUtils;
2020-06-07 18:54:09 +00:00
import android.util.Base64;
2019-12-21 11:55:46 +00:00
import android.util.Pair;
2019-12-21 11:06:21 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
2020-11-26 08:45:04 +00:00
import android.widget.CheckBox;
2020-01-09 11:15:41 +00:00
import android.widget.EditText;
2019-12-21 11:06:21 +00:00
import android.widget.ScrollView;
import android.widget.TextView;
2020-11-26 08:45:04 +00:00
import android.widget.Toast;
2019-12-21 11:06:21 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
2021-09-28 19:15:46 +00:00
import androidx.lifecycle.Lifecycle;
2019-12-21 11:06:21 +00:00
import androidx.preference.PreferenceManager;
import net.openid.appauth.AppAuthConfiguration;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
2021-05-31 15:52:38 +00:00
import net.openid.appauth.AuthorizationManagementActivity;
2019-12-21 11:06:21 +00:00
import net.openid.appauth.AuthorizationRequest;
import net.openid.appauth.AuthorizationResponse;
import net.openid.appauth.AuthorizationService;
import net.openid.appauth.AuthorizationServiceConfiguration;
import net.openid.appauth.ClientAuthentication;
import net.openid.appauth.ClientSecretPost;
import net.openid.appauth.CodeVerifierUtil;
2021-09-25 12:46:24 +00:00
import net.openid.appauth.GrantTypeValues;
2019-12-21 11:06:21 +00:00
import net.openid.appauth.NoClientAuthentication;
import net.openid.appauth.ResponseTypeValues;
2021-09-25 12:46:24 +00:00
import net.openid.appauth.TokenRequest;
2019-12-21 11:06:21 +00:00
import net.openid.appauth.TokenResponse;
import net.openid.appauth.browser.BrowserDescriptor;
import net.openid.appauth.browser.BrowserMatcher;
import net.openid.appauth.browser.Browsers;
import net.openid.appauth.browser.VersionRange;
import net.openid.appauth.browser.VersionedBrowserMatcher;
2021-06-18 07:35:31 +00:00
import org.json.JSONArray;
2019-12-21 11:06:21 +00:00
import org.json.JSONObject;
2021-05-19 15:20:42 +00:00
import java.io.FileNotFoundException;
2021-04-24 16:10:03 +00:00
import java.net.URL;
2019-12-21 11:55:46 +00:00
import java.util.ArrayList;
2021-09-25 12:46:24 +00:00
import java.util.Collections;
2019-12-21 11:06:21 +00:00
import java.util.Date;
2021-05-31 13:44:27 +00:00
import java.util.Iterator;
2022-03-23 19:04:05 +00:00
import java.util.LinkedHashMap;
2019-12-21 11:06:21 +00:00
import java.util.List;
import java.util.Map;
2020-06-13 11:54:37 +00:00
import javax.mail.AuthenticationFailedException;
2021-04-24 16:10:03 +00:00
import javax.net.ssl.HttpsURLConnection;
2020-06-13 11:54:37 +00:00
2019-12-21 11:06:21 +00:00
public class FragmentOAuth extends FragmentBase {
2019-12-21 15:03:57 +00:00
private String id;
2019-12-21 11:06:21 +00:00
private String name;
2021-06-26 07:05:25 +00:00
private String privacy;
2020-01-09 11:15:41 +00:00
private boolean askAccount;
2022-03-23 23:19:28 +00:00
private boolean askTenant;
2019-12-21 11:06:21 +00:00
2021-07-31 07:09:35 +00:00
private String personal;
private String address;
private boolean update;
2019-12-21 11:06:21 +00:00
private ViewGroup view;
private ScrollView scroll;
2021-06-26 07:05:25 +00:00
private TextView tvTitle;
private TextView tvPrivacy;
2020-01-09 11:15:41 +00:00
private EditText etName;
private EditText etEmail;
private EditText etTenant;
2020-11-26 08:45:04 +00:00
private CheckBox cbUpdate;
2019-12-21 11:06:21 +00:00
private Button btnOAuth;
private ContentLoadingProgressBar pbOAuth;
2020-08-06 20:22:47 +00:00
private TextView tvConfiguring;
2019-12-21 11:06:21 +00:00
private TextView tvGmailHint;
private TextView tvError;
private TextView tvGmailDraftsHint;
2020-06-13 11:54:37 +00:00
private TextView tvOfficeAuthHint;
2019-12-25 10:49:34 +00:00
private Button btnSupport;
2021-10-21 13:16:28 +00:00
private Button btnHelp;
2019-12-25 10:49:34 +00:00
private Group grpTenant;
2019-12-25 10:49:34 +00:00
private Group grpError;
2019-12-21 11:06:21 +00:00
2021-04-24 16:10:03 +00:00
private static final int MAILRU_TIMEOUT = 20 * 1000; // milliseconds
2019-12-21 11:06:21 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
2019-12-21 15:03:57 +00:00
id = args.getString("id");
2019-12-21 11:06:21 +00:00
name = args.getString("name");
2021-06-26 07:05:25 +00:00
privacy = args.getString("privacy");
2020-01-09 11:15:41 +00:00
askAccount = args.getBoolean("askAccount", false);
2022-03-23 23:19:28 +00:00
askTenant = args.getBoolean("askTenant", false);
2021-07-31 07:09:35 +00:00
personal = args.getString("personal");
address = args.getString("address");
update = args.getBoolean("update");
2019-12-21 11:06:21 +00:00
}
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
setSubtitle(name);
setHasOptionsMenu(true);
view = (ViewGroup) inflater.inflate(R.layout.fragment_oauth, container, false);
scroll = view.findViewById(R.id.scroll);
// Get controls
2021-06-26 07:05:25 +00:00
tvTitle = view.findViewById(R.id.tvTitle);
tvPrivacy = view.findViewById(R.id.tvPrivacy);
2020-01-09 11:15:41 +00:00
etName = view.findViewById(R.id.etName);
etEmail = view.findViewById(R.id.etEmail);
etTenant = view.findViewById(R.id.etTenant);
2020-11-26 08:45:04 +00:00
cbUpdate = view.findViewById(R.id.cbUpdate);
2019-12-21 11:06:21 +00:00
btnOAuth = view.findViewById(R.id.btnOAuth);
pbOAuth = view.findViewById(R.id.pbOAuth);
2020-08-06 20:22:47 +00:00
tvConfiguring = view.findViewById(R.id.tvConfiguring);
2019-12-21 11:06:21 +00:00
tvGmailHint = view.findViewById(R.id.tvGmailHint);
tvError = view.findViewById(R.id.tvError);
tvGmailDraftsHint = view.findViewById(R.id.tvGmailDraftsHint);
2020-06-13 11:54:37 +00:00
tvOfficeAuthHint = view.findViewById(R.id.tvOfficeAuthHint);
2019-12-25 10:49:34 +00:00
btnSupport = view.findViewById(R.id.btnSupport);
2021-10-21 13:16:28 +00:00
btnHelp = view.findViewById(R.id.btnHelp);
2019-12-25 10:49:34 +00:00
grpTenant = view.findViewById(R.id.grpTenant);
2019-12-25 10:49:34 +00:00
grpError = view.findViewById(R.id.grpError);
2019-12-21 11:06:21 +00:00
// Wire controls
2021-06-26 07:05:25 +00:00
tvPrivacy.setVisibility(TextUtils.isEmpty(privacy) ? View.GONE : View.VISIBLE);
tvPrivacy.setPaintFlags(tvPrivacy.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
tvPrivacy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Helper.view(v.getContext(), Uri.parse(privacy), false);
}
});
2019-12-21 11:06:21 +00:00
btnOAuth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onAuthorize();
}
});
2019-12-25 10:49:34 +00:00
btnSupport.setOnClickListener(new View.OnClickListener() {
@Override
2021-04-06 06:59:35 +00:00
public void onClick(View v) {
Helper.view(v.getContext(), Helper.getSupportUri(v.getContext()), false);
2019-12-25 10:49:34 +00:00
}
});
2021-10-21 13:16:28 +00:00
btnHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
EmailProvider provider = EmailProvider.getProvider(v.getContext(), id);
Helper.view(v.getContext(), Uri.parse(provider.link), false);
} catch (Throwable ex) {
Log.e(ex);
}
}
});
2019-12-21 11:06:21 +00:00
// Initialize
2021-06-26 07:05:25 +00:00
tvTitle.setText(getString(R.string.title_setup_oauth_rationale, name));
2020-01-09 11:15:41 +00:00
etName.setVisibility(askAccount ? View.VISIBLE : View.GONE);
etEmail.setVisibility(askAccount ? View.VISIBLE : View.GONE);
2022-03-23 23:19:28 +00:00
grpTenant.setVisibility(askTenant ? View.VISIBLE : View.GONE);
2019-12-21 11:06:21 +00:00
pbOAuth.setVisibility(View.GONE);
2020-08-06 20:22:47 +00:00
tvConfiguring.setVisibility(View.GONE);
2019-12-21 15:03:57 +00:00
tvGmailHint.setVisibility("gmail".equals(id) ? View.VISIBLE : View.GONE);
2019-12-21 11:06:21 +00:00
hideError();
2021-07-31 07:09:35 +00:00
etName.setText(personal);
etEmail.setText(address);
etTenant.setText(null);
2021-07-31 07:09:35 +00:00
cbUpdate.setChecked(update);
2019-12-21 11:06:21 +00:00
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
2020-04-06 18:59:32 +00:00
try {
switch (requestCode) {
case ActivitySetup.REQUEST_OAUTH:
if (resultCode == RESULT_OK && data != null)
onHandleOAuth(data);
else
onHandleCancel();
break;
case ActivitySetup.REQUEST_DONE:
finish();
break;
}
} catch (Throwable ex) {
Log.e(ex);
2019-12-21 11:06:21 +00:00
}
}
private void onAuthorize() {
try {
2020-01-09 11:15:41 +00:00
if (askAccount) {
String name = etName.getText().toString().trim();
String email = etEmail.getText().toString().trim();
if (TextUtils.isEmpty(name))
throw new IllegalArgumentException(getString(R.string.title_no_name));
if (TextUtils.isEmpty(email))
throw new IllegalArgumentException(getString(R.string.title_no_email));
2021-08-17 05:52:13 +00:00
int backslash = email.indexOf('\\');
if (backslash > 0)
email = email.substring(0, backslash);
2020-03-05 13:45:29 +00:00
if (!Helper.EMAIL_ADDRESS.matcher(email).matches())
2020-01-09 11:15:41 +00:00
throw new IllegalArgumentException(getString(R.string.title_email_invalid, email));
}
2021-08-09 17:31:35 +00:00
etName.clearFocus();
etEmail.clearFocus();
etTenant.clearFocus();
2021-08-09 17:31:35 +00:00
Helper.hideKeyboard(view);
2020-06-30 20:57:34 +00:00
etName.setEnabled(false);
etEmail.setEnabled(false);
etTenant.setEnabled(false);
2020-11-26 08:45:04 +00:00
cbUpdate.setEnabled(false);
2019-12-21 11:06:21 +00:00
btnOAuth.setEnabled(false);
pbOAuth.setVisibility(View.VISIBLE);
hideError();
2021-05-30 14:58:17 +00:00
final Context context = getContext();
2021-05-30 15:52:15 +00:00
PackageManager pm = context.getPackageManager();
2021-05-30 14:58:17 +00:00
EmailProvider provider = EmailProvider.getProvider(context, id);
2019-12-21 15:03:57 +00:00
2021-05-30 15:52:15 +00:00
int flags = PackageManager.GET_RESOLVED_FILTER;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
flags |= PackageManager.MATCH_ALL;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
List<ResolveInfo> ris = pm.queryIntentActivities(intent, flags);
EntityLog.log(context, "Browsers=" + (ris == null ? null : ris.size()));
if (ris != null)
2021-05-31 13:44:27 +00:00
for (ResolveInfo ri : ris) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("android.support.customtabs.action.CustomTabsService");
serviceIntent.setPackage(ri.activityInfo.packageName);
boolean tabs = (pm.resolveService(serviceIntent, 0) != null);
StringBuilder sb = new StringBuilder();
sb.append("Browser=").append(ri.activityInfo.packageName);
sb.append(" tabs=").append(tabs);
sb.append(" view=").append(ri.filter.hasAction(Intent.ACTION_VIEW));
sb.append(" browsable=").append(ri.filter.hasCategory(Intent.CATEGORY_BROWSABLE));
sb.append(" authorities=").append(ri.filter.authoritiesIterator() != null);
sb.append(" schemes=");
boolean first = true;
Iterator<String> schemeIter = ri.filter.schemesIterator();
while (schemeIter.hasNext()) {
String scheme = schemeIter.next();
if (first)
first = false;
else
sb.append(',');
sb.append(scheme);
}
EntityLog.log(context, sb.toString());
}
2021-05-30 15:52:15 +00:00
2019-12-21 15:03:57 +00:00
AppAuthConfiguration appAuthConfig = new AppAuthConfiguration.Builder()
.setBrowserMatcher(new BrowserMatcher() {
2021-06-29 19:38:15 +00:00
final BrowserMatcher SBROWSER = new VersionedBrowserMatcher(
Browsers.SBrowser.PACKAGE_NAME,
Browsers.SBrowser.SIGNATURE_SET,
true,
VersionRange.atMost("5.3"));
2019-12-21 15:03:57 +00:00
@Override
public boolean matches(@NonNull BrowserDescriptor descriptor) {
2021-06-29 19:38:15 +00:00
boolean accept =
(!SBROWSER.matches(descriptor) && !descriptor.useCustomTab);
2021-05-30 14:58:17 +00:00
EntityLog.log(context,
2021-05-30 12:22:59 +00:00
"Browser=" + descriptor.packageName +
":" + descriptor.version +
2021-06-29 19:38:15 +00:00
" tabs=" + descriptor.useCustomTab + "" +
2021-05-30 12:22:59 +00:00
" accept=" + accept +
" provider=" + provider.id);
return accept;
2019-12-21 15:03:57 +00:00
}
})
.build();
2021-05-30 14:58:17 +00:00
AuthorizationService authService = new AuthorizationService(context, appAuthConfig);
2019-12-21 15:03:57 +00:00
String authorizationEndpoint = provider.oauth.authorizationEndpoint;
String tokenEndpoint = provider.oauth.tokenEndpoint;
String tenant = etTenant.getText().toString().trim();
if (TextUtils.isEmpty(tenant))
tenant = "common";
authorizationEndpoint = authorizationEndpoint.replace("{tenant}", tenant);
tokenEndpoint = tokenEndpoint.replace("{tenant}", tenant);
2019-12-21 15:03:57 +00:00
AuthorizationServiceConfiguration serviceConfig = new AuthorizationServiceConfiguration(
Uri.parse(authorizationEndpoint),
Uri.parse(tokenEndpoint));
2019-12-21 15:03:57 +00:00
AuthState authState = new AuthState(serviceConfig);
2021-05-30 14:58:17 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2019-12-21 15:03:57 +00:00
prefs.edit().putString("oauth." + provider.id, authState.jsonSerializeString()).apply();
2022-03-24 19:38:55 +00:00
Map<String, String> params = (provider.oauth.parameters == null
? new LinkedHashMap<>()
: provider.oauth.parameters);
2020-01-09 16:53:00 +00:00
2019-12-21 15:03:57 +00:00
AuthorizationRequest.Builder authRequestBuilder =
new AuthorizationRequest.Builder(
serviceConfig,
provider.oauth.clientId,
ResponseTypeValues.CODE,
Uri.parse(provider.oauth.redirectUri))
.setScopes(provider.oauth.scopes)
.setState(provider.id)
.setAdditionalParameters(params);
if (askAccount) {
String address = etEmail.getText().toString().trim();
int backslash = address.indexOf('\\');
if (backslash > 0)
authRequestBuilder.setLoginHint(address.substring(0, backslash));
else
authRequestBuilder.setLoginHint(address);
}
2020-01-09 11:15:41 +00:00
if (provider.oauth.pcke)
authRequestBuilder.setCodeVerifier(CodeVerifierUtil.generateRandomCodeVerifier());
2022-03-23 19:04:05 +00:00
if (!TextUtils.isEmpty(provider.oauth.prompt))
authRequestBuilder.setPrompt(provider.oauth.prompt);
2019-12-21 15:03:57 +00:00
AuthorizationRequest authRequest = authRequestBuilder.build();
2021-05-30 14:58:17 +00:00
EntityLog.log(context, "OAuth request provider=" + provider.id + " uri=" + authRequest.toUri());
Intent authIntent;
2020-09-20 15:00:09 +00:00
try {
authIntent = authService.getAuthorizationRequestIntent(authRequest);
} catch (ActivityNotFoundException ex) {
2021-05-31 15:52:38 +00:00
Log.w(ex);
authIntent =
AuthorizationManagementActivity.createStartForResultIntent(
context, authRequest,
new Intent(Intent.ACTION_VIEW, authRequest.toUri()));
2020-09-20 15:00:09 +00:00
}
2021-05-30 15:52:15 +00:00
2021-05-31 15:52:38 +00:00
startActivityForResult(authIntent, ActivitySetup.REQUEST_OAUTH);
2019-12-21 11:06:21 +00:00
} catch (Throwable ex) {
showError(ex);
}
}
private void onHandleOAuth(@NonNull Intent data) {
try {
2020-06-30 20:57:34 +00:00
etName.setEnabled(true);
etEmail.setEnabled(true);
etTenant.setEnabled(true);
2020-11-26 08:45:04 +00:00
cbUpdate.setEnabled(true);
2020-06-30 20:57:34 +00:00
2019-12-21 11:06:21 +00:00
AuthorizationResponse auth = AuthorizationResponse.fromIntent(data);
if (auth == null)
throw AuthorizationException.fromIntent(data);
2019-12-21 15:03:57 +00:00
final EmailProvider provider = EmailProvider.getProvider(getContext(), auth.state);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String json = prefs.getString("oauth." + provider.id, null);
prefs.edit().remove("oauth." + provider.id).apply();
final AuthState authState = AuthState.jsonDeserialize(json);
Log.i("OAuth get token provider=" + provider.id);
authState.update(auth, null);
if (BuildConfig.DEBUG)
Log.i("OAuth response=" + authState.jsonSerializeString());
2019-12-21 11:06:21 +00:00
2019-12-21 15:03:57 +00:00
AuthorizationService authService = new AuthorizationService(getContext());
ClientAuthentication clientAuth;
if (provider.oauth.clientSecret == null)
clientAuth = NoClientAuthentication.INSTANCE;
else
clientAuth = new ClientSecretPost(provider.oauth.clientSecret);
2021-09-25 12:46:24 +00:00
TokenRequest.Builder builder = new TokenRequest.Builder(
auth.request.configuration,
auth.request.clientId)
.setGrantType(GrantTypeValues.AUTHORIZATION_CODE)
.setRedirectUri(auth.request.redirectUri)
.setCodeVerifier(auth.request.codeVerifier)
.setAuthorizationCode(auth.authorizationCode)
.setAdditionalParameters(Collections.<String, String>emptyMap())
.setNonce(auth.request.nonce);
2022-03-24 07:49:33 +00:00
if (provider.oauth.tokenScopes)
2021-09-25 12:46:24 +00:00
builder.setScope(TextUtils.join(" ", provider.oauth.scopes));
TokenRequest request = builder.build();
2019-12-21 15:03:57 +00:00
authService.performTokenRequest(
2021-09-25 12:46:24 +00:00
request,
2019-12-21 15:03:57 +00:00
clientAuth,
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(TokenResponse access, AuthorizationException error) {
try {
if (access == null)
throw error;
Log.i("OAuth got token provider=" + provider.id);
2020-06-07 18:54:09 +00:00
if (BuildConfig.DEBUG)
Log.i("TokenResponse=" + access.jsonSerializeString());
2019-12-21 15:03:57 +00:00
authState.update(access, null);
if (BuildConfig.DEBUG)
Log.i("OAuth response=" + authState.jsonSerializeString());
if (TextUtils.isEmpty(access.refreshToken))
throw new IllegalStateException("No refresh token");
2020-06-07 18:54:09 +00:00
onOAuthorized(access.accessToken, access.idToken, authState);
2019-12-21 15:03:57 +00:00
} catch (Throwable ex) {
showError(ex);
}
}
});
2019-12-21 11:06:21 +00:00
} catch (Throwable ex) {
showError(ex);
}
}
2020-06-07 18:54:09 +00:00
private void onOAuthorized(String accessToken, String idToken, AuthState state) {
2021-10-16 05:53:46 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
2019-12-21 11:06:21 +00:00
Bundle args = new Bundle();
2019-12-21 15:03:57 +00:00
args.putString("id", id);
2019-12-21 11:06:21 +00:00
args.putString("name", name);
args.putString("token", accessToken);
2020-06-07 18:54:09 +00:00
args.putString("jwt", idToken);
2019-12-21 11:06:21 +00:00
args.putString("state", state.jsonSerializeString());
2020-01-09 16:53:00 +00:00
args.putBoolean("askAccount", askAccount);
2020-01-09 11:15:41 +00:00
args.putString("personal", etName.getText().toString().trim());
args.putString("address", etEmail.getText().toString().trim());
2020-11-26 08:45:04 +00:00
args.putBoolean("update", cbUpdate.isChecked());
2019-12-21 11:06:21 +00:00
new SimpleTask<Void>() {
2020-08-06 20:22:47 +00:00
@Override
protected void onPreExecute(Bundle args) {
tvConfiguring.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
tvConfiguring.setVisibility(View.GONE);
}
2019-12-21 11:06:21 +00:00
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
2019-12-21 15:03:57 +00:00
String id = args.getString("id");
2019-12-21 11:06:21 +00:00
String name = args.getString("name");
String token = args.getString("token");
2020-06-07 18:54:09 +00:00
String jwt = args.getString("jwt");
2019-12-21 11:06:21 +00:00
String state = args.getString("state");
2020-01-09 16:53:00 +00:00
boolean askAccount = args.getBoolean("askAccount", false);
2020-01-09 11:15:41 +00:00
String personal = args.getString("personal");
String address = args.getString("address");
2019-12-21 11:06:21 +00:00
2020-06-30 17:12:26 +00:00
EmailProvider provider = EmailProvider.getProvider(context, id);
String aprotocol = (provider.imap.starttls ? "imap" : "imaps");
2020-08-23 07:28:10 +00:00
int aencryption = (provider.imap.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
2021-06-17 16:11:06 +00:00
String iprotocol = (provider.smtp.starttls ? "smtp" : "smtps");
int iencryption = (provider.smtp.starttls ? EmailService.ENCRYPTION_STARTTLS : EmailService.ENCRYPTION_SSL);
2020-06-30 17:12:26 +00:00
2021-09-25 14:29:47 +00:00
if ("outlook".equals(id) && BuildConfig.DEBUG) {
DB db = DB.getInstance(context);
// Create account
EntityAccount account = new EntityAccount();
account.host = provider.imap.host;
account.encryption = aencryption;
account.port = provider.imap.port;
account.auth_type = AUTH_TYPE_OAUTH;
account.provider = provider.id;
account.user = address;
account.password = state;
int at = account.user.indexOf('@');
String user = account.user.substring(0, at);
account.name = provider.name + "/" + user;
account.synchronize = true;
account.primary = false;
if (provider.keepalive > 0)
account.poll_interval = provider.keepalive;
account.partial_fetch = provider.partial;
account.created = new Date().getTime();
account.last_connected = account.created;
account.id = db.account().insertAccount(account);
args.putLong("account", account.id);
EntityLog.log(context, "OAuth account=" + account.name);
EntityFolder folder = new EntityFolder("INBOX", EntityFolder.INBOX);
folder.account = account.id;
folder.setProperties();
folder.setSpecials(account);
folder.id = db.folder().insertFolder(folder);
EntityLog.log(context, "OAuth folder=" + folder.name + " type=" + folder.type);
if (folder.synchronize)
EntityOperation.sync(context, folder.id, true);
return null;
}
2021-08-19 19:00:14 +00:00
/*
* Outlook shared mailbox
* Authenticate: main/shared account
* IMAP: shared account
* SMTP: main account
* https://docs.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#sasl-xoauth2-authentication-for-shared-mailboxes-in-office-365
*/
String username;
String sharedname;
int backslash = address.indexOf('\\');
if (backslash > 0) {
username = address.substring(0, backslash);
sharedname = address.substring(backslash + 1);
} else {
username = address;
sharedname = null;
}
2020-08-25 07:23:23 +00:00
2021-05-31 18:56:39 +00:00
List<String> usernames = new ArrayList<>();
2021-08-19 19:00:14 +00:00
usernames.add(sharedname == null ? username : sharedname);
2021-08-19 19:00:14 +00:00
if (token != null && sharedname == null) {
2020-08-25 07:23:23 +00:00
// https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens
2021-05-31 18:56:39 +00:00
String[] segments = token.split("\\.");
2020-06-30 19:33:53 +00:00
if (segments.length > 1)
try {
String payload = new String(Base64.decode(segments[1], Base64.DEFAULT));
EntityLog.log(context, "token payload=" + payload);
2020-06-07 18:54:09 +00:00
JSONObject jpayload = new JSONObject(payload);
2021-06-18 07:35:31 +00:00
2021-05-31 18:56:39 +00:00
if (jpayload.has("preferred_username")) {
String u = jpayload.getString("preferred_username");
2021-06-17 15:59:20 +00:00
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
2021-05-31 18:56:39 +00:00
usernames.add(u);
}
2021-06-18 07:35:31 +00:00
2020-08-25 07:23:23 +00:00
if (jpayload.has("unique_name")) {
2021-05-31 18:56:39 +00:00
String u = jpayload.getString("unique_name");
2021-06-17 15:59:20 +00:00
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
2021-05-31 18:56:39 +00:00
usernames.add(u);
}
2021-06-18 07:35:31 +00:00
2021-05-31 18:56:39 +00:00
if (jpayload.has("upn")) {
String u = jpayload.getString("upn");
2021-06-17 15:59:20 +00:00
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
2021-05-31 18:56:39 +00:00
usernames.add(u);
2020-06-07 18:54:09 +00:00
}
2020-08-25 07:23:23 +00:00
} catch (Throwable ex) {
Log.w(ex);
}
}
2021-08-19 19:00:14 +00:00
if (jwt != null && sharedname == null) {
2020-08-25 07:23:23 +00:00
// https://docs.microsoft.com/en-us/azure/active-directory/develop/id-tokens
String[] segments = jwt.split("\\.");
if (segments.length > 1)
try {
// https://jwt.ms/
String payload = new String(Base64.decode(segments[1], Base64.DEFAULT));
EntityLog.log(context, "jwt payload=" + payload);
2021-05-31 18:56:39 +00:00
JSONObject jpayload = new JSONObject(payload);
2021-06-18 07:35:31 +00:00
2021-05-31 18:56:39 +00:00
if (jpayload.has("preferred_username")) {
String u = jpayload.getString("preferred_username");
2021-06-17 15:59:20 +00:00
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
2021-05-31 18:56:39 +00:00
usernames.add(u);
}
2021-06-18 07:35:31 +00:00
2021-05-31 18:56:39 +00:00
if (jpayload.has("email")) {
String u = jpayload.getString("email");
2021-06-17 15:59:20 +00:00
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
2021-05-31 18:56:39 +00:00
usernames.add(u);
}
2021-06-18 07:35:31 +00:00
2021-05-31 18:56:39 +00:00
if (jpayload.has("unique_name")) {
String u = jpayload.getString("unique_name");
2021-06-17 15:59:20 +00:00
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
2021-05-31 18:56:39 +00:00
usernames.add(u);
}
2021-06-18 07:35:31 +00:00
if (jpayload.has("verified_primary_email")) {
JSONArray jsecondary =
jpayload.getJSONArray("verified_primary_email");
for (int i = 0; i < jsecondary.length(); i++) {
String u = jsecondary.getString(i);
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u);
}
2021-06-18 07:35:31 +00:00
}
if (jpayload.has("verified_secondary_email")) {
JSONArray jsecondary =
jpayload.getJSONArray("verified_secondary_email");
for (int i = 0; i < jsecondary.length(); i++) {
String u = jsecondary.getString(i);
if (!TextUtils.isEmpty(u) && !usernames.contains(u))
usernames.add(u);
2021-06-18 07:35:31 +00:00
}
}
2020-06-07 18:54:09 +00:00
} catch (Throwable ex) {
Log.e(ex);
}
}
2021-06-17 15:59:20 +00:00
if (usernames.size() > 1)
for (String alt : usernames) {
EntityLog.log(context, "Trying username=" + alt);
2021-06-17 16:11:06 +00:00
try {
try (EmailService aservice = new EmailService(
context, aprotocol, null, aencryption, false,
EmailService.PURPOSE_CHECK, true)) {
aservice.connect(
provider.imap.host, provider.imap.port,
AUTH_TYPE_OAUTH, provider.id,
alt, state,
null, null);
}
try (EmailService iservice = new EmailService(
context, iprotocol, null, iencryption, false,
EmailService.PURPOSE_CHECK, true)) {
iservice.connect(
provider.smtp.host, provider.smtp.port,
AUTH_TYPE_OAUTH, provider.id,
alt, state,
null, null);
}
2021-06-17 15:59:20 +00:00
EntityLog.log(context, "Using username=" + alt);
username = alt;
2021-06-17 16:11:06 +00:00
break;
} catch (Throwable ex) {
Log.w(ex);
2021-06-17 15:59:20 +00:00
}
2021-05-31 18:56:39 +00:00
}
2019-12-21 11:55:46 +00:00
List<Pair<String, String>> identities = new ArrayList<>();
2019-12-21 11:06:21 +00:00
2020-08-25 07:23:23 +00:00
if (askAccount)
2021-08-19 19:00:14 +00:00
identities.add(new Pair<>(username, personal));
2021-04-24 16:10:03 +00:00
else if ("mailru".equals(id)) {
URL url = new URL("https://oauth.mail.ru/userinfo?access_token=" + token);
Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(MAILRU_TIMEOUT);
connection.setConnectTimeout(MAILRU_TIMEOUT);
2021-05-14 09:47:14 +00:00
connection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context));
2021-04-24 16:10:03 +00:00
connection.connect();
try {
2021-05-19 15:20:42 +00:00
int status = connection.getResponseCode();
if (status != HttpsURLConnection.HTTP_OK)
2021-07-15 06:36:55 +00:00
throw new FileNotFoundException("Error " + status + ": " + connection.getResponseMessage());
2021-05-19 15:20:42 +00:00
2021-04-24 16:10:03 +00:00
String json = Helper.readStream(connection.getInputStream());
Log.i("json=" + json);
JSONObject data = new JSONObject(json);
name = data.getString("name");
2021-08-19 19:00:14 +00:00
username = data.getString("email");
identities.add(new Pair<>(username, name));
2021-04-24 16:10:03 +00:00
} finally {
connection.disconnect();
}
} else
2019-12-21 15:03:57 +00:00
throw new IllegalArgumentException("Unknown provider=" + id);
2019-12-21 11:06:21 +00:00
2020-07-29 12:06:40 +00:00
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ani = (cm == null ? null : cm.getActiveNetworkInfo());
if (ani == null || !ani.isConnected())
throw new IllegalArgumentException(context.getString(R.string.title_no_internet));
2020-08-25 07:23:23 +00:00
Log.i("OAuth username=" + username);
2019-12-21 11:55:46 +00:00
for (Pair<String, String> identity : identities)
Log.i("OAuth identity=" + identity.first + "/" + identity.second);
2019-12-21 11:06:21 +00:00
2019-12-21 15:03:57 +00:00
List<EntityFolder> folders;
2019-12-21 11:06:21 +00:00
2019-12-21 15:03:57 +00:00
Log.i("OAuth checking IMAP provider=" + provider.id);
2021-06-17 16:11:06 +00:00
try (EmailService aservice = new EmailService(
2020-08-23 07:28:10 +00:00
context, aprotocol, null, aencryption, false,
EmailService.PURPOSE_CHECK, true)) {
2021-06-17 16:11:06 +00:00
aservice.connect(
2020-02-06 12:06:10 +00:00
provider.imap.host, provider.imap.port,
2020-10-25 21:20:48 +00:00
AUTH_TYPE_OAUTH, provider.id,
2021-08-19 19:00:14 +00:00
sharedname == null ? username : sharedname, state,
2020-02-10 15:35:14 +00:00
null, null);
2019-12-21 11:06:21 +00:00
2021-06-17 16:11:06 +00:00
folders = aservice.getFolders();
2019-12-21 15:03:57 +00:00
}
2019-12-21 11:06:21 +00:00
2019-12-21 15:03:57 +00:00
Log.i("OAuth checking SMTP provider=" + provider.id);
2020-07-02 11:52:42 +00:00
Long max_size;
2020-08-23 07:28:10 +00:00
2020-02-06 12:06:10 +00:00
try (EmailService iservice = new EmailService(
2020-08-23 07:28:10 +00:00
context, iprotocol, null, iencryption, false,
EmailService.PURPOSE_CHECK, true)) {
2020-02-06 12:06:10 +00:00
iservice.connect(
provider.smtp.host, provider.smtp.port,
2020-10-25 21:20:48 +00:00
AUTH_TYPE_OAUTH, provider.id,
2020-08-25 07:23:23 +00:00
username, state,
2020-02-10 15:35:14 +00:00
null, null);
2020-07-02 11:52:42 +00:00
max_size = iservice.getMaxSize();
2019-12-21 15:03:57 +00:00
}
2019-12-21 11:06:21 +00:00
2019-12-21 15:03:57 +00:00
Log.i("OAuth passed provider=" + provider.id);
2019-12-21 11:06:21 +00:00
2021-08-13 13:09:14 +00:00
EntityAccount update = null;
2019-12-21 15:03:57 +00:00
DB db = DB.getInstance(context);
try {
db.beginTransaction();
2019-12-21 11:06:21 +00:00
2021-10-06 08:44:16 +00:00
if (args.getBoolean("update")) {
List<EntityAccount> accounts =
2021-12-24 07:18:25 +00:00
db.account().getAccounts(username, new int[]{AUTH_TYPE_OAUTH, AUTH_TYPE_PASSWORD});
2021-10-06 08:44:16 +00:00
if (accounts != null && accounts.size() == 1)
update = accounts.get(0);
}
2020-11-25 18:05:32 +00:00
if (update == null) {
EntityAccount primary = db.account().getPrimaryAccount();
// Create account
EntityAccount account = new EntityAccount();
account.host = provider.imap.host;
account.encryption = aencryption;
account.port = provider.imap.port;
account.auth_type = AUTH_TYPE_OAUTH;
account.provider = provider.id;
2021-08-19 19:00:14 +00:00
account.user = (sharedname == null ? username : sharedname);
2020-11-25 18:05:32 +00:00
account.password = state;
2021-08-19 19:00:14 +00:00
int at = account.user.indexOf('@');
String user = account.user.substring(0, at);
2020-11-25 18:05:32 +00:00
account.name = provider.name + "/" + user;
account.synchronize = true;
account.primary = (primary == null);
if (provider.keepalive > 0)
account.poll_interval = provider.keepalive;
account.partial_fetch = provider.partial;
account.created = new Date().getTime();
account.last_connected = account.created;
account.id = db.account().insertAccount(account);
args.putLong("account", account.id);
EntityLog.log(context, "OAuth account=" + account.name);
// Create folders
for (EntityFolder folder : folders) {
EntityFolder existing = db.folder().getFolderByName(account.id, folder.name);
if (existing == null) {
folder.account = account.id;
2020-12-01 20:07:13 +00:00
folder.setSpecials(account);
2020-11-25 18:05:32 +00:00
folder.id = db.folder().insertFolder(folder);
EntityLog.log(context, "OAuth folder=" + folder.name + " type=" + folder.type);
if (folder.synchronize)
2021-01-24 13:02:35 +00:00
EntityOperation.sync(context, folder.id, true);
2020-11-25 18:05:32 +00:00
}
2020-03-09 14:57:58 +00:00
}
2019-12-21 11:06:21 +00:00
2020-11-25 18:05:32 +00:00
// Set swipe left/right folder
for (EntityFolder folder : folders)
if (EntityFolder.TRASH.equals(folder.type))
account.swipe_left = folder.id;
else if (EntityFolder.ARCHIVE.equals(folder.type))
account.swipe_right = folder.id;
db.account().updateAccount(account);
// Create identities
for (Pair<String, String> identity : identities) {
EntityIdentity ident = new EntityIdentity();
ident.name = identity.second;
ident.email = identity.first;
ident.account = account.id;
ident.host = provider.smtp.host;
ident.encryption = iencryption;
ident.port = provider.smtp.port;
ident.auth_type = AUTH_TYPE_OAUTH;
ident.provider = provider.id;
ident.user = username;
ident.password = state;
2021-08-13 20:32:34 +00:00
ident.use_ip = provider.useip;
2020-11-25 18:05:32 +00:00
ident.synchronize = true;
ident.primary = ident.user.equals(ident.email);
ident.max_size = max_size;
ident.id = db.identity().insertIdentity(ident);
EntityLog.log(context, "OAuth identity=" + ident.name + " email=" + ident.email);
}
} else {
2021-08-13 13:09:14 +00:00
args.putLong("account", update.id);
2020-11-25 18:05:32 +00:00
EntityLog.log(context, "OAuth update account=" + update.name);
2021-10-07 05:15:58 +00:00
db.account().setAccountSynchronize(update.id, true);
2021-12-24 07:18:25 +00:00
db.account().setAccountPassword(update.id, state, AUTH_TYPE_OAUTH);
db.identity().setIdentityPassword(update.id, update.user, state, update.auth_type, AUTH_TYPE_OAUTH);
2019-12-21 15:03:57 +00:00
}
2019-12-21 11:06:21 +00:00
2019-12-21 15:03:57 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2019-12-21 11:06:21 +00:00
2021-08-13 13:09:14 +00:00
if (update == null)
ServiceSynchronize.eval(context, "OAuth");
else {
args.putBoolean("updated", true);
ServiceSynchronize.reload(context, update.id, true, "OAuth");
}
2019-12-21 11:06:21 +00:00
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
pbOAuth.setVisibility(View.GONE);
2021-08-13 13:09:14 +00:00
boolean updated = args.getBoolean("updated");
if (updated) {
2020-11-26 08:45:04 +00:00
finish();
ToastEx.makeText(getContext(), R.string.title_setup_oauth_updated, Toast.LENGTH_LONG).show();
2020-11-26 08:45:04 +00:00
} else {
2021-06-20 13:28:45 +00:00
FragmentDialogAccount fragment = new FragmentDialogAccount();
2020-11-26 08:45:04 +00:00
fragment.setArguments(args);
fragment.setTargetFragment(FragmentOAuth.this, ActivitySetup.REQUEST_DONE);
fragment.show(getParentFragmentManager(), "oauth:review");
}
2019-12-21 11:06:21 +00:00
}
@Override
protected void onException(Bundle args, Throwable ex) {
showError(ex);
}
}.execute(this, args, "oauth:configure");
}
2019-12-21 19:00:06 +00:00
private void onHandleCancel() {
2020-06-30 20:57:34 +00:00
etName.setEnabled(true);
etEmail.setEnabled(true);
etTenant.setEnabled(true);
2020-11-26 08:45:04 +00:00
cbUpdate.setEnabled(true);
2019-12-21 19:00:06 +00:00
btnOAuth.setEnabled(true);
pbOAuth.setVisibility(View.GONE);
}
2020-05-09 17:07:06 +00:00
private void showError(Throwable ex) {
2019-12-21 11:06:21 +00:00
Log.e(ex);
2021-10-16 05:24:24 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
2019-12-21 11:06:21 +00:00
if (ex instanceof IllegalArgumentException)
tvError.setText(ex.getMessage());
else
2021-05-03 19:35:03 +00:00
tvError.setText(Log.formatThrowable(ex, false));
2019-12-21 11:06:21 +00:00
grpError.setVisibility(View.VISIBLE);
2019-12-21 15:03:57 +00:00
if ("gmail".equals(id))
2019-12-21 11:06:21 +00:00
tvGmailDraftsHint.setVisibility(View.VISIBLE);
2022-03-24 07:49:33 +00:00
if ("office365".equals(id) || "outlook".equals(id)) {
2020-12-12 15:18:04 +00:00
if (ex instanceof AuthenticationFailedException)
tvOfficeAuthHint.setVisibility(View.VISIBLE);
2020-11-19 12:15:55 +00:00
}
2020-06-13 11:54:37 +00:00
2021-10-21 13:16:28 +00:00
EmailProvider provider;
try {
provider = EmailProvider.getProvider(getContext(), id);
} catch (Throwable exex) {
Log.e(exex);
provider = null;
}
btnHelp.setVisibility((provider != null && provider.link != null ? View.VISIBLE : View.GONE));
2020-06-30 20:57:34 +00:00
etName.setEnabled(true);
etEmail.setEnabled(true);
etTenant.setEnabled(true);
2020-11-26 08:45:04 +00:00
cbUpdate.setEnabled(true);
2019-12-21 15:23:12 +00:00
btnOAuth.setEnabled(true);
pbOAuth.setVisibility(View.GONE);
2020-08-23 15:34:14 +00:00
getMainHandler().post(new Runnable() {
2019-12-21 11:06:21 +00:00
@Override
public void run() {
2021-09-28 19:15:46 +00:00
if (!getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
return;
2019-12-21 11:06:21 +00:00
scroll.smoothScrollTo(0, tvError.getBottom());
}
});
}
2020-05-09 17:07:06 +00:00
private void hideError() {
2021-10-21 13:16:28 +00:00
btnHelp.setVisibility(View.GONE);
2019-12-21 11:06:21 +00:00
grpError.setVisibility(View.GONE);
tvGmailDraftsHint.setVisibility(View.GONE);
2020-06-13 11:54:37 +00:00
tvOfficeAuthHint.setVisibility(View.GONE);
2019-12-21 11:06:21 +00:00
}
}