1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

Invalidate the client token when access token expires and try the last operation [WIP]

This commit is contained in:
davigonz 2017-07-14 13:06:08 +02:00 committed by David A. Velasco
parent fe947194c5
commit a1b1c2f9ed

View File

@ -1,5 +1,5 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2016 ownCloud GmbH.
* Copyright (C) 2017 ownCloud GmbH.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -49,33 +49,50 @@ import java.io.IOException;
* Provides methods to execute the operation both synchronously or asynchronously.
*
* @author David A. Velasco
* @author David González Verdugo
*/
public abstract class RemoteOperation implements Runnable {
private static final String TAG = RemoteOperation.class.getSimpleName();
/** OCS API header name */
/**
* OCS API header name
*/
public static final String OCS_API_HEADER = "OCS-APIREQUEST";
/** OCS API header value */
/**
* OCS API header value
*/
public static final String OCS_API_HEADER_VALUE = "true";
/** ownCloud account in the remote ownCloud server to operate */
/**
* ownCloud account in the remote ownCloud server to operate
*/
private Account mAccount = null;
/** Android Application context */
/**
* Android Application context
*/
private Context mContext = null;
/** Object to interact with the remote server */
/**
* Object to interact with the remote server
*/
private OwnCloudClient mClient = null;
/** Callback object to notify about the execution of the remote operation */
/**
* Callback object to notify about the execution of the remote operation
*/
private OnRemoteOperationListener mListener = null;
/** Handler to the thread where mListener methods will be called */
/**
* Handler to the thread where mListener methods will be called
*/
private Handler mListenerHandler = null;
/** Activity */
/**
* Activity
*/
private Activity mCallerActivity;
@ -143,10 +160,6 @@ public abstract class RemoteOperation implements Runnable {
* This method should be used whenever an ownCloud account is available, instead of
* {@link #execute(OwnCloudClient)}.
*
* @deprecated This method will be removed in version 1.0.
* Use {@link #execute(Account, Context, OnRemoteOperationListener,
* Handler)} instead.
*
* @param account ownCloud account in remote ownCloud server to reach during
* the execution of the operation.
* @param context Android context for the component calling the method.
@ -154,6 +167,9 @@ public abstract class RemoteOperation implements Runnable {
* @param listenerHandler Handler associated to the thread where the methods of the listener
* objects must be called.
* @return Thread were the remote operation is executed.
* @deprecated This method will be removed in version 1.0.
* Use {@link #execute(Account, Context, OnRemoteOperationListener,
* Handler)} instead.
*/
@Deprecated
public Thread execute(Account account, Context context, OnRemoteOperationListener listener,
@ -302,9 +318,18 @@ public abstract class RemoteOperation implements Runnable {
result = run(mClient);
repeat = false;
AccountManager mAccountManager = AccountManager.get(mContext);
String isOAuthStr = mAccountManager.getUserData(mAccount,
AccountUtils.Constants.KEY_SUPPORTS_OAUTH2);
Boolean isOAuth = Boolean.valueOf(isOAuthStr);
/** DEPRECATED BLOCK - will be removed at version 1.0 ; don't trust in this code
* to trigger authentication update */
if (mCallerActivity != null && mAccount != null && mContext != null &&
if (mAccount != null && mContext != null &&
!result.isSuccess() &&
ResultCode.UNAUTHORIZED.equals(result.getCode())
) {
@ -313,19 +338,49 @@ public abstract class RemoteOperation implements Runnable {
OwnCloudCredentials cred = mClient.getCredentials();
if (cred != null) {
/// confirmed : unauthorized operation
OwnCloudClient client;
OwnCloudAccount ocAccount;
try {
/// Step 1: Invalidate credentials of current account
ocAccount = new OwnCloudAccount(mAccount, mContext);
client = (OwnCloudClientManagerFactory.getDefaultSingleton().
removeClientFor(ocAccount));
if (client != null) {
AccountManager am = AccountManager.get(mContext);
if (cred.authTokenExpires()) {
am.invalidateAuthToken(
am.invalidateAuthToken( // SAML & OAuth
mAccount.type,
cred.getAuthToken()
);
} else {
} else { //Basic
am.clearPassword(mAccount);
}
}
/// Step 2: Get new access token using refresh token
// Params needed: clientId, clientSecret (switch credentials), grantype and
// refresh token
String refreshToken = mAccountManager.getUserData(mAccount,
AccountUtils.Constants.KEY_OAUTH2_REFRESH_TOKEN);
// mClient.executeMethod(postMethod);
/// Step 3: Save access token in account manager
} catch (AccountUtils.AccountNotFoundException e) {
e.printStackTrace();
}
mClient = null;
// when repeated, the creation of a new OwnCloudClient after erasing the saved
// credentials will trigger the login activity
if (isOAuth) {
repeat = true;
}
result = null;
}
}
@ -345,8 +400,7 @@ public abstract class RemoteOperation implements Runnable {
mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
}
});
}
else if(mListener != null) {
} else if (mListener != null) {
mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend);
}
}
@ -360,5 +414,4 @@ public abstract class RemoteOperation implements Runnable {
public final OwnCloudClient getClient() {
return mClient;
}
}