root / src / com / rackspace / cloud / servers / api / client / http / Authentication.java @ 9c74202f
History | View | Annotate | Download (6.6 kB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package com.rackspace.cloud.servers.api.client.http; |
5 |
|
6 |
import java.io.IOException; |
7 |
import java.io.StringReader; |
8 |
import java.io.UnsupportedEncodingException; |
9 |
|
10 |
import javax.xml.parsers.FactoryConfigurationError; |
11 |
import javax.xml.parsers.ParserConfigurationException; |
12 |
import javax.xml.parsers.SAXParser; |
13 |
import javax.xml.parsers.SAXParserFactory; |
14 |
import java.net.URLEncoder; |
15 |
import org.apache.http.HttpResponse; |
16 |
import org.apache.http.client.ClientProtocolException; |
17 |
import org.apache.http.client.methods.HttpGet; |
18 |
import org.apache.http.client.methods.HttpPost; |
19 |
import org.apache.http.entity.StringEntity; |
20 |
import org.apache.http.impl.client.BasicResponseHandler; |
21 |
import org.apache.http.protocol.RequestExpectContinue; |
22 |
import org.xml.sax.InputSource; |
23 |
import org.xml.sax.SAXException; |
24 |
import org.xml.sax.XMLReader; |
25 |
|
26 |
import android.content.Context; |
27 |
import android.util.Log; |
28 |
|
29 |
import com.rackspace.cloud.android.Preferences; |
30 |
import com.rackspace.cloud.files.api.client.CustomHttpClient; |
31 |
import com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser; |
32 |
import com.rackspace.cloud.servers.api.client.Account; |
33 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
34 |
|
35 |
/**
|
36 |
* @author Mike Mayo - mike.mayo@rackspace.com - twitter.com/greenisus
|
37 |
*
|
38 |
*/
|
39 |
public class Authentication { |
40 |
|
41 |
public static boolean authenticate(Context context) throws CloudServersException{ |
42 |
//if(Account.getAccount().getApiKey() != null){
|
43 |
return authenticateV1(context);
|
44 |
//} else {
|
45 |
// return authenticateV2(context);
|
46 |
//}
|
47 |
} |
48 |
|
49 |
public static boolean authenticateV1(Context context) { |
50 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
51 |
String authServer = Account.getAccount().getAuthServer();
|
52 |
if(authServer==null) |
53 |
authServer = Account.getAccount().getAuthServerV2(); |
54 |
HttpGet get = new HttpGet(authServer);
|
55 |
get.addHeader("X-Auth-User", Account.getAccount().getUsername());
|
56 |
get.addHeader("X-Auth-Key", "lMvqjHxChbQY+LgDqwWpYg==");// Account.getAccount().getApiKey()); |
57 |
Log.i("AUTH STARTED", authServer);
|
58 |
try {
|
59 |
HttpResponse resp = httpclient.execute(get); |
60 |
Log.i("AUTH STARTED", "AUTH STARTED"+resp.getStatusLine().getStatusCode()); |
61 |
if (resp.getStatusLine().getStatusCode() == 204) { |
62 |
|
63 |
Account.getAccount().setAuthToken( |
64 |
getHeaderValue(resp, "X-Auth-Token"));
|
65 |
Account.getAccount().setServerUrl( |
66 |
getHeaderValue(resp, "X-Server-Management-Url"));
|
67 |
Account.getAccount().setStorageUrl( |
68 |
getHeaderValue(resp, "X-Storage-Url"));
|
69 |
Account.getAccount().setStorageToken( |
70 |
getHeaderValue(resp, "X-Storage-Token"));
|
71 |
if (Account.getAccount().getStorageToken() == null) |
72 |
Account.getAccount().setStorageToken( |
73 |
Account.getAccount().getAuthToken()); |
74 |
Account.getAccount().setCdnManagementUrl( |
75 |
getHeaderValue(resp, "X-Cdn-Management-Url"));
|
76 |
Account.getAccount().setLoadBalancerRegions(new String[0]); |
77 |
return true; |
78 |
} else {
|
79 |
Log.d("status code",
|
80 |
Integer.toString(resp.getStatusLine().getStatusCode()));
|
81 |
return false; |
82 |
} |
83 |
} catch (ClientProtocolException cpe) {
|
84 |
Log.e("err","",cpe); |
85 |
return false; |
86 |
} catch (IOException e) { |
87 |
Log.v("info", e.toString());
|
88 |
return false; |
89 |
} |
90 |
} |
91 |
|
92 |
public static boolean authenticateV2(Context context) throws CloudServersException { |
93 |
|
94 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
95 |
HttpResponse resp = null;
|
96 |
Log.d("info", "authing with " + Account.getAccount().getAuthServerV2() + "/tokens.xml"); |
97 |
HttpPost post = new HttpPost(Account.getAccount().getAuthServerV2() + "/tokens.xml"); |
98 |
|
99 |
post.addHeader("Content-Type", "application/json"); |
100 |
|
101 |
String username = Account.getAccount().getUsername().trim();
|
102 |
String password = Account.getAccount().getPassword().trim();
|
103 |
String requestBody =
|
104 |
"{" +
|
105 |
"\"auth\": { " +
|
106 |
"\"passwordCredentials\": { " +
|
107 |
"\"username\": " + "\"" + username + "\"" + |
108 |
"\"password\": " + "\"" + password + "\"" + |
109 |
"}" +
|
110 |
"}" +
|
111 |
"}";
|
112 |
Log.d("info", requestBody);
|
113 |
StringEntity tmp = null;
|
114 |
try {
|
115 |
tmp = new StringEntity(requestBody);
|
116 |
} catch (UnsupportedEncodingException e) { |
117 |
CloudServersException cse = new CloudServersException();
|
118 |
cse.setMessage(e.getLocalizedMessage()); |
119 |
throw cse;
|
120 |
} |
121 |
post.setEntity(tmp); |
122 |
|
123 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
124 |
|
125 |
HttpBundle bundle = new HttpBundle();
|
126 |
bundle.setCurlRequest(post); |
127 |
|
128 |
try {
|
129 |
resp = httpclient.execute(post); |
130 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
131 |
String body = responseHandler.handleResponse(resp);
|
132 |
bundle.setHttpResponse(resp); |
133 |
|
134 |
if(resp.getStatusLine().getStatusCode() == 200){ |
135 |
Log.d("info", "login success"); |
136 |
AuthXMLParser authXMLParser = new AuthXMLParser();
|
137 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
138 |
XMLReader xmlReader = saxParser.getXMLReader(); |
139 |
xmlReader.setContentHandler(authXMLParser); |
140 |
|
141 |
xmlReader.parse(new InputSource(new StringReader(body))); |
142 |
|
143 |
Account.getAccount().setAuthToken(authXMLParser.getToken()); |
144 |
Account.getAccount().setServerUrl(authXMLParser.getServerURL()); |
145 |
Account.getAccount().setStorageUrl(authXMLParser.getStorageURL()); |
146 |
Account.getAccount().setStorageToken(authXMLParser.getToken()); |
147 |
Account.getAccount().setCdnManagementUrl(authXMLParser.getCdnURL()); |
148 |
|
149 |
//Set the available regions for the account
|
150 |
if(Account.getAccount().getAuthServerV2().equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)){ |
151 |
Account.getAccount().setLoadBalancerRegions(Preferences.UK_REGIONS);
|
152 |
} else if(Account.getAccount().getAuthServerV2().equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)){ |
153 |
Account.getAccount().setLoadBalancerRegions(Preferences.US_REGIONS);
|
154 |
} |
155 |
|
156 |
return true; |
157 |
} else {
|
158 |
Log.d("info", "login failed"); |
159 |
return false; |
160 |
} |
161 |
} catch (ClientProtocolException e) {
|
162 |
CloudServersException cse = new CloudServersException();
|
163 |
cse.setMessage(e.getLocalizedMessage()); |
164 |
throw cse;
|
165 |
} catch (IOException e) { |
166 |
CloudServersException cse = new CloudServersException();
|
167 |
cse.setMessage(e.getLocalizedMessage()); |
168 |
throw cse;
|
169 |
} catch (FactoryConfigurationError e) { |
170 |
CloudServersException cse = new CloudServersException();
|
171 |
cse.setMessage(e.getLocalizedMessage()); |
172 |
throw cse;
|
173 |
} catch (ParserConfigurationException e) { |
174 |
// TODO Auto-generated catch block
|
175 |
e.printStackTrace(); |
176 |
} catch (SAXException e) {
|
177 |
// TODO Auto-generated catch block
|
178 |
e.printStackTrace(); |
179 |
} |
180 |
|
181 |
return false; |
182 |
} |
183 |
|
184 |
private static String getHeaderValue(HttpResponse resp, String key) { |
185 |
if (resp.getFirstHeader(key) != null) |
186 |
return resp.getFirstHeader(key).getValue();
|
187 |
return null; |
188 |
} |
189 |
} |