SsoWebViewClient.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 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. import java.lang.ref.WeakReference;
  19. import com.owncloud.android.utils.Log_OC;
  20. import android.graphics.Bitmap;
  21. import android.net.http.SslError;
  22. import android.os.Handler;
  23. import android.os.Message;
  24. import android.view.KeyEvent;
  25. import android.view.View;
  26. import android.webkit.CookieManager;
  27. import android.webkit.HttpAuthHandler;
  28. import android.webkit.SslErrorHandler;
  29. import android.webkit.WebResourceResponse;
  30. import android.webkit.WebView;
  31. import android.webkit.WebViewClient;
  32. /**
  33. * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
  34. * running in the {@link WebView} that is attached to.
  35. *
  36. * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
  37. * authentication process.
  38. *
  39. * @author David A. Velasco
  40. */
  41. public class SsoWebViewClient extends WebViewClient {
  42. private static final String TAG = SsoWebViewClient.class.getSimpleName();
  43. public interface SsoWebViewClientListener {
  44. public void onSsoFinished(String sessionCookie);
  45. }
  46. private Handler mListenerHandler;
  47. private WeakReference<SsoWebViewClientListener> mListenerRef;
  48. private String mTargetUrl;
  49. private String mLastReloadedUrlAtError;
  50. public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
  51. mListenerHandler = listenerHandler;
  52. mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
  53. mTargetUrl = "fake://url.to.be.set";
  54. mLastReloadedUrlAtError = null;
  55. }
  56. public String getTargetUrl() {
  57. return mTargetUrl;
  58. }
  59. public void setTargetUrl(String targetUrl) {
  60. mTargetUrl = targetUrl;
  61. }
  62. @Override
  63. public void onPageStarted (WebView view, String url, Bitmap favicon) {
  64. Log_OC.d(TAG, "onPageStarted : " + url);
  65. super.onPageStarted(view, url, favicon);
  66. }
  67. @Override
  68. public void onFormResubmission (WebView view, Message dontResend, Message resend) {
  69. Log_OC.d(TAG, "onFormResubMission ");
  70. // necessary to grant reload of last page when device orientation is changed after sending a form
  71. resend.sendToTarget();
  72. }
  73. @Override
  74. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  75. return false;
  76. }
  77. @Override
  78. public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
  79. Log_OC.e(TAG, "onReceivedError : " + failingUrl + ", code " + errorCode + ", description: " + description);
  80. if (!failingUrl.equals(mLastReloadedUrlAtError)) {
  81. view.reload();
  82. mLastReloadedUrlAtError = failingUrl;
  83. } else {
  84. mLastReloadedUrlAtError = null;
  85. super.onReceivedError(view, errorCode, description, failingUrl);
  86. }
  87. }
  88. @Override
  89. public void onPageFinished (WebView view, String url) {
  90. Log_OC.d(TAG, "onPageFinished : " + url);
  91. mLastReloadedUrlAtError = null;
  92. if (url.startsWith(mTargetUrl)) {
  93. view.setVisibility(View.GONE);
  94. CookieManager cookieManager = CookieManager.getInstance();
  95. final String cookies = cookieManager.getCookie(url);
  96. Log_OC.d(TAG, "Cookies: " + cookies);
  97. if (mListenerHandler != null && mListenerRef != null) {
  98. // this is good idea because onPageFinished is not running in the UI thread
  99. mListenerHandler.post(new Runnable() {
  100. @Override
  101. public void run() {
  102. SsoWebViewClientListener listener = mListenerRef.get();
  103. if (listener != null) {
  104. // Send Cookies to the listener
  105. listener.onSsoFinished(cookies);
  106. }
  107. }
  108. });
  109. }
  110. }
  111. }
  112. @Override
  113. public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
  114. Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
  115. }
  116. @Override
  117. public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
  118. Log_OC.d(TAG, "onReceivedSslError : " + error);
  119. handler.proceed();
  120. }
  121. @Override
  122. public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
  123. Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
  124. }
  125. @Override
  126. public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
  127. Log_OC.d(TAG, "shouldInterceptRequest : " + url);
  128. return null;
  129. }
  130. @Override
  131. public void onLoadResource (WebView view, String url) {
  132. Log_OC.d(TAG, "onLoadResource : " + url);
  133. }
  134. @Override
  135. public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
  136. Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
  137. }
  138. @Override
  139. public void onScaleChanged (WebView view, float oldScale, float newScale) {
  140. Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
  141. super.onScaleChanged(view, oldScale, newScale);
  142. }
  143. @Override
  144. public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
  145. Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
  146. }
  147. @Override
  148. public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
  149. Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
  150. return false;
  151. }
  152. }