OC-3111: Check in SsoWebViewClient#onReceivedSslError() if the failed X509Certificate...
[pub/Android/ownCloud.git] / src / com / owncloud / android / authentication / SsoWebViewClient.java
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
18 package com.owncloud.android.authentication;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.lang.ref.WeakReference;
23 import java.security.KeyStore;
24 import java.security.KeyStoreException;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.cert.Certificate;
27 import java.security.cert.CertificateException;
28 import java.security.cert.CertificateFactory;
29 import java.security.cert.X509Certificate;
30
31 import com.owncloud.android.lib.common.network.NetworkUtils;
32 import com.owncloud.android.utils.Log_OC;
33
34 import android.content.Context;
35 import android.graphics.Bitmap;
36 import android.net.http.SslCertificate;
37 import android.net.http.SslError;
38 import android.os.Bundle;
39 import android.os.Handler;
40 import android.os.Message;
41 import android.view.KeyEvent;
42 import android.view.View;
43 import android.webkit.CookieManager;
44 import android.webkit.HttpAuthHandler;
45 import android.webkit.SslErrorHandler;
46 import android.webkit.WebResourceResponse;
47 import android.webkit.WebView;
48 import android.webkit.WebViewClient;
49
50
51 /**
52 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
53 * running in the {@link WebView} that is attached to.
54 *
55 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
56 * authentication process.
57 *
58 * @author David A. Velasco
59 */
60 public class SsoWebViewClient extends WebViewClient {
61
62 private static final String TAG = SsoWebViewClient.class.getSimpleName();
63
64 public interface SsoWebViewClientListener {
65 public void onSsoFinished(String sessionCookie);
66 }
67
68 private Handler mListenerHandler;
69 private WeakReference<SsoWebViewClientListener> mListenerRef;
70 private String mTargetUrl;
71 private String mLastReloadedUrlAtError;
72
73 public SsoWebViewClient (Handler listenerHandler, SsoWebViewClientListener listener) {
74 mListenerHandler = listenerHandler;
75 mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
76 mTargetUrl = "fake://url.to.be.set";
77 mLastReloadedUrlAtError = null;
78 }
79
80 public String getTargetUrl() {
81 return mTargetUrl;
82 }
83
84 public void setTargetUrl(String targetUrl) {
85 mTargetUrl = targetUrl;
86 }
87
88 @Override
89 public void onPageStarted (WebView view, String url, Bitmap favicon) {
90 Log_OC.d(TAG, "onPageStarted : " + url);
91 super.onPageStarted(view, url, favicon);
92 }
93
94 @Override
95 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
96 Log_OC.d(TAG, "onFormResubMission ");
97
98 // necessary to grant reload of last page when device orientation is changed after sending a form
99 resend.sendToTarget();
100 }
101
102 @Override
103 public boolean shouldOverrideUrlLoading(WebView view, String url) {
104 return false;
105 }
106
107 @Override
108 public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
109 Log_OC.e(TAG, "onReceivedError : " + failingUrl + ", code " + errorCode + ", description: " + description);
110 if (!failingUrl.equals(mLastReloadedUrlAtError)) {
111 view.reload();
112 mLastReloadedUrlAtError = failingUrl;
113 } else {
114 mLastReloadedUrlAtError = null;
115 super.onReceivedError(view, errorCode, description, failingUrl);
116 }
117 }
118
119 @Override
120 public void onPageFinished (WebView view, String url) {
121 Log_OC.d(TAG, "onPageFinished : " + url);
122 mLastReloadedUrlAtError = null;
123 if (url.startsWith(mTargetUrl)) {
124 view.setVisibility(View.GONE);
125 CookieManager cookieManager = CookieManager.getInstance();
126 final String cookies = cookieManager.getCookie(url);
127 Log_OC.d(TAG, "Cookies: " + cookies);
128 if (mListenerHandler != null && mListenerRef != null) {
129 // this is good idea because onPageFinished is not running in the UI thread
130 mListenerHandler.post(new Runnable() {
131 @Override
132 public void run() {
133 SsoWebViewClientListener listener = mListenerRef.get();
134 if (listener != null) {
135 // Send Cookies to the listener
136 listener.onSsoFinished(cookies);
137 }
138 }
139 });
140 }
141 }
142 }
143
144
145 @Override
146 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
147 Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
148 }
149
150 @Override
151 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
152 Log_OC.d(TAG, "onReceivedSslError : " + error);
153 // Test 1
154 X509Certificate x509Certificate = getX509CertificateFromError(error);
155
156 if (x509Certificate != null) {
157 Log_OC.d(TAG, "------>>>>> x509Certificate " + x509Certificate.toString());
158
159 }
160
161 handler.proceed();
162 }
163
164 /**
165 * Obtain the X509Certificate from SslError
166 * @param error SslError
167 * @return X509Certificate from error
168 */
169 public X509Certificate getX509CertificateFromError (SslError error) {
170 Bundle bundle = SslCertificate.saveState(error.getCertificate());
171 X509Certificate x509Certificate;
172 byte[] bytes = bundle.getByteArray("x509-certificate");
173 if (bytes == null) {
174 x509Certificate = null;
175 } else {
176 try {
177 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
178 Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
179 x509Certificate = (X509Certificate) cert;
180 } catch (CertificateException e) {
181 x509Certificate = null;
182 }
183 }
184
185 // if (x509Certificate != null) {
186 // Log_OC.d(TAG, "------>>>>> x509Certificate " + x509Certificate.toString());
187 // }
188
189 return x509Certificate;
190 }
191
192 @Override
193 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
194 Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
195 }
196
197 @Override
198 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
199 Log_OC.d(TAG, "shouldInterceptRequest : " + url);
200 return null;
201 }
202
203 @Override
204 public void onLoadResource (WebView view, String url) {
205 Log_OC.d(TAG, "onLoadResource : " + url);
206 }
207
208 @Override
209 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
210 Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
211 }
212
213 @Override
214 public void onScaleChanged (WebView view, float oldScale, float newScale) {
215 Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
216 super.onScaleChanged(view, oldScale, newScale);
217 }
218
219 @Override
220 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
221 Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
222 }
223
224 @Override
225 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
226 Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
227 return false;
228 }
229
230 }