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