Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / servers / api / client / http / Authentication.java @ 7dbfc514

History | View | Annotate | Download (6.4 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
                HttpGet get = new HttpGet(Account.getAccount().getAuthServer());
52
                get.addHeader("X-Auth-User", Account.getAccount().getUsername());
53
                get.addHeader("X-Auth-Key", "lMvqjHxChbQY+LgDqwWpYg==");// Account.getAccount().getApiKey());
54
                Log.i("AUTH STARTED", "AUTH STARTED");
55
                try {
56
                        HttpResponse resp = httpclient.execute(get);
57
                        if (resp.getStatusLine().getStatusCode() == 204) {
58

    
59
                                Account.getAccount().setAuthToken(
60
                                                getHeaderValue(resp, "X-Auth-Token"));
61
                                Account.getAccount().setServerUrl(
62
                                                getHeaderValue(resp, "X-Server-Management-Url"));
63
                                Account.getAccount().setStorageUrl(
64
                                                getHeaderValue(resp, "X-Storage-Url"));
65
                                Account.getAccount().setStorageToken(
66
                                                getHeaderValue(resp, "X-Storage-Token"));
67
                                if (Account.getAccount().getStorageToken() == null)
68
                                        Account.getAccount().setStorageToken(
69
                                                        Account.getAccount().getAuthToken());
70
                                Account.getAccount().setCdnManagementUrl(
71
                                                getHeaderValue(resp, "X-Cdn-Management-Url"));
72
                                return true;
73
                        } else {
74
                                Log.d("status code",
75
                                                Integer.toString(resp.getStatusLine().getStatusCode()));
76
                                return false;
77
                        }
78
                } catch (ClientProtocolException cpe) {
79
                        return false;
80
                } catch (IOException e) {
81
                        Log.v("info", e.toString());
82
                        return false;
83
                }
84
        }
85

    
86
        public static boolean authenticateV2(Context context) throws CloudServersException {
87
                
88
                CustomHttpClient httpclient = new CustomHttpClient(context);
89
                HttpResponse resp = null;
90
                Log.d("info", "authing with " + Account.getAccount().getAuthServerV2() + "/tokens.xml");
91
                HttpPost post = new HttpPost(Account.getAccount().getAuthServerV2() + "/tokens.xml");
92
                
93
                post.addHeader("Content-Type", "application/json");
94
                
95
                String username = Account.getAccount().getUsername().trim();
96
                String password = Account.getAccount().getPassword().trim();
97
                String requestBody = 
98
                                "{" +
99
                                          "\"auth\": { " +
100
                                            "\"passwordCredentials\": { " +
101
                                              "\"username\": " + "\"" + username + "\"" +
102
                                              "\"password\": " + "\"" + password + "\"" + 
103
                                            "}" +
104
                                          "}" +
105
                                        "}";
106
                Log.d("info", requestBody);
107
                StringEntity tmp = null;
108
                try {
109
                        tmp = new StringEntity(requestBody);
110
                } catch (UnsupportedEncodingException e) {
111
                        CloudServersException cse = new CloudServersException();
112
                        cse.setMessage(e.getLocalizedMessage());
113
                        throw cse;
114
                }
115
                post.setEntity(tmp);
116
                
117
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
118
                
119
                HttpBundle bundle = new HttpBundle();
120
                bundle.setCurlRequest(post);
121

    
122
                try {                        
123
                        resp = httpclient.execute(post);
124
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
125
                    String body = responseHandler.handleResponse(resp);
126
                        bundle.setHttpResponse(resp);
127
                        
128
                        if(resp.getStatusLine().getStatusCode() == 200){
129
                                Log.d("info", "login success");
130
                                AuthXMLParser authXMLParser = new AuthXMLParser();
131
                            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
132
                            XMLReader xmlReader = saxParser.getXMLReader();
133
                            xmlReader.setContentHandler(authXMLParser);
134
                    
135
                            xmlReader.parse(new InputSource(new StringReader(body)));
136
                            
137
                            Account.getAccount().setAuthToken(authXMLParser.getToken());
138
                            Account.getAccount().setServerUrl(authXMLParser.getServerURL());
139
                            Account.getAccount().setStorageUrl(authXMLParser.getStorageURL());
140
                            Account.getAccount().setStorageToken(authXMLParser.getToken());
141
                            Account.getAccount().setCdnManagementUrl(authXMLParser.getCdnURL());
142
                            
143
                            //Set the available regions for the account
144
                            if(Account.getAccount().getAuthServerV2().equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)){
145
                                    Account.getAccount().setLoadBalancerRegions(Preferences.UK_REGIONS);
146
                            } else if(Account.getAccount().getAuthServerV2().equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)){
147
                                    Account.getAccount().setLoadBalancerRegions(Preferences.US_REGIONS);
148
                            }
149
                            
150
                                return true;
151
                        } else {
152
                                Log.d("info", "login failed");
153
                                return false;
154
                        }
155
                } catch (ClientProtocolException e) {
156
                        CloudServersException cse = new CloudServersException();
157
                        cse.setMessage(e.getLocalizedMessage());
158
                        throw cse;
159
                } catch (IOException e) {
160
                        CloudServersException cse = new CloudServersException();
161
                        cse.setMessage(e.getLocalizedMessage());
162
                        throw cse;
163
                } catch (FactoryConfigurationError e) {
164
                        CloudServersException cse = new CloudServersException();
165
                        cse.setMessage(e.getLocalizedMessage());
166
                        throw cse;
167
                } catch (ParserConfigurationException e) {
168
                        // TODO Auto-generated catch block
169
                        e.printStackTrace();
170
                } catch (SAXException e) {
171
                        // TODO Auto-generated catch block
172
                        e.printStackTrace();
173
                }
174
                
175
                return false;
176
        }
177
        
178
        private static String getHeaderValue(HttpResponse resp, String key) {
179
                if (resp.getFirstHeader(key) != null)
180
                        return resp.getFirstHeader(key).getValue();
181
                return null;
182
        }
183
}