Permalink
Browse files

Get the OAuth consumer from the Ubuntu SSO webservice.

  • Loading branch information...
1 parent c94e51b commit f84d129886f201841c556ce12a4421e7aebe677c @alecu committed Mar 1, 2011
View
@@ -3,7 +3,7 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
- <classpathentry kind="lib" path="libs/signpost-commonshttp4-1.2.jar"/>
- <classpathentry kind="lib" path="libs/signpost-core-1.2.jar"/>
+ <classpathentry kind="lib" path="libs/signpost-commonshttp4-1.2.1.1.jar"/>
+ <classpathentry kind="lib" path="libs/signpost-core-1.2.1.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -9,6 +9,7 @@
import java.io.Writer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
@@ -140,15 +141,13 @@ public boolean deleteDocument(String databaseName,
}
public JSONObject get(String url) {
- DefaultHttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(getHostUrl() + url);
// Execute the request
HttpResponse response;
JSONObject json = null;
try {
- signRequest(httpget);
- response = httpclient.execute(httpget);
+ response = executeRequest(httpget);
// Examine the response status
Log.i(TAG, response.getStatusLine().toString());
@@ -178,9 +177,6 @@ public JSONObject get(String url) {
return json;
}
- protected void signRequest(HttpUriRequest request) {
- }
-
/**
* @return the Json document
*/
@@ -210,9 +206,7 @@ public static String getStacktrace(Throwable e) {
*/
private JSONObject sendCouchRequest(HttpUriRequest request) {
try {
- signRequest(request);
- HttpResponse httpResponse = (HttpResponse) new DefaultHttpClient()
- .execute(request);
+ HttpResponse httpResponse = executeRequest(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
// Read the content stream
@@ -230,6 +224,10 @@ private JSONObject sendCouchRequest(HttpUriRequest request) {
return null;
}
+ protected HttpResponse executeRequest(HttpUriRequest request) throws ClientProtocolException, IOException {
+ return (HttpResponse) new DefaultHttpClient().execute(request);
+ }
+
/**
* @return the revision id of the updated document
*/
@@ -11,7 +11,7 @@
public class TestClient extends Activity {
- String DBNAME = "hackathon_5";
+ String DBNAME = "hackathon_83";
String TEST_DOC_ID = "test_document_1";
private TextView view;
@@ -26,7 +26,7 @@ public void onCreate(Bundle savedInstanceState) {
view.append("Running tests...\n\n");
setContentView(view);
try {
- droidCouch = new UbuntuOneDroidCouch();
+ droidCouch = new UbuntuOneDroidCouch(this);
testDatabaseDoesNotExist();
testCreateDatabase();
testGetDatabaseInfo();
@@ -0,0 +1,13 @@
+package se.msc.android.droidcouch.ubuntuone;
+
+import org.apache.http.HttpResponse;
+
+public class HttpError extends Exception {
+
+ private HttpResponse response;
+
+ public HttpError(HttpResponse response) {
+ this.response = response;
+ }
+
+}
@@ -0,0 +1,196 @@
+package se.msc.android.droidcouch.ubuntuone;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+
+import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
+import oauth.signpost.exception.OAuthException;
+import oauth.signpost.signature.HmacSha1MessageSigner;
+
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.os.Build;
+import android.preference.PreferenceManager;
+
+public class UbuntuOneCredentials {
+
+ private static final String USERNAME = "";
+ private static final String PASSWORD = "";
+
+ private static final String BASE_TOKEN_NAME = "Ubuntu One @ ";
+ private static final String CONSUMER_KEY = "consumer_key";
+ private static final String CONSUMER_SECRET = "consumer_secret";
+ private static final String ACCESS_TOKEN = "token";
+ private static final String TOKEN_SECRET = "token_secret";
+
+ private static final String LOGIN_HOST = "login.ubuntu.com";
+ private static final int LOGIN_PORT = 443;
+ private static final String LOGIN_URL = "https://" + LOGIN_HOST + ":"
+ + LOGIN_PORT + "/api/1.0/authentications"
+ + "?ws.op=authenticate&token_name=";
+ private static final String PING_URL = "https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/";
+ private static final String UTF8 = "UTF-8";
+
+ private SharedPreferences prefs;
+ private SharedPreferences.Editor prefsEditor;
+
+ private CommonsHttpOAuthConsumer consumer;
+
+ public UbuntuOneCredentials(Context ctx) {
+ prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
+ prefsEditor = prefs.edit();
+ }
+
+ public void invalidate() {
+ prefsEditor.remove(CONSUMER_KEY);
+ prefsEditor.remove(CONSUMER_SECRET);
+ prefsEditor.remove(ACCESS_TOKEN);
+ prefsEditor.remove(TOKEN_SECRET);
+ prefsEditor.commit();
+ consumer = null;
+ }
+
+ public void signRequest(HttpRequest request) {
+ int retries = 3;
+
+ if (consumer == null) {
+ buildConsumer();
+ }
+
+ while (retries-- > 0) {
+ try {
+ if (consumer != null) {
+ // We need to remove the previous Authorization header
+ // because signpost fails to sign a second time otherwise.
+ request.removeHeaders("Authorization");
+ consumer.sign(request);
+ return;
+ }
+ } catch (OAuthException e) {
+ e.printStackTrace();
+ }
+ login(USERNAME, PASSWORD);
+ }
+ }
+
+ private void login(String username, String password) {
+ invalidate();
+ DefaultHttpClient httpClient = new DefaultHttpClient();
+ httpClient.getCredentialsProvider().setCredentials(
+ new AuthScope(LOGIN_HOST, LOGIN_PORT),
+ new UsernamePasswordCredentials(username, password));
+ HttpUriRequest request = new HttpGet(buildLoginUrl());
+ try {
+ HttpResponse response = httpClient.execute(request);
+ verifyResponse(response);
+ JSONObject loginData = responseToJson(response);
+ storeTokens(loginData.getString(CONSUMER_KEY), loginData
+ .getString(CONSUMER_SECRET), loginData
+ .getString(ACCESS_TOKEN), loginData.getString(TOKEN_SECRET));
+ buildConsumer();
+ ping_u1_url(username);
+ } catch (ClientProtocolException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (JSONException e) {
+ e.printStackTrace();
+ } catch (HttpError e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void ping_u1_url(String username) {
+ try {
+ String ping_url = PING_URL + username;
+ HttpGet request = new HttpGet(ping_url);
+ DefaultHttpClient httpClient = new DefaultHttpClient();
+ HttpResponse response = null;
+ int retries = 3;
+
+ while (retries-- > 0) {
+ signRequest(request);
+ response = httpClient.execute(request);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode == 400 || statusCode == 401) {
+ invalidate();
+ } else {
+ return;
+ }
+ }
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ } catch (ClientProtocolException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void verifyResponse(HttpResponse response) throws HttpError {
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode < 200 || statusCode > 299) {
+ throw new HttpError(response);
+ }
+ }
+
+ private JSONObject responseToJson(HttpResponse response)
+ throws UnsupportedEncodingException, IOException, JSONException {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(
+ response.getEntity().getContent(), "UTF-8"));
+ StringBuilder builder = new StringBuilder();
+ for (String line = null; (line = reader.readLine()) != null;) {
+ builder.append(line).append("\n");
+ }
+ return new JSONObject(builder.toString());
+ }
+
+ private String buildLoginUrl() {
+ String token_name = BASE_TOKEN_NAME + Build.MODEL;
+ String login_url = LOGIN_URL;
+ try {
+ login_url += URLEncoder.encode(token_name, UTF8);
+ } catch (UnsupportedEncodingException e) {
+ login_url += "Android";
+ }
+ return login_url;
+ }
+
+ private void storeTokens(String consumer_key, String consumer_secret,
+ String access_token, String token_secret) {
+ prefsEditor.putString(CONSUMER_KEY, consumer_key);
+ prefsEditor.putString(CONSUMER_SECRET, consumer_secret);
+ prefsEditor.putString(ACCESS_TOKEN, access_token);
+ prefsEditor.putString(TOKEN_SECRET, token_secret);
+ prefsEditor.commit();
+ }
+
+ private void buildConsumer() {
+ String consumer_key = prefs.getString(CONSUMER_KEY, null);
+ String consumer_secret = prefs.getString(CONSUMER_SECRET, null);
+ String access_token = prefs.getString(ACCESS_TOKEN, null);
+ String token_secret = prefs.getString(TOKEN_SECRET, null);
+
+ if (consumer_key != null && consumer_secret != null
+ && access_token != null && token_secret != null) {
+ consumer = new CommonsHttpOAuthConsumer(consumer_key,
+ consumer_secret);
+ consumer.setMessageSigner(new HmacSha1MessageSigner());
+ consumer.setTokenWithSecret(access_token, token_secret);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit f84d129

Please sign in to comment.