Search in sources :

Example 6 with ExistenceCheckRemoteOperation

use of com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation in project android by owncloud.

the class DetectAuthenticationMethodOperation method run.

/**
     *  Performs the operation.
     * 
     *  Triggers a check of existence on the root folder of the server, granting
     *  that the request is not authenticated.
     *  
     *  Analyzes the result of check to find out what authentication method, if
     *  any, is requested by the server.
     */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    AuthenticationMethod authMethod = AuthenticationMethod.UNKNOWN;
    RemoteOperation operation = new ExistenceCheckRemoteOperation("", mContext, false);
    client.clearCredentials();
    client.setFollowRedirects(false);
    // try to access the root folder, following redirections but not SAML SSO redirections
    result = operation.execute(client);
    String redirectedLocation = result.getRedirectedLocation();
    while (redirectedLocation != null && redirectedLocation.length() > 0 && !result.isIdPRedirection()) {
        client.setBaseUri(Uri.parse(result.getRedirectedLocation()));
        result = operation.execute(client);
        redirectedLocation = result.getRedirectedLocation();
    }
    // analyze response  
    if (result.getHttpCode() == HttpStatus.SC_UNAUTHORIZED) {
        String authRequest = ((result.getAuthenticateHeader()).trim()).toLowerCase();
        if (authRequest.startsWith("basic")) {
            authMethod = AuthenticationMethod.BASIC_HTTP_AUTH;
        } else if (authRequest.startsWith("bearer")) {
            authMethod = AuthenticationMethod.BEARER_TOKEN;
        }
    // else - fall back to UNKNOWN
    } else if (result.isSuccess()) {
        authMethod = AuthenticationMethod.NONE;
    } else if (result.isIdPRedirection()) {
        authMethod = AuthenticationMethod.SAML_WEB_SSO;
    }
    // else - fall back to UNKNOWN
    Log_OC.d(TAG, "Authentication method found: " + authenticationMethodToString(authMethod));
    if (!authMethod.equals(AuthenticationMethod.UNKNOWN)) {
        result = new RemoteOperationResult(true, result.getHttpCode(), result.getHttpPhrase(), null);
    }
    ArrayList<Object> data = new ArrayList<Object>();
    data.add(authMethod);
    result.setData(data);
    // same result instance, so that other errors
    return result;
// can be handled by the caller transparently
}
Also used : ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ArrayList(java.util.ArrayList)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)6 ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)6 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)3 ArrayList (java.util.ArrayList)2 Uri (android.net.Uri)1 OCFile (com.owncloud.android.datamodel.OCFile)1 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)1 OwnCloudCredentials (com.owncloud.android.lib.common.OwnCloudCredentials)1 RedirectionPath (com.owncloud.android.lib.common.network.RedirectionPath)1 GetRemoteUserInfoOperation (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation)1 SyncOperation (com.owncloud.android.operations.common.SyncOperation)1