1 package com
.owncloud
.android
.operations
;
3 import java
.io
.UnsupportedEncodingException
;
4 import java
.util
.ArrayList
;
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
;
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
;
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
;
26 * Implements the communication with oAuth2 server to get User Code and other useful values.
28 * @author SolidGear S.L.
31 public class OAuth2GetAuthorizationToken
implements Runnable
{
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";
41 private static final String CODE_RESPONSE_TYPE
= "response_type";
42 private static final String CODE_REDIRECT_URI
= "redirect_uri";
44 private String mGrantType
= OAuth2Context
.OAUTH2_AUTH_CODE_GRANT_TYPE
;
46 private static final String TAG
= "OAuth2GetCodeRunnable";
47 private OnOAuth2GetCodeResultListener mListener
;
49 private Handler mHandler
;
50 private Context mContext
;
51 //private JSONObject codeResponseJson = null;
52 ResultOAuthType mLatestResult
;
55 public void setListener(OnOAuth2GetCodeResultListener listener
, Handler handler
) {
60 public OAuth2GetAuthorizationToken(String url
, Context context
) {
71 postResult(ResultOAuthType
.NO_NETWORK_CONNECTION
,null
);
75 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
76 mLatestResult
= (mUrl
.startsWith("https://"))? ResultOAuthType
.OK_SSL
: ResultOAuthType
.OK_NO_SSL
;
78 mUrl
= "https://" + mUrl
;
79 mLatestResult
= ResultOAuthType
.OK_SSL
;
82 if (mGrantType
.equals(OAuth2Context
.OAUTH2_AUTH_CODE_GRANT_TYPE
)) {
83 requestBrowserToGetAuthorizationCode();
85 } /*else if (mGrantType.equals(OAuth2Context.OAUTH_G_DEVICE_GETTOKEN_GRANT_TYPE)) {
86 getAuthorizationCode();
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);
100 uri
= uriBuilder
.build();
101 Log
.d(TAG
, "Starting browser to view " + uri
.toString());
103 Intent i
= new Intent(Intent
.ACTION_VIEW
, uri
);
104 mContext
.startActivity(i
);
106 postResult(mLatestResult
, null
);
111 private void getAuthorizationCode() {
112 ConnectorOAuth2 connectorOAuth2 = new ConnectorOAuth2(mUrl);
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());
127 if (codeResponseJson == null) {
128 mLatestResult = ResultOAuthType.HOST_NOT_AVAILABLE;
130 postResult(mLatestResult, codeResponseJson);
135 private boolean isOnline() {
136 ConnectivityManager cm
= (ConnectivityManager
) mContext
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
137 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnectedOrConnecting();
140 private void postResult(final ResultOAuthType result
,final JSONObject codeResponseJson
) {
141 if (mHandler
!= null
&& mListener
!= null
) {
142 mHandler
.post(new Runnable() {
145 mListener
.onOAuth2GetCodeResult(result
, codeResponseJson
);