62085b2d28824d53d2e0a72a7d3bf3d42847003b
[pub/Android/ownCloud.git] / src / com / owncloud / android / authenticator / oauth2 / OAuth2GetCodeRunnable.java
1 package com.owncloud.android.authenticator.oauth2;
2
3 import java.io.UnsupportedEncodingException;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.http.NameValuePair;
8 import org.apache.http.client.entity.UrlEncodedFormEntity;
9 import org.apache.http.message.BasicNameValuePair;
10 import org.json.JSONException;
11 import org.json.JSONObject;
12
13 import android.content.Context;
14 import android.content.Intent;
15 import android.net.ConnectivityManager;
16 import android.net.Uri;
17 import android.os.Handler;
18 import android.util.Log;
19
20 import com.owncloud.android.authenticator.oauth2.OnOAuth2GetCodeResultListener.ResultOAuthType;
21 import com.owncloud.android.authenticator.oauth2.connection.ConnectorOAuth2;
22
23 /**
24 * Implements the communication with oAuth2 server to get User Code and other useful values.
25 *
26 * @author SolidGear S.L.
27 *
28 */
29 public class OAuth2GetCodeRunnable implements Runnable {
30
31 public static final String CODE_USER_CODE = "user_code";
32 public static final String CODE_CLIENT_ID = "client_id";
33 public static final String CODE_SCOPE = "scope";
34 public static final String CODE_VERIFICATION_URL = "verification_url";
35 public static final String CODE_EXPIRES_IN = "expires_in";
36 public static final String CODE_DEVICE_CODE = "device_code";
37 public static final String CODE_INTERVAL = "interval";
38
39 private static final String CODE_RESPONSE_TYPE = "response_type";
40 private static final String CODE_REDIRECT_URI = "redirect_uri";
41
42 private String mGrantType = OAuth2Context.OAUTH2_AUTH_CODE_GRANT_TYPE;
43
44 private static final String TAG = "OAuth2GetCodeRunnable";
45 private OnOAuth2GetCodeResultListener mListener;
46 private String mUrl;
47 private Handler mHandler;
48 private Context mContext;
49 private JSONObject codeResponseJson = null;
50 ResultOAuthType mLatestResult;
51
52
53 public void setListener(OnOAuth2GetCodeResultListener listener, Handler handler) {
54 mListener = listener;
55 mHandler = handler;
56 }
57
58 public OAuth2GetCodeRunnable(String url, Context context) {
59 mListener = null;
60 mHandler = null;
61 mUrl = url;
62 mContext = context;
63 }
64
65 @Override
66 public void run() {
67
68 if (!isOnline()) {
69 postResult(ResultOAuthType.NO_NETWORK_CONNECTION,null);
70 return;
71 }
72
73 if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
74 mLatestResult = (mUrl.startsWith("https://"))? ResultOAuthType.OK_SSL : ResultOAuthType.OK_NO_SSL;
75 } else {
76 mUrl = "https://" + mUrl;
77 mLatestResult = ResultOAuthType.OK_SSL;
78 }
79
80 if (mGrantType.equals(OAuth2Context.OAUTH2_AUTH_CODE_GRANT_TYPE)) {
81 requestBrowserToGetAuthorizationCode();
82
83 } else if (mGrantType.equals(OAuth2Context.OAUTH_G_DEVICE_GETTOKEN_GRANT_TYPE)) {
84 getAuthorizationCode();
85 }
86 }
87
88 /// open the authorization endpoint in a web browser!
89 private void requestBrowserToGetAuthorizationCode() {
90 Uri uri = Uri.parse(mUrl);
91 Uri.Builder uriBuilder = uri.buildUpon();
92 uriBuilder.appendQueryParameter(CODE_RESPONSE_TYPE, OAuth2Context.OAUTH2_CODE_RESPONSE_TYPE);
93 uriBuilder.appendQueryParameter(CODE_REDIRECT_URI, OAuth2Context.MY_REDIRECT_URI);
94 uriBuilder.appendQueryParameter(CODE_CLIENT_ID, OAuth2Context.OAUTH2_F_CLIENT_ID);
95 uriBuilder.appendQueryParameter(CODE_SCOPE, OAuth2Context.OAUTH2_F_SCOPE);
96 //uriBuilder.appendQueryParameter(CODE_STATE, whateverwewant);
97
98 uri = uriBuilder.build();
99 Log.d(TAG, "Starting browser to view " + uri.toString());
100
101 Intent i = new Intent(Intent.ACTION_VIEW, uri);
102 mContext.startActivity(i);
103
104 postResult(mLatestResult, null);
105 }
106
107
108 private void getAuthorizationCode() {
109 ConnectorOAuth2 connectorOAuth2 = new ConnectorOAuth2(mUrl);
110 try {
111 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
112 nameValuePairs.add(new BasicNameValuePair(CODE_CLIENT_ID, OAuth2Context.OAUTH2_G_DEVICE_CLIENT_ID));
113 nameValuePairs.add(new BasicNameValuePair(CODE_SCOPE,OAuth2Context.OAUTH2_G_DEVICE_GETCODE_SCOPES));
114 UrlEncodedFormEntity params = new UrlEncodedFormEntity(nameValuePairs);
115 codeResponseJson = new JSONObject(connectorOAuth2.connPost(params));
116 } catch (JSONException e) {
117 Log.e(TAG, "JSONException converting to Json: " + e.toString());
118 } catch (UnsupportedEncodingException e) {
119 Log.e(TAG, "UnsupportedEncodingException encoding URL values: " + e.toString());
120 } catch (Exception e) {
121 Log.e(TAG, "Exception : " + e.toString());
122 }
123
124 if (codeResponseJson == null) {
125 mLatestResult = ResultOAuthType.HOST_NOT_AVAILABLE;
126 }
127 postResult(mLatestResult, codeResponseJson);
128 }
129
130
131 private boolean isOnline() {
132 ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
133 return cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
134 }
135
136 private void postResult(final ResultOAuthType result,final JSONObject codeResponseJson) {
137 if (mHandler != null && mListener != null) {
138 mHandler.post(new Runnable() {
139 @Override
140 public void run() {
141 mListener.onOAuth2GetCodeResult(result, codeResponseJson);
142 }
143 });
144 }
145 }
146
147 }