1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 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/>.
18 package com
.owncloud
.android
.authentication
;
20 import java
.lang
.ref
.WeakReference
;
22 import com
.owncloud
.android
.utils
.Log_OC
;
24 import android
.graphics
.Bitmap
;
25 import android
.net
.http
.SslError
;
26 import android
.os
.Handler
;
27 import android
.os
.Message
;
28 import android
.view
.KeyEvent
;
29 import android
.view
.View
;
30 import android
.webkit
.CookieManager
;
31 import android
.webkit
.HttpAuthHandler
;
32 import android
.webkit
.SslErrorHandler
;
33 import android
.webkit
.WebResourceResponse
;
34 import android
.webkit
.WebView
;
35 import android
.webkit
.WebViewClient
;
39 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
40 * running in the {@link WebView} that is attached to.
42 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
43 * authentication process.
45 * @author David A. Velasco
47 public class SsoWebViewClient
extends WebViewClient
{
49 private static final String TAG
= SsoWebViewClient
.class.getSimpleName();
51 public interface SsoWebViewClientListener
{
52 public void onSsoFinished(String sessionCookie
);
55 private Handler mListenerHandler
;
56 private WeakReference
<SsoWebViewClientListener
> mListenerRef
;
57 private String mTargetUrl
;
58 private String mLastReloadedUrlAtError
;
60 public SsoWebViewClient (Handler listenerHandler
, SsoWebViewClientListener listener
) {
61 mListenerHandler
= listenerHandler
;
62 mListenerRef
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
);
63 mTargetUrl
= "fake://url.to.be.set";
64 mLastReloadedUrlAtError
= null
;
67 public String
getTargetUrl() {
71 public void setTargetUrl(String targetUrl
) {
72 mTargetUrl
= targetUrl
;
76 public void onPageStarted (WebView view
, String url
, Bitmap favicon
) {
77 Log_OC
.d(TAG
, "onPageStarted : " + url
);
78 super.onPageStarted(view
, url
, favicon
);
82 public void onFormResubmission (WebView view
, Message dontResend
, Message resend
) {
83 Log_OC
.d(TAG
, "onFormResubMission ");
85 // necessary to grant reload of last page when device orientation is changed after sending a form
86 resend
.sendToTarget();
90 public boolean shouldOverrideUrlLoading(WebView view
, String url
) {
95 public void onReceivedError (WebView view
, int errorCode
, String description
, String failingUrl
) {
96 Log_OC
.e(TAG
, "onReceivedError : " + failingUrl
+ ", code " + errorCode
+ ", description: " + description
);
97 if (!failingUrl
.equals(mLastReloadedUrlAtError
)) {
99 mLastReloadedUrlAtError
= failingUrl
;
101 mLastReloadedUrlAtError
= null
;
102 super.onReceivedError(view
, errorCode
, description
, failingUrl
);
107 public void onPageFinished (WebView view
, String url
) {
108 Log_OC
.d(TAG
, "onPageFinished : " + url
);
109 mLastReloadedUrlAtError
= null
;
110 if (url
.startsWith(mTargetUrl
)) {
111 view
.setVisibility(View
.GONE
);
112 CookieManager cookieManager
= CookieManager
.getInstance();
113 final String cookies
= cookieManager
.getCookie(url
);
114 Log_OC
.d(TAG
, "Cookies: " + cookies
);
115 if (mListenerHandler
!= null
&& mListenerRef
!= null
) {
116 // this is good idea because onPageFinished is not running in the UI thread
117 mListenerHandler
.post(new Runnable() {
120 SsoWebViewClientListener listener
= mListenerRef
.get();
121 if (listener
!= null
) {
122 // Send Cookies to the listener
123 listener
.onSsoFinished(cookies
);
133 public void doUpdateVisitedHistory (WebView view
, String url
, boolean isReload
) {
134 Log_OC
.d(TAG
, "doUpdateVisitedHistory : " + url
);
138 public void onReceivedSslError (WebView view
, SslErrorHandler handler
, SslError error
) {
139 Log_OC
.d(TAG
, "onReceivedSslError : " + error
);
144 public void onReceivedHttpAuthRequest (WebView view
, HttpAuthHandler handler
, String host
, String realm
) {
145 Log_OC
.d(TAG
, "onReceivedHttpAuthRequest : " + host
);
149 public WebResourceResponse
shouldInterceptRequest (WebView view
, String url
) {
150 Log_OC
.d(TAG
, "shouldInterceptRequest : " + url
);
155 public void onLoadResource (WebView view
, String url
) {
156 Log_OC
.d(TAG
, "onLoadResource : " + url
);
160 public void onReceivedLoginRequest (WebView view
, String realm
, String account
, String args
) {
161 Log_OC
.d(TAG
, "onReceivedLoginRequest : " + realm
+ ", " + account
+ ", " + args
);
165 public void onScaleChanged (WebView view
, float oldScale
, float newScale
) {
166 Log_OC
.d(TAG
, "onScaleChanged : " + oldScale
+ " -> " + newScale
);
167 super.onScaleChanged(view
, oldScale
, newScale
);
171 public void onUnhandledKeyEvent (WebView view
, KeyEvent event
) {
172 Log_OC
.d(TAG
, "onUnhandledKeyEvent : " + event
);
176 public boolean shouldOverrideKeyEvent (WebView view
, KeyEvent event
) {
177 Log_OC
.d(TAG
, "shouldOverrideKeyEvent : " + event
);