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
.lang
.ref
.WeakReference
;
22 import java
.security
.cert
.Certificate
;
23 import java
.security
.cert
.CertificateException
;
24 import java
.security
.cert
.CertificateFactory
;
25 import java
.security
.cert
.X509Certificate
;
27 import com
.owncloud
.android
.lib
.common
.network
.NetworkUtils
;
28 import com
.owncloud
.android
.utils
.Log_OC
;
30 import android
.content
.Context
;
31 import android
.graphics
.Bitmap
;
32 import android
.net
.http
.SslCertificate
;
33 import android
.net
.http
.SslError
;
34 import android
.os
.Bundle
;
35 import android
.os
.Handler
;
36 import android
.os
.Message
;
37 import android
.view
.KeyEvent
;
38 import android
.view
.View
;
39 import android
.webkit
.CookieManager
;
40 import android
.webkit
.HttpAuthHandler
;
41 import android
.webkit
.SslErrorHandler
;
42 import android
.webkit
.WebResourceResponse
;
43 import android
.webkit
.WebView
;
44 import android
.webkit
.WebViewClient
;
48 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
49 * running in the {@link WebView} that is attached to.
51 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
52 * authentication process.
54 * @author David A. Velasco
56 public class SsoWebViewClient
extends WebViewClient
{
58 private static final String TAG
= SsoWebViewClient
.class.getSimpleName();
60 public interface SsoWebViewClientListener
{
61 public void onSsoFinished(String sessionCookie
);
64 private Context mContext
;
65 private Handler mListenerHandler
;
66 private WeakReference
<SsoWebViewClientListener
> mListenerRef
;
67 private String mTargetUrl
;
68 private String mLastReloadedUrlAtError
;
70 public SsoWebViewClient (Context context
, Handler listenerHandler
, SsoWebViewClientListener listener
) {
72 mListenerHandler
= listenerHandler
;
73 mListenerRef
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
);
74 mTargetUrl
= "fake://url.to.be.set";
75 mLastReloadedUrlAtError
= null
;
78 public String
getTargetUrl() {
82 public void setTargetUrl(String targetUrl
) {
83 mTargetUrl
= targetUrl
;
87 public void onPageStarted (WebView view
, String url
, Bitmap favicon
) {
88 Log_OC
.d(TAG
, "onPageStarted : " + url
);
89 super.onPageStarted(view
, url
, favicon
);
93 public void onFormResubmission (WebView view
, Message dontResend
, Message resend
) {
94 Log_OC
.d(TAG
, "onFormResubMission ");
96 // necessary to grant reload of last page when device orientation is changed after sending a form
97 resend
.sendToTarget();
101 public boolean shouldOverrideUrlLoading(WebView view
, String url
) {
106 public void onReceivedError (WebView view
, int errorCode
, String description
, String failingUrl
) {
107 Log_OC
.e(TAG
, "onReceivedError : " + failingUrl
+ ", code " + errorCode
+ ", description: " + description
);
108 if (!failingUrl
.equals(mLastReloadedUrlAtError
)) {
110 mLastReloadedUrlAtError
= failingUrl
;
112 mLastReloadedUrlAtError
= null
;
113 super.onReceivedError(view
, errorCode
, description
, failingUrl
);
118 public void onPageFinished (WebView view
, String url
) {
119 Log_OC
.d(TAG
, "onPageFinished : " + url
);
120 mLastReloadedUrlAtError
= null
;
121 if (url
.startsWith(mTargetUrl
)) {
122 view
.setVisibility(View
.GONE
);
123 CookieManager cookieManager
= CookieManager
.getInstance();
124 final String cookies
= cookieManager
.getCookie(url
);
125 Log_OC
.d(TAG
, "Cookies: " + cookies
);
126 if (mListenerHandler
!= null
&& mListenerRef
!= null
) {
127 // this is good idea because onPageFinished is not running in the UI thread
128 mListenerHandler
.post(new Runnable() {
131 SsoWebViewClientListener listener
= mListenerRef
.get();
132 if (listener
!= null
) {
133 // Send Cookies to the listener
134 listener
.onSsoFinished(cookies
);
144 public void doUpdateVisitedHistory (WebView view
, String url
, boolean isReload
) {
145 Log_OC
.d(TAG
, "doUpdateVisitedHistory : " + url
);
149 public void onReceivedSslError (final WebView view
, final SslErrorHandler handler
, SslError error
) {
150 Log_OC
.d(TAG
, "onReceivedSslError : " + error
);
152 X509Certificate x509Certificate
= getX509CertificateFromError(error
);
153 boolean isKnowServer
= false
;
155 if (x509Certificate
!= null
) {
156 Log_OC
.d(TAG
, "------>>>>> x509Certificate " + x509Certificate
.toString());
159 isKnowServer
= NetworkUtils
.isCertInKnownServersStore((Certificate
) x509Certificate
, mContext
);
160 } catch (Exception e
) {
161 Log_OC
.e(TAG
, "Exception: " + e
.getMessage());
168 // Show a dialog with the certificate info
169 ((AuthenticatorActivity
)mContext
).showUntrustedCertDialog(x509Certificate
, error
);
170 // SslUntrustedCertDialog dialog = SslUntrustedCertDialog.newInstance(mContext, x509Certificate, error);
171 // FragmentManager fm = ((FragmentActivity)mContext).getSupportFragmentManager();
172 // FragmentTransaction ft = fm.beginTransaction();
173 // dialog.show(ft, DIALOG_UNTRUSTED_CERT);
179 * Obtain the X509Certificate from SslError
180 * @param error SslError
181 * @return X509Certificate from error
183 public X509Certificate
getX509CertificateFromError (SslError error
) {
184 Bundle bundle
= SslCertificate
.saveState(error
.getCertificate());
185 X509Certificate x509Certificate
;
186 byte[] bytes
= bundle
.getByteArray("x509-certificate");
188 x509Certificate
= null
;
191 CertificateFactory certFactory
= CertificateFactory
.getInstance("X.509");
192 Certificate cert
= certFactory
.generateCertificate(new ByteArrayInputStream(bytes
));
193 x509Certificate
= (X509Certificate
) cert
;
194 } catch (CertificateException e
) {
195 x509Certificate
= null
;
198 return x509Certificate
;
202 public void onReceivedHttpAuthRequest (WebView view
, HttpAuthHandler handler
, String host
, String realm
) {
203 Log_OC
.d(TAG
, "onReceivedHttpAuthRequest : " + host
);
207 public WebResourceResponse
shouldInterceptRequest (WebView view
, String url
) {
208 Log_OC
.d(TAG
, "shouldInterceptRequest : " + url
);
213 public void onLoadResource (WebView view
, String url
) {
214 Log_OC
.d(TAG
, "onLoadResource : " + url
);
218 public void onReceivedLoginRequest (WebView view
, String realm
, String account
, String args
) {
219 Log_OC
.d(TAG
, "onReceivedLoginRequest : " + realm
+ ", " + account
+ ", " + args
);
223 public void onScaleChanged (WebView view
, float oldScale
, float newScale
) {
224 Log_OC
.d(TAG
, "onScaleChanged : " + oldScale
+ " -> " + newScale
);
225 super.onScaleChanged(view
, oldScale
, newScale
);
229 public void onUnhandledKeyEvent (WebView view
, KeyEvent event
) {
230 Log_OC
.d(TAG
, "onUnhandledKeyEvent : " + event
);
234 public boolean shouldOverrideKeyEvent (WebView view
, KeyEvent event
) {
235 Log_OC
.d(TAG
, "shouldOverrideKeyEvent : " + event
);