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