|
|
|
@ -120,7 +120,7 @@ public class ServiceAuthenticator extends Authenticator {
|
|
|
|
|
return authState.getAccessToken();
|
|
|
|
|
} else if (auth == AUTH_TYPE_OAUTH && provider != null) {
|
|
|
|
|
AuthState authState = AuthState.jsonDeserialize(password);
|
|
|
|
|
OAuthRefresh(context, provider, user, authState, forceRefresh);
|
|
|
|
|
OAuthRefresh(context, provider, auth, user, authState, forceRefresh);
|
|
|
|
|
Long expiration = authState.getAccessTokenExpirationTime();
|
|
|
|
|
if (expiration != null)
|
|
|
|
|
EntityLog.log(context, user + " token expiration=" + new Date(expiration));
|
|
|
|
@ -156,7 +156,7 @@ public class ServiceAuthenticator extends Authenticator {
|
|
|
|
|
void onPasswordChanged(Context context, String newPassword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void OAuthRefresh(Context context, String id, String user, AuthState authState, boolean forceRefresh)
|
|
|
|
|
static void OAuthRefresh(Context context, String id, int auth_type, String user, AuthState authState, boolean forceRefresh)
|
|
|
|
|
throws MessagingException {
|
|
|
|
|
try {
|
|
|
|
|
long now = new Date().getTime();
|
|
|
|
@ -171,26 +171,30 @@ public class ServiceAuthenticator extends Authenticator {
|
|
|
|
|
if (needsRefresh)
|
|
|
|
|
authState.setNeedsTokenRefresh(true);
|
|
|
|
|
|
|
|
|
|
EntityLog.log(context, EntityLog.Type.General, "Token user=" + id + ":" + user +
|
|
|
|
|
EntityLog.log(context, EntityLog.Type.General, "Token" +
|
|
|
|
|
" provider=" + id + ":" + getAuthTypeName(auth_type) +
|
|
|
|
|
" user" + user +
|
|
|
|
|
" expiration=" + (expiration == null ? null : new Date(expiration)) +
|
|
|
|
|
" need=" + needsRefresh + "/" + authState.getNeedsTokenRefresh() +
|
|
|
|
|
" force=" + forceRefresh);
|
|
|
|
|
|
|
|
|
|
ClientAuthentication clientAuth;
|
|
|
|
|
EmailProvider provider = EmailProvider.getProvider(context, id);
|
|
|
|
|
if (provider.oauth.clientSecret == null)
|
|
|
|
|
EmailProvider.OAuth oauth = (auth_type == AUTH_TYPE_GRAPH ? provider.graph : provider.oauth);
|
|
|
|
|
|
|
|
|
|
if (oauth.clientSecret == null)
|
|
|
|
|
clientAuth = NoClientAuthentication.INSTANCE;
|
|
|
|
|
else
|
|
|
|
|
clientAuth = new ClientSecretPost(provider.oauth.clientSecret);
|
|
|
|
|
clientAuth = new ClientSecretPost(oauth.clientSecret);
|
|
|
|
|
|
|
|
|
|
ErrorHolder holder = new ErrorHolder();
|
|
|
|
|
Semaphore semaphore = new Semaphore(0);
|
|
|
|
|
|
|
|
|
|
Map<String, String> params = new LinkedHashMap<>();
|
|
|
|
|
if (provider.oauth.tokenScopes)
|
|
|
|
|
params.put("scope", TextUtils.join(" ", provider.oauth.scopes));
|
|
|
|
|
if (oauth.tokenScopes)
|
|
|
|
|
params.put("scope", TextUtils.join(" ", oauth.scopes));
|
|
|
|
|
|
|
|
|
|
Log.i("OAuth refresh user=" + id + ":" + user);
|
|
|
|
|
Log.i("OAuth refresh provider=" + id + ":" + getAuthTypeName(auth_type) + " user=" + user);
|
|
|
|
|
AppAuthConfiguration config = new AppAuthConfiguration.Builder()
|
|
|
|
|
.setBrowserMatcher(new BrowserMatcher() {
|
|
|
|
|
@Override
|
|
|
|
@ -218,17 +222,17 @@ public class ServiceAuthenticator extends Authenticator {
|
|
|
|
|
|
|
|
|
|
authService.dispose();
|
|
|
|
|
|
|
|
|
|
Log.i("OAuth refreshed user=" + id + ":" + user);
|
|
|
|
|
|
|
|
|
|
if (holder.error != null) {
|
|
|
|
|
if (holder.error == null)
|
|
|
|
|
Log.i("OAuth refreshed provider=" + id + ":" + getAuthTypeName(auth_type) + " user=" + user);
|
|
|
|
|
else {
|
|
|
|
|
Log.e(new Throwable("Token refresh failed" +
|
|
|
|
|
" id=" + id +
|
|
|
|
|
" provider=" + id + ":" + getAuthTypeName(auth_type) +
|
|
|
|
|
" error=" + holder.error.getMessage(),
|
|
|
|
|
holder.error));
|
|
|
|
|
throw holder.error;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
throw new MessagingException("OAuth refresh id=" + id, ex);
|
|
|
|
|
throw new MessagingException("OAuth refresh provider=" + id + ":" + getAuthTypeName(auth_type), ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|