Improved status messages for authentication through SAML-based federated SSO
[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 android.content.Context;
21 import android.graphics.Bitmap;
22 import android.view.View;
23 import android.webkit.CookieManager;
24 import android.webkit.WebView;
25 import android.webkit.WebViewClient;
26 import android.widget.Toast;
27
28 import com.owncloud.android.Log_OC;
29
30 /**
31 * Custom {@link WebViewClient} client aimed to catch the end of a single-sign-on process
32 * running in the {@link WebView} that is attached to.
33 *
34 * Assumes that the single-sign-on is kept thanks to a cookie set at the end of the
35 * authentication process.
36 *
37 * @author David A. Velasco
38 */
39 public class SsoWebViewClient extends WebViewClient {
40
41 private static final String TAG = SsoWebViewClient.class.getSimpleName();
42
43 private Context mContext;
44 private String mTargetUrl;
45
46 public SsoWebViewClient (Context context) {
47 mContext = context;
48 mTargetUrl = "fake://url.to.be.set";
49 }
50
51 public String getTargetUrl() {
52 return mTargetUrl;
53 }
54
55 public void setTargetUrl(String targetUrl) {
56 mTargetUrl = targetUrl;
57 }
58
59 @Override
60 public void onPageStarted (WebView view, String url, Bitmap favicon) {
61 //Log_OC.e(TAG, "onPageStarted : " + url);
62 if (url.startsWith(mTargetUrl)) {
63 view.setVisibility(View.GONE);
64 CookieManager cookieManager = CookieManager.getInstance();
65 String cookies = cookieManager.getCookie(url);
66 Toast.makeText(mContext, "got cookies: " + cookies, Toast.LENGTH_LONG).show();
67 }
68 }
69
70 @Override
71 public boolean shouldOverrideUrlLoading(WebView view, String url) {
72 //view.loadUrl(url);
73 return false;
74 }
75
76 @Override
77 public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
78 Log_OC.e(TAG, "onReceivedError : " + failingUrl);
79 }
80
81 /*
82
83 @Override
84 public void doUpdateVisitedHistory (WebView view, String url, boolean isReload) {
85 Log_OC.e(TAG, "doUpdateVisitedHistory : " + url);
86 }
87
88 @Override
89 public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
90 Log_OC.e(TAG, "onReceivedSslError : " + error);
91 }
92
93 @Override
94 public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) {
95 Log_OC.e(TAG, "onReceivedHttpAuthRequest : " + host);
96 }
97
98 @Override
99 public void onPageFinished (WebView view, String url) {
100 Log_OC.e(TAG, "onPageFinished : " + url);
101 }
102
103 @Override
104 public WebResourceResponse shouldInterceptRequest (WebView view, String url) {
105 Log_OC.e(TAG, "shouldInterceptRequest : " + url);
106 return null;
107 }
108
109 @Override
110 public void onLoadResource (WebView view, String url) {
111 Log_OC.e(TAG, "onLoadResource : " + url);
112 }
113
114 @Override
115 public void onFormResubmission (WebView view, Message dontResend, Message resend) {
116 Log_OC.e(TAG, "onFormResubMission ");
117 super.onFormResubmission(view, dontResend, resend);
118 }
119
120 @Override
121 public void onReceivedLoginRequest (WebView view, String realm, String account, String args) {
122 Log_OC.e(TAG, "onReceivedLoginRequest : " + realm + ", " + account + ", " + args);
123 }
124
125 @Override
126 public void onScaleChanged (WebView view, float oldScale, float newScale) {
127 Log_OC.e(TAG, "onScaleChanged : " + oldScale + " -> " + newScale);
128 }
129
130 @Override
131 public void onUnhandledKeyEvent (WebView view, KeyEvent event) {
132 Log_OC.e(TAG, "onUnhandledKeyEvent : " + event);
133 }
134
135 @Override
136 public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
137 Log_OC.e(TAG, "shouldOverrideKeyEvent : " + event);
138 return false;
139 }
140
141 */
142 }