Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / PithosLoginActivity.java @ 6ba04c48

History | View | Annotate | Download (9.5 kB)

1
package com.rackspace.cloud.android;
2

    
3

    
4

    
5

    
6

    
7
import java.net.URLDecoder;
8
import java.util.HashMap;
9
import java.util.Map;
10
import java.util.StringTokenizer;
11

    
12
import org.apache.http.impl.cookie.BasicClientCookie;
13

    
14
import android.app.AlertDialog;
15
import android.content.DialogInterface;
16
import android.content.Intent;
17
import android.net.http.SslError;
18
import android.os.Build;
19
import android.os.Bundle;
20
import android.text.method.PasswordTransformationMethod;
21
import android.util.Log;
22
import android.webkit.CookieManager;
23
import android.webkit.CookieSyncManager;
24
import android.webkit.HttpAuthHandler;
25
import android.webkit.SslErrorHandler;
26
import android.webkit.WebSettings.LayoutAlgorithm;
27
import android.webkit.WebView;
28
import android.webkit.WebViewClient;
29
import android.widget.EditText;
30
import android.widget.LinearLayout;
31
import android.widget.LinearLayout.LayoutParams;
32
import android.widget.TextView;
33

    
34

    
35
public class PithosLoginActivity extends CloudActivity{
36
        String loginUrl=null;
37
        String auth = null;
38
        public void onCreate(Bundle savedInstanceState) {
39
        super.onCreate(savedInstanceState);
40
        trackPageView(GoogleAnalytics.PAGE_PROVIDERS);
41
        Log.i("LOGIN","START");
42
        setContentView(R.layout.login);
43
        loginUrl = getIntent().getExtras().getString("login");
44
        auth = getIntent().getExtras().getString("auth");
45
        doLogin();
46
    
47
        
48
    } 
49
        /*
50
        protected void onSaveInstanceState(Bundle outState) {
51
                super.onSaveInstanceState(outState);
52
                //outState.putBoolean("isHidden", isHidden);
53
        }*/
54
        
55
        private void doLogin(){
56
                CookieSyncManager.createInstance(this);
57
                CookieSyncManager.getInstance().startSync();
58
                CookieSyncManager.getInstance().sync();
59
                //String loginUrl ="https://plus.pithos.grnet.gr/im/login/";//invitation?code=3219662435709009446");
60
                /*
61
                CookieManager.getInstance().setCookie(loginUrl, "_pithos2_a='';expires=Mon, 17 Oct 2010 10:47:11 UTC;");
62
                CookieManager.getInstance().removeExpiredCookie();
63
                CookieManager.getInstance().removeSessionCookie();*/
64
                CookieManager.getInstance().removeAllCookie();
65
                final WebView webview = (WebView) findViewById(R.id.browser);//new WebView(this);
66
                webview.getSettings().setJavaScriptEnabled(true);
67
                // webview.setHttpAuthUsernamePassword("vho.grnet.gr", "VHO login",
68
                // "ebs-koutsoub", "mwJBdgbtv");
69
                
70
                webview.clearCache(true);
71
                webview.clearFormData();
72
                webview.clearHistory();
73
                
74
                
75
                webview.getSettings().setBuiltInZoomControls(true);
76
                //webview.getSettings().setDefaultZoom(ZoomDensity.);
77
                if(Build.VERSION.SDK_INT!=7)
78
                        webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
79
                
80
                webview.setWebViewClient(new WebViewClient() {
81
                        
82
                        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
83
                          handler.proceed();
84
                     }
85

    
86
                          @Override public boolean shouldOverrideUrlLoading(WebView view,
87
                          String url) { 
88
                                  view.loadUrl(url); return true;
89
                          }
90
                         
91
                        @Override//?code=321 966 243 570 900 944 6 c: _pithos2_a=chstath%40ebs.gr%7CEL6igBZTXMyaemdV64OXPg%3D%3D
92

    
93
                        public void onPageFinished(WebView view, String url) {
94
                                super.onPageFinished(view, url);
95
                                
96
                                CookieSyncManager.getInstance().sync();
97
                                
98
                                String cookie = CookieManager.getInstance().getCookie(url);
99
                                if(cookie!=null)
100
                                Log.i("LOGIN COOKIE",url+" c: "+cookie +" "+cookie.contains("_pithos2_a"));
101
                                /*if(url.indexOf('?')!=-1){
102
                                        String q = url.substring(url.indexOf('?'),url.length());
103
                                        Log.i("LOGIN COOKIE",url+" c: "+q);
104
                                        Map<String,String> params = getQueryMap(q);
105
                                        String token = params.get("token");
106
                                        String user = params.get("user");
107
                                        if(token!=null&&user!=null)
108
                                                exit(user,token);
109
                                        
110
                                }*/
111
                                if(null!=cookie && cookie.contains("_pithos2_a")){
112
                                        StringTokenizer str1 = new StringTokenizer(cookie,";");
113
                                        while(str1.hasMoreTokens()){
114
                                                String cc = str1.nextToken();
115
                                                Log.i("LOGIN","token:"+cc);
116
                                                if(cc.trim().startsWith("_pithos2_a")){
117
                                                        Log.i("LOGIN COOKIE INSIDE",url+" c: "+cc);
118
                                                        try {
119
                                                                String value = URLDecoder.decode(parseRawCookie(cc).getValue());
120
                                                                StringTokenizer str = new StringTokenizer(value,"|");
121
                                                                if(str.countTokens()==2){
122
                                                                        String u = str.nextToken();
123
                                                                        String t = str.nextToken();
124
                                                                        Log.i("value",value);
125
                                                                        Log.i("s1",u);
126
                                                                        Log.i("s2",t);
127
                                                                        exit(u,t);
128
                                                                        return;
129
                                                                }
130
                                                                
131
                                                                //exit(s[0], s[1]);
132
                                                        } catch (Exception e) {
133
                                                                // TODO Auto-generated catch block
134
                                                                e.printStackTrace();
135
                                                        }
136
                                                }
137
                                        }
138
                                        
139
                                }
140
                        }
141

    
142
                        @Override
143
                        public void onReceivedHttpAuthRequest(WebView view,
144
                                        final HttpAuthHandler handler, String host, String realm) {
145
                                String[] up = view.getHttpAuthUsernamePassword(host, realm);
146
                                if (up != null && up.length == 2) {
147
                                        handler.proceed(up[0], up[1]);
148
                                        
149
                                } else {
150
                                                final AlertDialog.Builder alert = new AlertDialog.Builder(PithosLoginActivity.this);
151
                                        alert.setTitle("Pithos Login");
152
                                        //View lv = findViewById(R.layout.dialoglogin);
153
                                        //alert.setView(lv);
154
                                            final EditText input = new EditText(PithosLoginActivity.this);//(EditText) lv.findViewById(R.id.username); 
155
                                            final EditText pass = new EditText(PithosLoginActivity.this);//(EditText) lv.findViewById(R.id.password);
156
                                            pass.setTransformationMethod(new PasswordTransformationMethod());
157
                                            LinearLayout layout = new LinearLayout(PithosLoginActivity.this);
158
                                            layout.setOrientation(LinearLayout.VERTICAL);
159
                                            LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
160
                                            layout.setLayoutParams(params);
161
                                            input.setLayoutParams(params);
162
                                            pass.setLayoutParams(params);
163
                                            TextView username = new TextView(PithosLoginActivity.this);
164
                                            username.setText("Username:");
165
                                            
166
                                            TextView password = new TextView(PithosLoginActivity.this);
167
                                            password.setText("Password:");
168
                                            layout.addView(username);
169
                                            layout.addView(input);
170
                                            layout.addView(password);
171
                                            layout.addView(pass);
172
                                            alert.setView(layout);
173
                                            
174
                                            alert.setPositiveButton("Login", new DialogInterface.OnClickListener() {
175
                                                    public void onClick(DialogInterface dialog, int whichButton) {
176
                                                            String uvalue = input.getText().toString().trim();
177
                                                            String pvalue = pass.getText().toString().trim();
178
                                                            handler.proceed(uvalue, pvalue);                                                            
179
                                                    }
180
                                            });
181

    
182
                                            alert.setNegativeButton("Cancel",
183
                                                            new DialogInterface.OnClickListener() {
184
                                                                    public void onClick(DialogInterface dialog, int whichButton) {
185
                                                                            dialog.cancel();
186
                                                                    }
187
                                                            });
188
                                            alert.show();
189
                                        }
190
                                }
191

    
192
                        
193
                });
194
                Log.i("LOGIN","LOADING:"+loginUrl);
195
                //setContentView(webview);
196
                webview.clearSslPreferences();
197
                //webview.loadUrl("https://pithos.dev.grnet.gr/im/login/invitation?code=6879429392467020041");
198
                
199
                
200
                webview.loadUrl(loginUrl);//invitation?code=3219662435709009446");
201
                //webview.loadUrl("http://www.google.gr");
202
        }
