Adapt behaviour Authentication AyncTask + rotations
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / AuthenticatorAsyncTask.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2015 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17 package com.owncloud.android.authentication;
18
19 import android.app.Activity;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.os.AsyncTask;
23
24 import com.owncloud.android.MainApp;
25 import com.owncloud.android.lib.common.OwnCloudClient;
26 import com.owncloud.android.lib.common.OwnCloudClientFactory;
27 import com.owncloud.android.lib.common.OwnCloudCredentials;
28 import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
29 import com.owncloud.android.lib.common.accounts.AccountTypeUtils;
30 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
31 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
32
33 import java.lang.ref.WeakReference;
34
35
36 /**
37 * Async Task to verify the credentials of a user
38 *
39 * @author masensio on 09/02/2015.
40 */
41 public class AuthenticatorAsyncTask extends AsyncTask<String, Void, RemoteOperationResult> {
42
43 private static String REMOTE_PATH = "/";
44 private static boolean SUCCESS_IF_ABSENT = false;
45
46 private Context mContext;
47 private final WeakReference<OnAuthenticatorTaskListener> mListener;
48 protected Activity mActivity;
49
50 public AuthenticatorAsyncTask(Activity activity) {
51 mContext = activity.getApplicationContext();
52 mListener = new WeakReference<>((OnAuthenticatorTaskListener)activity);
53 mActivity = activity;
54 }
55
56 @Override
57 protected RemoteOperationResult doInBackground(String... params) {
58
59 RemoteOperationResult result;
60 if (params!= null && params.length==5) {
61 String url = params[0];
62 String username = params[1];
63 String password = params[2];
64 String authToken = params[3];
65 String authTokenType = params[4];
66
67 // Client
68 String basic = AccountTypeUtils.getAuthTokenTypePass(MainApp.getAccountType());
69 String oAuth = AccountTypeUtils.getAuthTokenTypeAccessToken(MainApp.getAccountType());
70 String saml = AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType());
71
72 Uri uri = Uri.parse(url);
73 OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(uri, mContext, false);
74 OwnCloudCredentials credentials = null;
75 if (authTokenType.equals(basic)) {
76 credentials = OwnCloudCredentialsFactory.newBasicCredentials(
77 username, password); // basic
78
79 } else if (authTokenType.equals(oAuth)) {
80 credentials = OwnCloudCredentialsFactory.newBearerCredentials(
81 authToken); // bearer token
82
83 } else if (authTokenType.equals(saml)) {
84 credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(
85 authToken); // SAML SSO
86 }
87
88 client.setCredentials(credentials);
89
90 // Operation
91 ExistenceCheckRemoteOperation operation = new ExistenceCheckRemoteOperation(REMOTE_PATH,
92 mContext, SUCCESS_IF_ABSENT);
93 result = operation.execute(client);
94
95 } else {
96 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
97 }
98
99 return result;
100 }
101
102 @Override
103 protected void onPostExecute(RemoteOperationResult result) {
104
105 if (result!= null)
106 {
107 OnAuthenticatorTaskListener listener = mListener.get();
108 if (listener!= null)
109 {
110 listener.onAuthenticatorTaskCallback(result);
111 }
112 }
113 }
114 /*
115 * Interface to retrieve data from recognition task
116 */
117 public interface OnAuthenticatorTaskListener{
118
119 void onAuthenticatorTaskCallback(RemoteOperationResult result);
120 }
121 }