2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 package com
.owncloud
.android
.authentication
;
23 import java
.io
.ByteArrayInputStream
;
24 import java
.lang
.ref
.WeakReference
;
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
.lib
.common
.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 public class SsoWebViewClient
extends WebViewClient
{
59 private static final String TAG
= SsoWebViewClient
.class.getSimpleName();
61 public interface SsoWebViewClientListener
{
62 public void onSsoFinished(String sessionCookie
);
65 private Context mContext
;
66 private Handler mListenerHandler
;
67 private WeakReference
<SsoWebViewClientListener
> mListenerRef
;
68 private String mTargetUrl
;
69 private String mLastReloadedUrlAtError
;
72 public SsoWebViewClient (Context context
, Handler listenerHandler
, SsoWebViewClientListener listener
) {
74 mListenerHandler
= listenerHandler
;
75 mListenerRef
= new WeakReference
<SsoWebViewClient
.SsoWebViewClientListener
>(listener
);
76 mTargetUrl
= "fake://url.to.be.set";
77 mLastReloadedUrlAtError
= null
;
80 public String
getTargetUrl() {
84 public void setTargetUrl(String targetUrl
) {
85 mTargetUrl
= targetUrl
;
89 public void onPageStarted (WebView view
, String url
, Bitmap favicon
) {
90 Log_OC
.d(TAG
, "onPageStarted : " + url
);
91 view
.clearCache(true
);
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
);
146 public void onReceivedSslError (final WebView view
, final SslErrorHandler handler
, SslError error
) {
147 Log_OC
.e(TAG
, "onReceivedSslError : " + error
);
149 X509Certificate x509Certificate
= getX509CertificateFromError(error
);
150 boolean isKnownServer
= false
;
152 if (x509Certificate
!= null
) {
154 isKnownServer
= NetworkUtils
.isCertInKnownServersStore((Certificate
) x509Certificate
, mContext
);
155 } catch (Exception e
) {
156 Log_OC
.e(TAG
, "Exception: " + e
.getMessage());
163 ((AuthenticatorActivity
)mContext
).showUntrustedCertDialog(x509Certificate
, error
, handler
);
168 * Obtain the X509Certificate from SslError
169 * @param error SslError
170 * @return X509Certificate from error
172 public X509Certificate
getX509CertificateFromError (SslError error
) {
173 Bundle bundle
= SslCertificate
.saveState(error
.getCertificate());
174 X509Certificate x509Certificate
;
175 byte[] bytes
= bundle
.getByteArray("x509-certificate");
177 x509Certificate
= null
;
180 CertificateFactory certFactory
= CertificateFactory
.getInstance("X.509");
181 Certificate cert
= certFactory
.generateCertificate(new ByteArrayInputStream(bytes
));
182 x509Certificate
= (X509Certificate
) cert
;
183 } catch (CertificateException e
) {
184 x509Certificate
= null
;
187 return x509Certificate
;
191 public void onReceivedHttpAuthRequest (WebView view
, HttpAuthHandler handler
, String host
, String realm
) {
192 Log_OC
.d(TAG
, "onReceivedHttpAuthRequest : " + host
);
194 ((AuthenticatorActivity
)mContext
).createAuthenticationDialog(view
, handler
);