Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit c59d154

Browse files
committed
clean up credentials stuff from owncloudclient
1 parent 5582097 commit c59d154

File tree

1 file changed

+11
-112
lines changed

1 file changed

+11
-112
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java

Lines changed: 11 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525

2626
package com.owncloud.android.lib.common;
2727

28-
import android.accounts.AccountManager;
29-
import android.accounts.AccountsException;
3028
import android.net.Uri;
3129

3230
import at.bitfire.dav4jvm.exception.HttpException;
@@ -47,7 +45,6 @@
4745

4846
import java.io.IOException;
4947
import java.io.InputStream;
50-
import java.util.ArrayList;
5148
import java.util.List;
5249

5350
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
@@ -143,13 +140,6 @@ private int saveExecuteHttpMethod(HttpBaseMethod method) throws Exception {
143140
status = followRedirection(method).getLastStatus();
144141
}
145142

146-
/*
147-
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
148-
if (repeatWithFreshCredentials) {
149-
repeatCounter++;
150-
}
151-
*/
152-
153143
} while (retry && repeatCounter < MAX_RETRY_COUNT);
154144

155145
return status;
@@ -178,29 +168,18 @@ private void stacklog(int status, HttpBaseMethod method) {
178168
}
179169

180170
private int executeRedirectedHttpMethod(HttpBaseMethod method) throws Exception {
181-
boolean repeatWithFreshCredentials;
182-
int repeatCounter = 0;
183171
int status;
184-
185-
do {
186-
String requestId = RandomUtils.generateRandomUUID();
187-
188-
// Header to allow tracing requests in apache and ownCloud logs
189-
Timber.d("Executing in request with id %s", requestId);
190-
method.setRequestHeader(OC_X_REQUEST_ID, requestId);
191-
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
192-
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
193-
if (mCredentials.getHeaderAuth() != null) {
194-
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
195-
}
196-
status = method.execute();
197-
198-
repeatWithFreshCredentials = checkUnauthorizedAccess(status, repeatCounter);
199-
if (repeatWithFreshCredentials) {
200-
repeatCounter++;
201-
}
202-
} while (repeatWithFreshCredentials);
203-
172+
String requestId = RandomUtils.generateRandomUUID();
173+
174+
// Header to allow tracing requests in apache and ownCloud logs
175+
Timber.d("Executing in request with id %s", requestId);
176+
method.setRequestHeader(OC_X_REQUEST_ID, requestId);
177+
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
178+
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
179+
if (mCredentials.getHeaderAuth() != null) {
180+
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
181+
}
182+
status = method.execute();
204183
return status;
205184
}
206185

@@ -365,86 +344,6 @@ public void setAccount(OwnCloudAccount account) {
365344
this.mAccount = account;
366345
}
367346

368-
/**
369-
* Checks the status code of an execution and decides if should be repeated with fresh credentials.
370-
* <p>
371-
* Invalidates current credentials if the request failed as anauthorized.
372-
* <p>
373-
* Refresh current credentials if possible, and marks a retry.
374-
*
375-
* @param status
376-
* @param repeatCounter
377-
* @return
378-
*/
379-
private boolean checkUnauthorizedAccess(int status, int repeatCounter) {
380-
boolean credentialsWereRefreshed = false;
381-
382-
if (shouldInvalidateAccountCredentials(status)) {
383-
invalidateAccountCredentials();
384-
385-
if (getCredentials().authTokenCanBeRefreshed() &&
386-
repeatCounter < 1) {
387-
try {
388-
mAccount.loadCredentials(getContext());
389-
// if mAccount.getCredentials().length() == 0 --> refresh failed
390-
setCredentials(mAccount.getCredentials());
391-
credentialsWereRefreshed = true;
392-
393-
} catch (AccountsException | IOException e) {
394-
Timber.e(e, "Error while trying to refresh auth token for %s",
395-
mAccount.getSavedAccount().name
396-
);
397-
}
398-
399-
if (!credentialsWereRefreshed && mSingleSessionManager != null) {
400-
// if credentials are not refreshed, client must be removed
401-
// from the OwnCloudClientManager to prevent it is reused once and again
402-
mSingleSessionManager.removeClientFor(mAccount);
403-
}
404-
}
405-
// else: onExecute will finish with status 401
406-
}
407-
408-
return credentialsWereRefreshed;
409-
}
410-
411-
/**
412-
* Determines if credentials should be invalidated according the to the HTTPS status
413-
* of a network request just performed.
414-
*
415-
* @param httpStatusCode Result of the last request ran with the 'credentials' belows.
416-
* @return 'True' if credentials should and might be invalidated, 'false' if shouldn't or
417-
* cannot be invalidated with the given arguments.
418-
*/
419-
private boolean shouldInvalidateAccountCredentials(int httpStatusCode) {
420-
boolean shouldInvalidateAccountCredentials =
421-
(httpStatusCode == HttpConstants.HTTP_UNAUTHORIZED);
422-
423-
shouldInvalidateAccountCredentials &= (mCredentials != null && // real credentials
424-
!(mCredentials instanceof OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials));
425-
426-
// test if have all the needed to effectively invalidate ...
427-
shouldInvalidateAccountCredentials &= (mAccount != null && mAccount.getSavedAccount() != null && getContext() != null);
428-
429-
return shouldInvalidateAccountCredentials;
430-
}
431-
432-
/**
433-
* Invalidates credentials stored for the given account in the system {@link AccountManager} and in
434-
* current {@link SingleSessionManager#getDefaultSingleton()} instance.
435-
* <p>
436-
* {@link #shouldInvalidateAccountCredentials(int)} should be called first.
437-
*
438-
*/
439-
private void invalidateAccountCredentials() {
440-
AccountManager am = AccountManager.get(getContext());
441-
am.invalidateAuthToken(
442-
mAccount.getSavedAccount().type,
443-
mCredentials.getAuthToken()
444-
);
445-
am.clearPassword(mAccount.getSavedAccount()); // being strict, only needed for Basic Auth credentials
446-
}
447-
448347
public boolean getFollowRedirects() {
449348
return mFollowRedirects;
450349
}

0 commit comments

Comments
 (0)