aa193143499d805b0221a4116b9d677fa7ace978
[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.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;
29
30 import com.owncloud.android.lib.common.network.NetworkUtils;
31 import com.owncloud.android.utils.Log_OC;
32
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;
48
49
50 /**
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.
53 *
54 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
55 * authentication process.
56 *
57 * @author David A. Velasco
58 */
59 public class SsoWebViewClient extends WebViewClient {
60
61 private static final String TAG = SsoWebViewClient.class.getSimpleName();
62
63 public interface SsoWebViewClientListener {
64 public void onSsoFinished(String sessionCookie);
65 }
66
67 private Context mContext;
68 private Handler mListenerHandler;
69 private WeakReference<SsoWebViewClientListener> mListenerRef;
70 private String mTargetUrl;
71 private String mLastReloadedUrlAtError;
72
73 public SsoWebViewClient (Context context, Handler listenerHandler, SsoWebViewClientListener listener) {
74 mContext = context;
75 mListenerHandler = listenerHandler;
76 mListenerRef = new WeakReference<SsoWebViewClient.SsoWebViewClientListener>(listener);
77 mTargetUrl = "fake://url.to.be.set";
78 mLastReloadedUrlAtError = null;
79 }
80
81 public String getTargetUrl() {
82 return mTargetUrl;
83 }
84
85 public void setTargetUrl(String targetUrl) {
86 mTargetUrl = targetUrl;
87 }
88
89 @Override
90 public void onPageStarted (WebView view, String url, Bitmap favicon) {
91 Log_OC.d(TAG, "onPageStarted : " + url);
92 super.onPageStarted(view, url, favicon);
93 }
94
95 @Override
96 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
97 Log_OC.d(TAG, "onFormResubMission ");
98
99 // necessary to grant reload of last page when device orientation is changed after sending a form
100 resend.sendToTarget();
101 }
102
103 @Override
104 public boolean shouldOverrideUrlLoading(WebView view, String url) {
105 return false;
106 }
107
108 @Override
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)) {
112 view.reload();
113 mLastReloadedUrlAtError = failingUrl;
114 } else {
115 mLastReloadedUrlAtError = null;
116 super.onReceivedError(view, errorCode, description, failingUrl);
117 }
118 }
119
120 @Override
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() {
132 @Override
133 public void run() {
134 SsoWebViewClientListener listener = mListenerRef.get();
135 if (listener != null) {
136 // Send Cookies to the listener
137 listener.onSsoFinished(cookies);
138 }
139 }
140 });
141 }
142 }
143 }
144
145
146 @Override
147 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
148 Log_OC.d(TAG, "doUpdateVisitedHistory : " + url);
149 }
150
151 @Override
152 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
153 Log_OC.d(TAG, "onReceivedSslError : " + error);
154 // Test 1
155 X509Certificate x509Certificate = getX509CertificateFromError(error);
156 boolean isKnowServer = false;
157
158 if (x509Certificate != null) {
159 Log_OC.d(TAG, "------>>>>> x509Certificate " + x509Certificate.toString());
160
161 try {
162 isKnowServer = NetworkUtils.isCertInKnownServersStore((Certificate) x509Certificate, mContext);
163 } catch (KeyStoreException e) {
164 // TODO Auto-generated catch block
165 e.printStackTrace();
166 } catch (NoSuchAlgorithmException e) {
167 // TODO Auto-generated catch block
168 e.printStackTrace();
169 } catch (CertificateException e) {
170 // TODO Auto-generated catch block
171 e.printStackTrace();
172 } catch (IOException e) {
173 // TODO Auto-generated catch block
174 e.printStackTrace();
175 }
176 }
177 if (isKnowServer) {
178 handler.proceed();
179 } else {
180
181 }
182 }
183
184 /**
185 * Obtain the X509Certificate from SslError
186 * @param error SslError
187 * @return X509Certificate from error
188 */
189 public X509Certificate getX509CertificateFromError (SslError error) {
190 Bundle bundle = SslCertificate.saveState(error.getCertificate());
191 X509Certificate x509Certificate;
192 byte[] bytes = bundle.getByteArray("x509-certificate");
193 if (bytes == null) {
194 x509Certificate = null;
195 } else {
196 try {
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;
202 }
203 }
204
205 // if (x509Certificate != null) {
206 // Log_OC.d(TAG, "------>>>>> x509Certificate " + x509Certificate.toString());
207 // }
208
209 return x509Certificate;
210 }
211
212 @Override
213 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
214 Log_OC.d(TAG, "onReceivedHttpAuthRequest : " + host);
215 }
216
217 @Override
218 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
219 Log_OC.d(TAG, "shouldInterceptRequest : " + url);
220 return null;
221 }
222
223 @Override
224 public void onLoadResource (WebView view, String url) {
225 Log_OC.d(TAG, "onLoadResource : " + url);
226 }
227
228 @Override
229 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
230 Log_OC.d(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
231 }
232
233 @Override
234 public void onScaleChanged (WebView view, float oldScale, float newScale) {
235 Log_OC.d(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
236 super.onScaleChanged(view, oldScale, newScale);
237 }
238
239 @Override
240 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
241 Log_OC.d(TAG, "onUnhandledKeyEvent : " + event);
242 }
243
244 @Override
245 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
246 Log_OC.d(TAG, "shouldOverrideKeyEvent : " + event);
247 return false;
248 }
249
250 }