From: masensio Date: Tue, 14 Jan 2014 10:06:03 +0000 (+0100) Subject: OC-2541: Adapt the code the new saml implementation. Get the username from OCS-API X-Git-Tag: oc-android-1.5.5~73^2~9 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/8f0128cbab3127d64493beb7ba8c817fabc2e1b5?ds=inline;hp=--cc OC-2541: Adapt the code the new saml implementation. Get the username from OCS-API --- 8f0128cbab3127d64493beb7ba8c817fabc2e1b5 diff --git a/oc_framework/src/com/owncloud/android/oc_framework/accounts/AccountUtils.java b/oc_framework/src/com/owncloud/android/oc_framework/accounts/AccountUtils.java index e42d1bf7..20e5b078 100644 --- a/oc_framework/src/com/owncloud/android/oc_framework/accounts/AccountUtils.java +++ b/oc_framework/src/com/owncloud/android/oc_framework/accounts/AccountUtils.java @@ -136,7 +136,7 @@ public class AccountUtils { * @return userName */ public static String getUserNameForSamlSso(String authToken) { - if (authToken != null) { + if (authToken != null) { String [] cookies = authToken.split(";"); for (int i=0; i " + url + " mTarget==> " + mTargetUrl); } - } - /* + @Override public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) { Log.d(TAG, "doUpdateVisitedHistory : " + url); @@ -132,6 +138,7 @@ public class SsoWebViewClient extends WebViewClient { @Override public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { Log.d(TAG, "onReceivedSslError : " + error); + handler.proceed(); } @Override @@ -171,5 +178,5 @@ public class SsoWebViewClient extends WebViewClient { Log.d(TAG, "shouldOverrideKeyEvent : " + event); return false; } - */ + } diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java index 5c857fb1..ada12d25 100644 --- a/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/RemoteOperationResult.java @@ -101,11 +101,13 @@ public class RemoteOperationResult implements Serializable { private String mRedirectedLocation; private ArrayList mFiles; + private String mUserName; public RemoteOperationResult(ResultCode code) { mCode = code; mSuccess = (code == ResultCode.OK || code == ResultCode.OK_SSL || code == ResultCode.OK_NO_SSL); mFiles = null; + setUserName(""); } private RemoteOperationResult(boolean success, int httpCode) { @@ -356,4 +358,12 @@ public class RemoteOperationResult implements Serializable { mRedirectedLocation.toLowerCase().contains("wayf"))); } + public String getUserName() { + return mUserName; + } + + public void setUserName(String mUserName) { + this.mUserName = mUserName; + } + } diff --git a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java index d45a4066..d8e7626b 100644 --- a/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java +++ b/oc_framework/src/com/owncloud/android/oc_framework/operations/remote/GetUserNameRemoteOperation.java @@ -21,7 +21,6 @@ import java.io.IOException; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.http.HttpStatus; import org.json.JSONException; import org.json.JSONObject; @@ -34,12 +33,11 @@ import com.owncloud.android.oc_framework.operations.RemoteOperationResult; /** - * * @author masensio * * Get the UserName for a SAML connection, from a JSON with the format: * id - * display_name + * display-name * email */ @@ -48,28 +46,35 @@ public class GetUserNameRemoteOperation extends RemoteOperation { private static final String TAG = GetUserNameRemoteOperation.class.getSimpleName(); // HEADER - private static final String TAG_HEADER = "OCS-APIREQUEST"; - private static final String TAG_HEADER_VALUE = "true"; + private static final String TAG_HEADER_OCS_API = "OCS-APIREQUEST"; + private static final String TAG_HEADER_OCS_API_VALUE = "true"; + + private static final String TAG_HEADER_CONTENT = "Content-Type"; + private static final String TAG_HEADER_CONTENT_VALUE = "application/xml"; + private static final String TAG_HEADER_COOKIE = "Cookie"; // OCS Route - private static final String TAG_OCS_ROUTE = "index.php/ocs/cloud/user?format=json"; + private static final String TAG_OCS_ROUTE ="/index.php/ocs/cloud/user?format=json"; // JSON Node names + private static final String TAG_OCS = "ocs"; + private static final String TAG_DATA = "data"; private static final String TAG_ID = "id"; - private static final String TAG_DISPLAY_NAME= "display_name"; + private static final String TAG_DISPLAY_NAME= "display-name"; private static final String TAG_EMAIL= "email"; + private String mUrl; + private String mSessionCookie; private String mUserName; - public String getUserName() { return mUserName; } - private String mUrl; - public GetUserNameRemoteOperation(String url) { + public GetUserNameRemoteOperation(String url, String sessioncookie) { mUrl = url; + mSessionCookie = sessioncookie; } @Override @@ -78,29 +83,37 @@ public class GetUserNameRemoteOperation extends RemoteOperation { int status = -1; // Get Method - GetMethod get = new GetMethod(mUrl); + GetMethod get = new GetMethod(mUrl + TAG_OCS_ROUTE); + Log.d(TAG, "URL ------> " + mUrl + TAG_OCS_ROUTE); // Add the Header - get.addRequestHeader("application/xml", "Content-Type"); - get.addRequestHeader(TAG_HEADER, TAG_HEADER_VALUE); + get.addRequestHeader(TAG_HEADER_CONTENT, TAG_HEADER_CONTENT_VALUE); + get.addRequestHeader(TAG_HEADER_OCS_API, TAG_HEADER_OCS_API_VALUE); + get.setRequestHeader(TAG_HEADER_COOKIE, mSessionCookie); //Get the user try { status = client.executeMethod(get); - if(isSuccess(status)) { + if(isSuccess(status)) { Log.d(TAG, "Obtain RESPONSE"); String response = get.getResponseBodyAsString(); - Log.d(TAG, response); + Log.d(TAG, "GET RESPONSE.................... " + response); // Parse the response JSONObject respJSON = new JSONObject(response); - String id = respJSON.getString(TAG_ID); - String display_name = respJSON.getString(TAG_DISPLAY_NAME); - String email = respJSON.getString(TAG_EMAIL); - - Log.d(TAG, "Response: " + id + "-" + display_name + "-" + email); + JSONObject respOCS = respJSON.getJSONObject(TAG_OCS); + JSONObject respData = respOCS.getJSONObject(TAG_DATA); + String id = respData.getString(TAG_ID); + String displayName = respData.getString(TAG_DISPLAY_NAME); + String email = respData.getString(TAG_EMAIL); + + // Result + result = new RemoteOperationResult(isSuccess(status), status, (get != null ? get.getResponseHeaders() : null)); + result.setUserName(displayName); + + Log.d(TAG, "Response: " + id + " - " + displayName + " - " + email); - } + } } catch (HttpException e) { result = new RemoteOperationResult(e); e.printStackTrace(); diff --git a/src/com/owncloud/android/authentication/AuthenticatorActivity.java b/src/com/owncloud/android/authentication/AuthenticatorActivity.java index 9d6d3f36..dd918e61 100644 --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@ -18,6 +18,8 @@ package com.owncloud.android.authentication; +import java.util.concurrent.ExecutionException; + import android.accounts.Account; import android.accounts.AccountManager; import android.app.AlertDialog; @@ -29,6 +31,7 @@ import android.content.SharedPreferences; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; @@ -64,6 +67,7 @@ import com.owncloud.android.oc_framework.operations.RemoteOperation; import com.owncloud.android.oc_framework.operations.RemoteOperationResult; import com.owncloud.android.oc_framework.operations.RemoteOperationResult.ResultCode; import com.owncloud.android.oc_framework.operations.remote.ExistenceCheckRemoteOperation; +import com.owncloud.android.oc_framework.operations.remote.GetUserNameRemoteOperation; import com.owncloud.android.ui.dialog.SamlWebViewDialog; import com.owncloud.android.ui.dialog.SslValidatorDialog; import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener; @@ -102,9 +106,6 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList private static final String KEY_AUTH_STATUS_TEXT = "AUTH_STATUS_TEXT"; private static final String KEY_AUTH_STATUS_ICON = "AUTH_STATUS_ICON"; private static final String KEY_REFRESH_BUTTON_ENABLED = "KEY_REFRESH_BUTTON_ENABLED"; - - // TODO Remove it - //private static final String KEY_OC_USERNAME_EQUALS = "oc_username="; private static final String AUTH_ON = "on"; private static final String AUTH_OFF = "off"; @@ -795,8 +796,9 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList } } } - - + + + private void onSamlBasedFederatedSingleSignOnAuthorizationStart(RemoteOperation operation, RemoteOperationResult result) { try { dismissDialog(DIALOG_LOGIN_PROGRESS); @@ -1178,7 +1180,11 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList mAccountMgr.setAuthToken(mAccount, mAuthTokenType, mAuthToken); } else if (AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(MainApp.getAccountType()).equals(mAuthTokenType)) { - String username = com.owncloud.android.oc_framework.accounts.AccountUtils.getUserNameForSamlSso(mAuthToken); + + String username= getUserNameForSaml(mHostBaseUrl); + if (username == null) + return false; + if (!mUsernameInput.getText().toString().equals(username)) { // fail - not a new account, but an existing one; disallow RemoteOperationResult result = new RemoteOperationResult(ResultCode.ACCOUNT_NOT_THE_SAME); @@ -1218,8 +1224,10 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList Uri uri = Uri.parse(mHostBaseUrl); String username = mUsernameInput.getText().toString().trim(); if (isSaml) { - username = com.owncloud.android.oc_framework.accounts.AccountUtils.getUserNameForSamlSso(mAuthToken); - + username = getUserNameForSaml(mHostBaseUrl); + if (username == null) + return false; + } else if (isOAuth) { username = "OAuth_user" + (new java.util.Random(System.currentTimeMillis())).nextLong(); } @@ -1280,20 +1288,6 @@ implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeList } } -// TODO Remove it -// private String getUserNameForSamlSso() { -// if (mAuthToken != null) { -// String [] cookies = mAuthToken.split(";"); -// for (int i=0; i{ + + @Override + protected String doInBackground(String... params) { + + String hostUrl = (String)params[0]; + + GetUserNameRemoteOperation getUserOperation = new GetUserNameRemoteOperation(hostUrl, mAuthToken); + WebdavClient client = OwnCloudClientFactory.createOwnCloudClient(Uri.parse(hostUrl), getApplicationContext(), true); + RemoteOperationResult result = getUserOperation.execute(client); + + return result.getUserName(); + } + + } + + /** + * Get the user name form OCS-API + * @param hostUrl + * @return username + */ + private String getUserNameForSaml(String hostUrl){ + + GetUserNameTask getUserTask = new GetUserNameTask(); + String username = null; + try { + username = getUserTask.execute(mHostBaseUrl).get(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + + return username; + } }