]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/gss-lib-2.2/src/com/pras/auth/BasicAuthenticatorImpl.java
Added gss-lib-2.2.
[Dictionary.git] / jars / gss-lib-2.2 / src / com / pras / auth / BasicAuthenticatorImpl.java
1 /*\r
2  * Copyright (C) 2010 Prasanta Paul, http://prasanta-paul.blogspot.com\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.pras.auth;\r
18 \r
19 import com.pras.Log;\r
20 import com.pras.conn.HttpConHandler;\r
21 import com.pras.conn.Response;\r
22 \r
23 /**\r
24  * This will provide a basic and LOW Level way of getting Authetication Token\r
25  * for various Google's services.\r
26  * This is a generic approach for core Java. User can add Platform specific Autheticator\r
27  * implementation.\r
28  * @author Prasanta Paul\r
29  *\r
30  */\r
31 public class BasicAuthenticatorImpl implements Authenticator {\r
32 \r
33         final String TAG = "BasicAuthenticatorImpl";\r
34         // Google Authentication URL\r
35         private final String GOOGLE_CLIENT_LOGIN_URL = "https://www.google.com/accounts/ClientLogin";\r
36         Account account;\r
37         \r
38         public BasicAuthenticatorImpl(Account account){\r
39                 this.account = account;\r
40         }\r
41         \r
42         /* (non-Javadoc)\r
43          * @see com.pras.auth.Authenticator#getAuthToken(java.lang.String)\r
44          */\r
45         public String getAuthToken(String service) {\r
46                 if(account == null){\r
47                         Log.p(TAG, "No Account Info "+ account);\r
48                 }\r
49                         \r
50                 // TODO Auto-generated method stub\r
51                 String postData = "accountType="+ account.getAccountType() +"&Email="+ account.getEmail() +"&Passwd="+ account.getPassword() +"&service="+ service +"&source=test-app-log";\r
52                 \r
53                 HttpConHandler http = new HttpConHandler();\r
54                 Response res = http.doConnect(GOOGLE_CLIENT_LOGIN_URL, HttpConHandler.HTTP_POST, null, postData);\r
55                 \r
56                 if(res.isError()){\r
57                         return null;\r
58                 }\r
59                 \r
60                 String out = res.getOutput();\r
61                 /*\r
62                  * Format of success response\r
63                  * SID=<>LSID=<>Auth=<>\r
64                  */\r
65                 String[] parms = out.split("=");\r
66                 String authToken = null;\r
67                 for(int i=0; i<parms.length; i++){\r
68                         if(parms[i].toLowerCase().endsWith("auth")){\r
69                                 authToken = parms[i+1];\r
70                                 break;\r
71                         }\r
72                 }\r
73                 return authToken;\r
74         }\r
75 }\r