From: masensio Date: Tue, 13 Aug 2013 11:28:45 +0000 (+0200) Subject: Move Shibboleth WebView to a big Dialog X-Git-Tag: oc-android-1.4.6~18^2~12 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/55f2bd23e6e0464ec68f3bd6beb43ae72b30b717?ds=inline;hp=-c Move Shibboleth WebView to a big Dialog --- 55f2bd23e6e0464ec68f3bd6beb43ae72b30b717 diff --combined src/com/owncloud/android/authentication/AuthenticatorActivity.java index 852f1d52,0e8ba48c..d844011e --- a/src/com/owncloud/android/authentication/AuthenticatorActivity.java +++ b/src/com/owncloud/android/authentication/AuthenticatorActivity.java @@@ -18,8 -18,24 +18,8 @@@ package com.owncloud.android.authentication; -import com.owncloud.android.Log_OC; -import com.owncloud.android.ui.dialog.SslValidatorDialog; -import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener; -import com.owncloud.android.utils.OwnCloudVersion; -import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener; -import com.owncloud.android.network.OwnCloudClientUtils; -import com.owncloud.android.operations.OwnCloudServerCheckOperation; -import com.owncloud.android.operations.ExistenceCheckOperation; -import com.owncloud.android.operations.OAuth2GetAccessToken; -import com.owncloud.android.operations.OnRemoteOperationListener; -import com.owncloud.android.operations.RemoteOperation; -import com.owncloud.android.operations.RemoteOperationResult; -import com.owncloud.android.operations.RemoteOperationResult.ResultCode; - import android.accounts.Account; -import android.accounts.AccountAuthenticatorActivity; import android.accounts.AccountManager; -import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; @@@ -33,7 -49,6 +33,6 @@@ import android.net.Uri import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; - import android.support.v4.app.FragmentManager; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; @@@ -44,27 -59,17 +43,28 @@@ import android.view.View.OnFocusChangeL import android.view.View.OnTouchListener; import android.view.Window; import android.view.inputmethod.EditorInfo; -import android.webkit.CookieManager; -import android.webkit.WebSettings; -import android.webkit.WebView; +import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; -import android.widget.Button; import android.widget.TextView; -import android.widget.Toast; import android.widget.TextView.OnEditorActionListener; +import android.widget.Toast; +import com.owncloud.android.Log_OC; import com.owncloud.android.R; ++import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener; +import com.owncloud.android.network.OwnCloudClientUtils; +import com.owncloud.android.operations.ExistenceCheckOperation; +import com.owncloud.android.operations.OAuth2GetAccessToken; +import com.owncloud.android.operations.OnRemoteOperationListener; +import com.owncloud.android.operations.OwnCloudServerCheckOperation; +import com.owncloud.android.operations.RemoteOperation; +import com.owncloud.android.operations.RemoteOperationResult; +import com.owncloud.android.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.ui.dialog.SamlWebViewDialog; +import com.owncloud.android.ui.dialog.SslValidatorDialog; +import com.owncloud.android.ui.dialog.SslValidatorDialog.OnSslValidatorListener; +import com.owncloud.android.utils.OwnCloudVersion; import eu.alefzero.webdav.WebdavClient; @@@ -75,7 -80,7 +75,7 @@@ * @author David A. Velasco */ public class AuthenticatorActivity extends AccountAuthenticatorActivity - implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener, OnEditorActionListener { -implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener, OnEditorActionListener, SsoWebViewClientListener { ++implements OnRemoteOperationListener, OnSslValidatorListener, OnFocusChangeListener, OnEditorActionListener, SsoWebViewClientListener{ private static final String TAG = AuthenticatorActivity.class.getSimpleName(); @@@ -111,8 -116,6 +111,8 @@@ public static final byte ACTION_CREATE = 0; public static final byte ACTION_UPDATE_TOKEN = 1; + private static final String TAG_SAML_DIALOG = "samlWebViewDialog"; + private String mHostBaseUrl; private OwnCloudVersion mDiscoveredVersion; @@@ -149,7 -152,8 +149,7 @@@ private TextView mOAuthTokenEndpointText; private TextView mAccountNameInput; - private WebView mSsoWebView; - private SsoWebViewClient mWebViewClient; + private SamlWebViewDialog mSamlDialog; private View mOkButton; @@@ -176,6 -180,7 +176,6 @@@ mOAuthTokenEndpointText = (TextView)findViewById(R.id.oAuthEntryPoint_2); mOAuth2Check = (CheckBox) findViewById(R.id.oauth_onOff_check); mAccountNameInput = (EditText) findViewById(R.id.account_name); - mSsoWebView = (WebView) findViewById(R.id.web_sso_view); mOkButton = findViewById(R.id.buttonOK); mAuthStatusLayout = (TextView) findViewById(R.id.auth_status_text); @@@ -226,7 -231,6 +226,6 @@@ mHostUrlInput.setText(mHostBaseUrl); } initAuthorizationMethod(); // checks intent and setup.xml to determine mCurrentAuthorizationMethod - mOAuth2Check.setChecked(mCurrentAuthTokenType == AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN); mJustCreated = true; if (mAction == ACTION_UPDATE_TOKEN || !mHostUrlInputEnabled) { @@@ -258,6 -262,9 +257,7 @@@ mCurrentAuthTokenType = savedInstanceState.getString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE); if (mCurrentAuthTokenType == null) { mCurrentAuthTokenType = AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD; + - } else if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) { - restoreWebView(savedInstanceState); } // check if server check was interrupted by a configuration change @@@ -288,7 -295,7 +288,7 @@@ if (mServerIsChecked && !mServerIsValid && refreshButtonEnabled) showRefreshButton(); mOkButton.setEnabled(mServerIsValid); // state not automatically recovered in configuration changes - if (mCurrentAuthTokenType == AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE || + if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType) || !AUTH_OPTIONAL.equals(getString(R.string.auth_method_oauth2))) { mOAuth2Check.setVisibility(View.GONE); } @@@ -326,17 -333,42 +326,9 @@@ } }); - // FragmentManager fm = getSupportFragmentManager(); - // - // // try to find searching by tag name - // mSamlDialog = (SamlWebViewDialog) fm.findFragmentByTag(TAG_SAML_DIALOG); - // - // if (mSamlDialog != null) { - // mSamlDialog.show(fm, TAG_SAML_DIALOG); - // Log_OC.d(TAG_SAML_DIALOG, "mSamlDialog not null"); - // } } - @SuppressLint("SetJavaScriptEnabled") - private void initWebView() { - CookieManager cookieManager = CookieManager.getInstance(); - cookieManager.setAcceptCookie(true); - cookieManager.removeAllCookie(); - - mWebViewClient = new SsoWebViewClient(mHandler, this); - mSsoWebView.setWebViewClient(mWebViewClient); - WebSettings webSettings = mSsoWebView.getSettings(); - webSettings.setJavaScriptEnabled(true); - webSettings.setBuiltInZoomControls(true); - webSettings.setLoadWithOverviewMode(false); - webSettings.setSavePassword(false); - webSettings.setUserAgentString(WebdavClient.USER_AGENT); - } - - @SuppressLint("SetJavaScriptEnabled") - private void restoreWebView(Bundle savedInstanceState) { - mSsoWebView.restoreState(savedInstanceState); - - CookieManager cookieManager = CookieManager.getInstance(); - Log_OC.e(TAG, "Accept Cookie: " + cookieManager.acceptCookie()); - - mWebViewClient = new SsoWebViewClient(mHandler, this); - mSsoWebView.setWebViewClient(mWebViewClient); - mWebViewClient.setTargetUrl(mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)); - - WebSettings webSettings = mSsoWebView.getSettings(); - webSettings.setJavaScriptEnabled(true); // at least this one is not being kept by WebView#restoreState - webSettings.setBuiltInZoomControls(true); - webSettings.setLoadWithOverviewMode(false); - webSettings.setSavePassword(false); - webSettings.setUserAgentString(WebdavClient.USER_AGENT); - } ++ private void initAuthorizationMethod() { boolean oAuthRequired = false; @@@ -371,6 -403,12 +363,9 @@@ String userName = mAccount.name.substring(0, mAccount.name.lastIndexOf('@')); mUsernameInput.setText(userName); } + - if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) { - initWebView(); - } + mOAuth2Check.setChecked(AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN.equals(mCurrentAuthTokenType)); + } /** @@@ -407,10 -445,12 +402,10 @@@ outState.putParcelable(KEY_ACCOUNT, mAccount); } outState.putString(AccountAuthenticator.KEY_AUTH_TOKEN_TYPE, mCurrentAuthTokenType); - if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) { - mSsoWebView.saveState(outState); - } // refresh button enabled outState.putBoolean(KEY_REFRESH_BUTTON_ENABLED, (mRefreshButton.getVisibility() == View.VISIBLE)); + } @@@ -451,16 -491,6 +446,7 @@@ } mJustCreated = false; + - - // try to find searching by tag name - // FragmentManager fm = getSupportFragmentManager(); - // mSamlDialog = (SamlWebViewDialog) fm.findFragmentByTag(TAG_SAML_DIALOG); - // - // if (mSamlDialog != null) { - // // mSamlDialog.show(fm, TAG_SAML_DIALOG); - // mSamlDialog.setRetainInstance(true); - // } } @@@ -757,14 -787,11 +743,14 @@@ } catch (IllegalArgumentException e) { // NOTHING TO DO ; can't find out what situation that leads to the exception in this code, but user logs signal that it happens } - + if (result.isTemporalRedirection()) { String url = result.getRedirectedLocation(); - mWebViewClient.setTargetUrl(mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType)); - mSsoWebView.loadUrl(url); + String targetUrl = mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType); + + // Show dialog - mSamlDialog = SamlWebViewDialog.newInstance(mHandler, url, targetUrl); ++ mSamlDialog = SamlWebViewDialog.newInstance(url, targetUrl); + mSamlDialog.show(getSupportFragmentManager(), TAG_SAML_DIALOG); mAuthStatusIcon = android.R.drawable.ic_secure; mAuthStatusText = R.string.auth_follow_auth_server; @@@ -1411,6 -1438,7 +1397,6 @@@ mUsernameInput.setVisibility(View.GONE); mPasswordInput.setVisibility(View.GONE); mAccountNameInput.setVisibility(View.GONE); - mSsoWebView.setVisibility(View.GONE); } else if (AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE.equals(mCurrentAuthTokenType)) { // SAML-based web Single Sign On @@@ -1419,7 -1447,8 +1405,6 @@@ mUsernameInput.setVisibility(View.GONE); mPasswordInput.setVisibility(View.GONE); mAccountNameInput.setVisibility(View.VISIBLE); - mSsoWebView.setVisibility(View.VISIBLE); -- } else { // basic HTTP authorization mOAuthAuthEndpointText.setVisibility(View.GONE); @@@ -1427,6 -1456,7 +1412,6 @@@ mUsernameInput.setVisibility(View.VISIBLE); mPasswordInput.setVisibility(View.VISIBLE); mAccountNameInput.setVisibility(View.GONE); - mSsoWebView.setVisibility(View.GONE); } } @@@ -1497,22 -1527,26 +1482,41 @@@ } + public void onSamlDialogSuccess(String sessionCookie){ + mAuthToken = sessionCookie; + + if (sessionCookie != null && sessionCookie.length() > 0) { + Log_OC.d(TAG, "Successful SSO - time to save the account"); + mAuthToken = sessionCookie; + if (mAction == ACTION_CREATE) { + createAccount(); + + } else { + updateToken(); + } + + finish(); + + } + } + ++ ++ + @Override + public void onSsoFinished(String sessionCookie) { + //Toast.makeText(this, "got cookies: " + sessionCookie, Toast.LENGTH_LONG).show(); - ++ + if (sessionCookie != null && sessionCookie.length() > 0) { + Log_OC.d(TAG, "Successful SSO - time to save the account"); - mAuthToken = sessionCookie; - if (mAction == ACTION_CREATE) { - createAccount(); - - } else { - updateToken(); - } - ++ onSamlDialogSuccess(sessionCookie); + finish(); + + } else { + // TODO - show fail + Log_OC.d(TAG, "SSO failed"); + } + } ++ ++ + } diff --combined src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java index 2d0ac94a,00000000..17a1e13f mode 100644,000000..100644 --- a/src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java +++ b/src/com/owncloud/android/ui/dialog/SamlWebViewDialog.java @@@ -1,150 -1,0 +1,153 @@@ +package com.owncloud.android.ui.dialog; + +import android.annotation.SuppressLint; ++import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.os.Bundle; +import android.os.Handler; +import android.support.v4.app.DialogFragment; +import android.webkit.CookieManager; +import android.webkit.WebSettings; +import android.webkit.WebView; + +import com.owncloud.android.Log_OC; - import com.owncloud.android.authentication.AuthenticatorActivity; +import com.owncloud.android.authentication.SsoWebViewClient; +import com.owncloud.android.authentication.SsoWebViewClient.SsoWebViewClientListener; + +import eu.alefzero.webdav.WebdavClient; + +/** + * Dialog to show the WebView for SAML Authentication + * + * @author Maria Asensio + */ +public class SamlWebViewDialog extends DialogFragment - implements SsoWebViewClientListener{ ++ { + + public final String SAML_DIALOG_TAG = "SamlWebViewDialog"; + + private final static String TAG = SamlWebViewDialog.class.getSimpleName(); + + private WebView mSsoWebView; + private SsoWebViewClient mWebViewClient; + + private static String mUrl; + private static String mTargetUrl; + - private static Handler mHandler; ++ private Handler mHandler; ++ ++ private SsoWebViewClientListener mSsoWebViewClientListener; + + + /** + * Public factory method to get dialog instances. + * + * @param handler + * @param Url Url to open at WebView + * @param targetURL mHostBaseUrl + AccountUtils.getWebdavPath(mDiscoveredVersion, mCurrentAuthTokenType) + * @return New dialog instance, ready to show. + */ - public static SamlWebViewDialog newInstance(Handler handler,String url, String targetUrl) { ++ public static SamlWebViewDialog newInstance(String url, String targetUrl) { + SamlWebViewDialog fragment = new SamlWebViewDialog(); - mHandler = handler; ++ + mUrl = url; + mTargetUrl = targetUrl; + return fragment; + } + + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + // Save the state of the WebView + mSsoWebView.saveState(outState); + } + - - @Override ++ @SuppressLint("SetJavaScriptEnabled") ++ @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Log_OC.d(TAG, "On Create Dialog"); + - /// load the dialog - initWebView(savedInstanceState); - setRetainInstance(true); - /// build the dialog - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); ++ mHandler = new Handler(); ++ ++ mSsoWebView = new WebView(getActivity()) { ++ @Override ++ public boolean onCheckIsTextEditor() { ++ return true; ++ } ++ }; ++ ++ ++ mWebViewClient = new SsoWebViewClient(mHandler, mSsoWebViewClientListener); ++ mSsoWebView.setWebViewClient(mWebViewClient); ++ mWebViewClient.setTargetUrl(mTargetUrl); ++ ++ mSsoWebView.setFocusable(true); ++ mSsoWebView.setFocusableInTouchMode(true); ++ mSsoWebView.setClickable(true); ++ ++ WebSettings webSettings = mSsoWebView.getSettings(); ++ webSettings.setJavaScriptEnabled(true); ++ webSettings.setBuiltInZoomControls(true); ++ webSettings.setLoadWithOverviewMode(false); ++ webSettings.setSavePassword(false); ++ webSettings.setUserAgentString(WebdavClient.USER_AGENT); ++ ++ // load the dialog ++ if (savedInstanceState == null) { ++ initWebView(); ++ } ++ else { ++ restoreWebView(savedInstanceState); ++ } + ++ // build the dialog ++ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + Dialog dialog = builder.setView(mSsoWebView).create(); + + return dialog; + } + - + @SuppressLint("SetJavaScriptEnabled") - private void initWebView(Bundle savedInstanceState) { ++ private void initWebView() { + CookieManager cookieManager = CookieManager.getInstance(); + cookieManager.setAcceptCookie(true); - //cookieManager.removeSessionCookie(); ++ cookieManager.removeAllCookie(); + - mWebViewClient = new SsoWebViewClient(mHandler, this); - mWebViewClient.setTargetUrl(mTargetUrl); - if (savedInstanceState == null) { - - Log_OC.d(TAG, "Saved Instance State NULL"); - mSsoWebView = new WebView(getActivity()) { - @Override - public boolean onCheckIsTextEditor() { - return true; - } - }; - - mSsoWebView.setWebViewClient(mWebViewClient); - mSsoWebView.setFocusable(true); - mSsoWebView.setFocusableInTouchMode(true); - mSsoWebView.setClickable(true); - - WebSettings webSettings = mSsoWebView.getSettings(); - webSettings.setJavaScriptEnabled(true); - webSettings.setBuiltInZoomControls(true); - webSettings.setLoadWithOverviewMode(false); - webSettings.setSavePassword(false); - webSettings.setUserAgentString(WebdavClient.USER_AGENT); - - mSsoWebView.loadUrl(mUrl); - } - else { - Log_OC.d(TAG, "Saved Instance State NOT NULL"); - - mSsoWebView.restoreState(savedInstanceState); - } - ++ mSsoWebView.loadUrl(mUrl); + } + ++ @SuppressLint("SetJavaScriptEnabled") ++ private void restoreWebView(Bundle savedInstanceState) { ++ mSsoWebView.restoreState(savedInstanceState); ++ ++ CookieManager cookieManager = CookieManager.getInstance(); ++ Log_OC.e(TAG, "Accept Cookie: " + cookieManager.acceptCookie()); ++ } ++ ++ + @Override + public void onDestroyView() { + Dialog dialog = getDialog(); + Log_OC.d(TAG, "On Destroy"); + // Work around bug: http://code.google.com/p/android/issues/detail?id=17423 + if ((dialog != null) && getRetainInstance()) + getDialog().setOnDismissListener(null); + + super.onDestroyView(); + } + + + @Override - public void onSsoFinished(String sessionCookie) { - //Toast.makeText(this, "got cookies: " + sessionCookie, Toast.LENGTH_LONG).show(); - - if (sessionCookie != null && sessionCookie.length() > 0) { - Log_OC.d(TAG, "Successful SSO - time to save the account"); - ((AuthenticatorActivity) getActivity()).onSamlDialogSuccess(sessionCookie); - dismiss(); - - } else { - // TODO - show fail - Log_OC.d(TAG, "SSO failed"); ++ public void onAttach(Activity activity) { ++ super.onAttach(activity); ++ Log_OC.e(TAG, "onAttach"); ++ try { ++ mSsoWebViewClientListener = (SsoWebViewClientListener) activity; ++ } catch (ClassCastException e) { ++ throw new ClassCastException(activity.toString() + " must implement " + SsoWebViewClientListener.class.getSimpleName()); + } + } - +}