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
.io
.ByteArrayInputStream
;
21 import java
.io
.IOException
;
22 import java
.lang
.ref
.WeakReference
;
23 import java
.security
.KeyStoreException
;
24 import java
.security
.NoSuchAlgorithmException
;
25 import java
.security
.cert
.Certificate
;
26 import java
.security
.cert
.CertificateException
;
27 import java
.security
.cert
.CertificateFactory
;
28 import java
.security
.cert
.X509Certificate
;
30 import com
.owncloud
.android
.lib
.common
.network
.NetworkUtils
;
31 import com
.owncloud
.android
.utils
.Log_OC
;
33 import android
.content
.Context
;
34 import android
.graphics
.Bitmap
;
35 import android
.net
.http
.SslCertificate
;
36 import android
.net
.http
.SslError
;
37 import android
.os
.Bundle
;
38 import android
.os
.Handler
;
39 import android
.os
.Message
;
40 import android
.view
.KeyEvent
;
41 import android
.view
.View
;
42 import android
.webkit
.CookieManager
;
43 import android
.webkit
.HttpAuthHandler
;
44 import android
.webkit
.SslErrorHandler
;
45 import android
.webkit
.WebResourceResponse
;
46 import android
.webkit
.WebView
;
47 import android
.webkit
.WebViewClient
;
51 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
52 * running in the {@link WebView} that is attached to.
54 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
55 * authentication process.
57 * @author David A. Velasco
59 public class SsoWebViewClient
extends WebViewClient
{
61 private static final String TAG
= SsoWebViewClient
.class.getSimpleName();
63 public interface SsoWebViewClientListener
{
64 public void onSsoFinished(String sessionCookie
);
67 private Context mContext
;
68 private Handler mListenerHandler
;
69 private WeakReference
<SsoWebViewClientListener
> mListenerRef
;
70 private String mTargetUrl
;
71 private String mLastReloadedUrlAtError
;
73 public SsoWebViewClient (Context context
, Handler listenerHandler
, SsoWebViewClientListener listener
) {
75 mListenerHandler
= listenerHandler
;
76 mListenerRef
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
);
77 mTargetUrl
= "fake://url.to.be.set";
78 mLastReloadedUrlAtError
= null
;
81 public String
getTargetUrl() {
85 public void setTargetUrl(String targetUrl
) {
86 mTargetUrl
= targetUrl
;
90 public void onPageStarted (WebView view
, String url
, Bitmap favicon
) {
91 Log_OC
.d(TAG
, "onPageStarted : " + url
);
92 super.onPageStarted(view
, url
, favicon
);
96 public void onFormResubmission (WebView view
, Message dontResend
, Message resend
) {
97 Log_OC
.d(TAG
, "onFormResubMission ");
99 // necessary to grant reload of last page when device orientation is changed after sending a form
100 resend
.sendToTarget();
104 public boolean shouldOverrideUrlLoading(WebView view
, String url
) {
109 public void onReceivedError (WebView view
, int errorCode
, String description
, String failingUrl
) {
110 Log_OC
.e(TAG
, "onReceivedError : " + failingUrl
+ ", code " + errorCode
+ ", description: " + description
);
111 if (!failingUrl
.equals(mLastReloadedUrlAtError
)) {
113 mLastReloadedUrlAtError
= failingUrl
;
115 mLastReloadedUrlAtError
= null
;
116 super.onReceivedError(view
, errorCode
, description
, failingUrl
);
121 public void onPageFinished (WebView view
, String url
) {
122 Log_OC
.d(TAG
, "onPageFinished : " + url
);
123 mLastReloadedUrlAtError
= null
;
124 if (url
.startsWith(mTargetUrl
)) {
125 view
.setVisibility(View
.GONE
);
126 CookieManager cookieManager
= CookieManager
.getInstance();
127 final String cookies
= cookieManager
.getCookie(url
);
128 Log_OC
.d(TAG
, "Cookies: " + cookies
);
129 if (mListenerHandler
!= null
&& mListenerRef
!= null
) {
130 // this is good idea because onPageFinished is not running in the UI thread
131 mListenerHandler
.post(new Runnable() {
134 SsoWebViewClientListener listener
= mListenerRef
.get();
135 if (listener
!= null
) {
136 // Send Cookies to the listener
137 listener
.onSsoFinished(cookies
);
147 public void doUpdateVisitedHistory (WebView view
, String url
, boolean isReload
) {
148 Log_OC
.d(TAG
, "doUpdateVisitedHistory : " + url
);
152 public void onReceivedSslError (WebView view
, SslErrorHandler handler
, SslError error
) {
153 Log_OC
.d(TAG
, "onReceivedSslError : " + error
);
155 X509Certificate x509Certificate
= getX509CertificateFromError(error
);
156 boolean isKnowServer
= false
;
158 if (x509Certificate
!= null
) {
159 Log_OC
.d(TAG
, "------>>>>> x509Certificate " + x509Certificate
.toString());
162 isKnowServer
= NetworkUtils
.isCertInKnownServersStore((Certificate
) x509Certificate
, mContext
);
163 } catch (KeyStoreException e
) {
164 // TODO Auto-generated catch block
166 } catch (NoSuchAlgorithmException e
) {
167 // TODO Auto-generated catch block
169 } catch (CertificateException e
) {
170 // TODO Auto-generated catch block
172 } catch (IOException e
) {
173 // TODO Auto-generated catch block
185 * Obtain the X509Certificate from SslError
186 * @param error SslError
187 * @return X509Certificate from error
189 public X509Certificate
getX509CertificateFromError (SslError error
) {
190 Bundle bundle
= SslCertificate
.saveState(error
.getCertificate());
191 X509Certificate x509Certificate
;
192 byte[] bytes
= bundle
.getByteArray("x509-certificate");
194 x509Certificate
= null
;
197 CertificateFactory certFactory
= CertificateFactory
.getInstance("X.509");
198 Certificate cert
= certFactory
.generateCertificate(new ByteArrayInputStream(bytes
));
199 x509Certificate
= (X509Certificate
) cert
;
200 } catch (CertificateException e
) {
201 x509Certificate
= null
;
205 // if (x509Certificate != null) {
206 // Log_OC.d(TAG, "------>>>>> x509Certificate " + x509Certificate.toString());
209 return x509Certificate
;
213 public void onReceivedHttpAuthRequest (WebView view
, HttpAuthHandler handler
, String host
, String realm
) {
214 Log_OC
.d(TAG
, "onReceivedHttpAuthRequest : " + host
);
218 public WebResourceResponse
shouldInterceptRequest (WebView view
, String url
) {
219 Log_OC
.d(TAG
, "shouldInterceptRequest : " + url
);
224 public void onLoadResource (WebView view
, String url
) {
225 Log_OC
.d(TAG
, "onLoadResource : " + url
);
229 public void onReceivedLoginRequest (WebView view
, String realm
, String account
, String args
) {
230 Log_OC
.d(TAG
, "onReceivedLoginRequest : " + realm
+ ", " + account
+ ", " + args
);
234 public void onScaleChanged (WebView view
, float oldScale
, float newScale
) {
235 Log_OC
.d(TAG
, "onScaleChanged : " + oldScale
+ " -> " + newScale
);
236 super.onScaleChanged(view
, oldScale
, newScale
);
240 public void onUnhandledKeyEvent (WebView view
, KeyEvent event
) {
241 Log_OC
.d(TAG
, "onUnhandledKeyEvent : " + event
);
245 public boolean shouldOverrideKeyEvent (WebView view
, KeyEvent event
) {
246 Log_OC
.d(TAG
, "shouldOverrideKeyEvent : " + event
);