1 package com
.owncloud
.android
.authenticator
.oauth2
;
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
.OnOAuth2GetCodeResultListener
.ResultOAuthType
;
21 import com
.owncloud
.android
.authenticator
.oauth2
.connection
.ConnectorOAuth2
;
24 * Implements the communication with oAuth2 server to get User Code and other useful values.
26 * @author SolidGear S.L.
29 public class OAuth2GetCodeRunnable
implements Runnable
{
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";
39 private static final String CODE_RESPONSE_TYPE
= "response_type";
40 private static final String CODE_REDIRECT_URI
= "redirect_uri";
42 private String mGrantType
= OAuth2Context
.OAUTH2_AUTH_CODE_GRANT_TYPE
;
44 private static final String TAG
= "OAuth2GetCodeRunnable";
45 private OnOAuth2GetCodeResultListener mListener
;
47 private Handler mHandler
;
48 private Context mContext
;
49 private JSONObject codeResponseJson
= null
;
50 ResultOAuthType mLatestResult
;
53 public void setListener(OnOAuth2GetCodeResultListener listener
, Handler handler
) {
58 public OAuth2GetCodeRunnable(String url
, Context context
) {
69 postResult(ResultOAuthType
.NO_NETWORK_CONNECTION
,null
);
73 if (mUrl
.startsWith("http://") || mUrl
.startsWith("https://")) {
74 mLatestResult
= (mUrl
.startsWith("https://"))? ResultOAuthType
.OK_SSL
: ResultOAuthType
.OK_NO_SSL
;
76 mUrl
= "https://" + mUrl
;
77 mLatestResult
= ResultOAuthType
.OK_SSL
;
80 if (mGrantType
.equals(OAuth2Context
.OAUTH2_AUTH_CODE_GRANT_TYPE
)) {
81 requestBrowserToGetAuthorizationCode();
83 } else if (mGrantType
.equals(OAuth2Context
.OAUTH_G_DEVICE_GETTOKEN_GRANT_TYPE
)) {
84 getAuthorizationCode();
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);
98 uri
= uriBuilder
.build();
99 Log
.d(TAG
, "Starting browser to view " + uri
.toString());
101 Intent i
= new Intent(Intent
.ACTION_VIEW
, uri
);
102 mContext
.startActivity(i
);
104 postResult(mLatestResult
, null
);
108 private void getAuthorizationCode() {
109 ConnectorOAuth2 connectorOAuth2
= new ConnectorOAuth2(mUrl
);
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());
124 if (codeResponseJson
== null
) {
125 mLatestResult
= ResultOAuthType
.HOST_NOT_AVAILABLE
;
127 postResult(mLatestResult
, codeResponseJson
);
131 private boolean isOnline() {
132 ConnectivityManager cm
= (ConnectivityManager
) mContext
.getSystemService(Context
.CONNECTIVITY_SERVICE
);
133 return cm
!= null
&& cm
.getActiveNetworkInfo() != null
&& cm
.getActiveNetworkInfo().isConnectedOrConnecting();
136 private void postResult(final ResultOAuthType result
,final JSONObject codeResponseJson
) {
137 if (mHandler
!= null
&& mListener
!= null
) {
138 mHandler
.post(new Runnable() {
141 mListener
.onOAuth2GetCodeResult(result
, codeResponseJson
);