1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-24 16:53:37 +00:00

Gmail refresh refactoring

This commit is contained in:
M66B 2020-10-25 15:55:27 +01:00
parent 8f33714f6e
commit 63d1e511f3
4 changed files with 29 additions and 21 deletions

View file

@ -776,7 +776,7 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
if (account.auth_type == EmailService.AUTH_TYPE_GMAIL) {
AccountManager am = AccountManager.get(context);
boolean found = false;
for (Account google : am.getAccountsByType("com.google"))
for (Account google : am.getAccountsByType(EmailService.TYPE_GOOGLE))
if (account.user.equals(google.name)) {
found = true;
break;

View file

@ -3,6 +3,7 @@ package eu.faircode.email;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.content.SharedPreferences;
import android.security.KeyChain;
@ -104,6 +105,8 @@ public class EmailService implements AutoCloseable {
private ExecutorService executor = Helper.getBackgroundExecutor(0, "mail");
static final String TYPE_GOOGLE = "com.google";
static final int AUTH_TYPE_PASSWORD = 1;
static final int AUTH_TYPE_GMAIL = 2;
static final int AUTH_TYPE_OAUTH = 3;
@ -347,22 +350,9 @@ public class EmailService implements AutoCloseable {
// Refresh token
if (auth == AUTH_TYPE_GMAIL)
try {
String type = "com.google";
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType(type);
for (Account account : accounts)
if (user.equals(account.name)) {
Log.i("Refreshing token user=" + user);
am.invalidateAuthToken(type, password);
String token = am.blockingGetAuthToken(account, getAuthTokenType(type), true);
if (token == null)
throw new AuthenticatorException("No token on refresh for " + user);
connect(host, port, auth, user, token, factory);
return token;
}
throw new AuthenticatorException("Account not found for " + user);
String token = GmailRefresh(context, user, password);
connect(host, port, auth, user, token, factory);
return token;
} catch (Exception ex1) {
Log.e(ex1);
throw new AuthenticationFailedException(ex.getMessage(), ex1);
@ -619,6 +609,24 @@ public class EmailService implements AutoCloseable {
AuthorizationException error;
}
private static String GmailRefresh(Context context, String user, String password) throws AuthenticatorException, OperationCanceledException, IOException {
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType(TYPE_GOOGLE);
for (Account account : accounts)
if (user.equals(account.name)) {
Log.i("Refreshing token user=" + user);
if (password != null)
am.invalidateAuthToken(TYPE_GOOGLE, password);
String token = am.blockingGetAuthToken(account, getAuthTokenType(TYPE_GOOGLE), true);
if (token == null)
throw new AuthenticatorException("No token on refresh for " + user);
return token;
}
throw new AuthenticatorException("Account not found for " + user);
}
private static AuthState OAuthRefresh(Context context, String id, String json) throws MessagingException {
try {
AuthState authState = AuthState.jsonDeserialize(json);

View file

@ -1334,12 +1334,12 @@ public class FragmentAccount extends FragmentBase {
return null;
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType("com.google");
Account[] accounts = am.getAccountsByType(EmailService.TYPE_GOOGLE);
for (Account google : accounts)
if (account.user.equals(google.name))
return am.blockingGetAuthToken(
google,
EmailService.getAuthTokenType("com.google"),
EmailService.getAuthTokenType(EmailService.TYPE_GOOGLE),
true);
return null;

View file

@ -987,12 +987,12 @@ public class FragmentIdentity extends FragmentBase {
return null;
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType("com.google");
Account[] accounts = am.getAccountsByType(EmailService.TYPE_GOOGLE);
for (Account google : accounts)
if (identity.user.equals(google.name))
return am.blockingGetAuthToken(
google,
EmailService.getAuthTokenType("com.google"),
EmailService.getAuthTokenType(EmailService.TYPE_GOOGLE),
true);
return null;