203
        
204
        public static Map<String, String> getQueryMap(String query) {
205
                String[] params = query.split("&");
206
                Map<String, String> map = new HashMap<String, String>();
207
                for (String param : params) {
208
                        String name = param.split("=")[0];
209
                        String value = param.split("=")[1];
210
                        if(param.endsWith("=="))
211
                                value=value+"==";
212
                        else if(param.endsWith("="))
213
                                value=value+"=";
214
                        
215
                        Log.d("DD",name +" "+value);
216
                        map.put(name, value);
217
                }
218
                return map;
219
        }
220
        void exit(String username,String token){
221
                Log.i("LOGINA",username+" "+token);
222
                Intent result = new Intent();
223
                Bundle b = new Bundle();
224
                b.putString("username", username);
225
                b.putString("apiKey", token);
226
                b.putString("server", auth);
227
                result.putExtra("accountInfo", b);
228
                setResult(RESULT_OK, result);
229
                finish();
230
        }
231
        
232
        
233
        BasicClientCookie parseRawCookie(String rawCookie) throws Exception {
234
            String[] rawCookieParams = rawCookie.split(";");
235

    
236
            String[] rawCookieNameAndValue = rawCookieParams[0].split("=");
237
            if (rawCookieNameAndValue.length != 2) {
238
                throw new Exception("Invalid cookie: missing name and value.");
239
            }
240

    
241
            String cookieName = rawCookieNameAndValue[0].trim();
242
            String cookieValue = rawCookieNameAndValue[1].trim();
243
            BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
244
            for (int i = 1; i < rawCookieParams.length; i++) {
245
                String rawCookieParamNameAndValue[] = rawCookieParams[i].trim().split("=");
246

    
247
                String paramName = rawCookieParamNameAndValue[0].trim();
248

    
249
                if (paramName.equalsIgnoreCase("secure")) {
250
                    cookie.setSecure(true);
251
                } else {
252
                    if (rawCookieParamNameAndValue.length != 2) {
253
                        throw new Exception("Invalid cookie: attribute not a flag or missing value.");
254
                    }
255

    
256
                    String paramValue = rawCookieParamNameAndValue[1].trim();
257

    
258
                    
259
                }
260
            }
261

    
262
            return cookie;
263
        }
264
        
265
        private String lastHtml;
266
        
267
        public void setLastHtml(String lastHtml) {
268
                this.lastHtml = lastHtml;
269
        }
270
        
271
        public String getLastHtml() {
272
                return lastHtml;
273
        }
274
          class MyJavaScriptInterface {
275
          
276
          public void showHTML(String html) { 
277
                  setLastHtml(html);
278
          
279
          } }
280
          
281
          private String getHtmlValue(String substring, String html){ String
282
          result=null; boolean found = false; found = html.contains(substring); if
283
          (found) { int indx = html.indexOf(substring); StringBuffer res = new
284
          StringBuffer(); for (int i = indx + substring.length(); i < html
285
          .length(); i++) { char c = html.charAt(i); if (c == '<') break;
286
          res.append(c); }
287
          
288
          result = res.toString(); result = result.trim();
289
          
290
          } return result; }
291
         
292
}