1 /* ownCloud Android client application
2 * Copyright (C) 2012-2015 ownCloud Inc.
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.
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.
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/>.
17 package com
.owncloud
.android
.authentication
;
19 import android
.app
.Activity
;
20 import android
.content
.Context
;
21 import android
.net
.Uri
;
22 import android
.os
.AsyncTask
;
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
;
33 import java
.lang
.ref
.WeakReference
;
37 * Async Task to verify the credentials of a user
39 * @author masensio on 09/02/2015.
41 public class AuthenticatorAsyncTask
extends AsyncTask
<String
, Void
, RemoteOperationResult
> {
43 private static String REMOTE_PATH
= "/";
44 private static boolean SUCCESS_IF_ABSENT
= false
;
46 private Context mContext
;
47 private final WeakReference
<OnAuthenticatorTaskListener
> mListener
;
48 protected Activity mActivity
;
50 public AuthenticatorAsyncTask(Activity activity
) {
51 mContext
= activity
.getApplicationContext();
52 mListener
= new WeakReference
<OnAuthenticatorTaskListener
>((OnAuthenticatorTaskListener
)activity
);
57 protected RemoteOperationResult
doInBackground(String
... params
) {
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];
68 String basic
= AccountTypeUtils
.getAuthTokenTypePass(MainApp
.getAccountType());
69 String oAuth
= AccountTypeUtils
.getAuthTokenTypeAccessToken(MainApp
.getAccountType());
70 String saml
= AccountTypeUtils
.getAuthTokenTypeSamlSessionCookie(MainApp
.getAccountType());
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
79 } else if (authTokenType
.equals(oAuth
)) {
80 credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(
81 authToken
); // bearer token
83 } else if (authTokenType
.equals(saml
)) {
84 credentials
= OwnCloudCredentialsFactory
.newSamlSsoCredentials(
85 authToken
); // SAML SSO
88 client
.setCredentials(credentials
);
91 ExistenceCheckRemoteOperation operation
= new ExistenceCheckRemoteOperation(REMOTE_PATH
,
92 mContext
, SUCCESS_IF_ABSENT
);
93 result
= operation
.execute(client
);
96 result
= new RemoteOperationResult(RemoteOperationResult
.ResultCode
.UNKNOWN_ERROR
);
103 protected void onPostExecute(RemoteOperationResult result
) {
107 OnAuthenticatorTaskListener listener
= mListener
.get();
110 listener
.onAuthenticatorTaskCallback(result
);
115 * Interface to retrieve data from recognition task
117 public interface OnAuthenticatorTaskListener
{
119 void onAuthenticatorTaskCallback(RemoteOperationResult result
);