Fixing build
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorAsyncTask.java
1 package com.owncloud.android.authentication;
2
3 import android.app.Activity;
4 import android.content.Context;
5 import android.net.Uri;
6 import android.os.AsyncTask;
7
8 import com.owncloud.android.MainApp;
9 import com.owncloud.android.lib.common.OwnCloudClient;
10 import com.owncloud.android.lib.common.OwnCloudClientFactory;
11 import com.owncloud.android.lib.common.OwnCloudCredentials;
12 import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
13 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
14 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
15 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
16
17 import java.lang.ref.WeakReference;
18
19
20 /**
21 * Async Task to verify the credentials of a user
22 *
23 * @author masensio on 09/02/2015.
24 */
25 public class AuthenticatorAsyncTask extends AsyncTask<String, Void, RemoteOperationResult> {
26
27
28 private static String REMOTE_PATH = "/";
29 private static boolean SUCCESS_IF_ABSENT = false;
30
31 private Context mContext;
32 private final WeakReference<OnAuthenticatorTaskListener> mListener;
33
34 public AuthenticatorAsyncTask(Activity activity) {
35 mContext = activity.getApplicationContext();
36 mListener = new WeakReference<OnAuthenticatorTaskListener>((OnAuthenticatorTaskListener)activity);
37 }
38
39 @Override
40 protected RemoteOperationResult doInBackground(String... params) {
41
42 RemoteOperationResult result;
43 if (params!= null && params.length==5) {
44 String url = params[0];
45 String username = params[1];
46 String password = params[2];
47 String authToken = params[3];
48 String authTokenType = params[4];
49
50 // Client
51 String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());
52 String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType());
53 String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType());
54
55 Uri uri = Uri.parse(url);
56 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, false);
57 OwnCloudCredentials credentials = null;
58 if (authTokenType.equals(basic)) {
59 credentials = OwnCloudCredentialsFactory.newBasicCredentials(
60 username, password); // basic
61
62 } else if (authTokenType.equals(oAuth)) {
63 credentials = OwnCloudCredentialsFactory.newBearerCredentials(
64 authToken); // bearer token
65
66 } else if (authTokenType.equals(saml)) {
67 credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(
68 authToken); // SAML SSO
69 }
70
71 client.setCredentials(credentials);
72
73 // Operation
74 ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH,
75 mContext, SUCCESS_IF_ABSENT);
76 result = operation.execute(client);
77
78 } else {
79 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
80 }
81
82 return result;
83 }
84
85 @Override
86 protected void onPostExecute(RemoteOperationResult result) {
87
88 if (result!= null)
89 {
90 OnAuthenticatorTaskListener listener = mListener.get();
91 if (listener!= null)
92 {
93 listener.onAuthenticatorTaskCallback(result);
94 }
95 }
96 }
97
98 /*
99 * Interface to retrieve data from recognition task
100 */
101 public interface OnAuthenticatorTaskListener{
102
103 void onAuthenticatorTaskCallback(RemoteOperationResult result);
104 }
105 }