1 package com
.owncloud
.android
.operations
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
7 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
8 import org
.apache
.commons
.httpclient
.NameValuePair
;
9 import org
.json
.JSONException
;
10 import org
.json
.JSONObject
;
12 import com
.owncloud
.android
.authentication
.OAuth2Constants
;
13 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
14 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
15 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
16 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
17 import com
.owncloud
.android
.utils
.Log_OC
;
20 public class OAuth2GetAccessToken
extends RemoteOperation
{
22 private static final String TAG
= OAuth2GetAccessToken
.class.getSimpleName();
24 private String mClientId
;
25 private String mRedirectUri
;
26 private String mGrantType
;
28 private String mOAuth2AuthorizationResponse
;
29 private Map
<String
, String
> mOAuth2ParsedAuthorizationResponse
;
30 private Map
<String
, String
> mResultTokenMap
;
33 public OAuth2GetAccessToken(String clientId
, String redirectUri
, String grantType
, String oAuth2AuthorizationResponse
) {
35 mRedirectUri
= redirectUri
;
36 mGrantType
= grantType
;
37 mOAuth2AuthorizationResponse
= oAuth2AuthorizationResponse
;
38 mOAuth2ParsedAuthorizationResponse
= new HashMap
<String
, String
>();
39 mResultTokenMap
= null
;
43 public Map<String, String> getResultTokenMap() {
44 return mResultTokenMap;
49 protected RemoteOperationResult
run(OwnCloudClient client
) {
50 RemoteOperationResult result
= null
;
51 PostMethod postMethod
= null
;
54 parseAuthorizationResponse();
55 if (mOAuth2ParsedAuthorizationResponse
.keySet().contains(OAuth2Constants
.KEY_ERROR
)) {
56 if (OAuth2Constants
.VALUE_ERROR_ACCESS_DENIED
.equals(mOAuth2ParsedAuthorizationResponse
.get(OAuth2Constants
.KEY_ERROR
))) {
57 result
= new RemoteOperationResult(ResultCode
.OAUTH2_ERROR_ACCESS_DENIED
);
59 result
= new RemoteOperationResult(ResultCode
.OAUTH2_ERROR
);
64 NameValuePair
[] nameValuePairs
= new NameValuePair
[4];
65 nameValuePairs
[0] = new NameValuePair(OAuth2Constants
.KEY_GRANT_TYPE
, mGrantType
);
66 nameValuePairs
[1] = new NameValuePair(OAuth2Constants
.KEY_CODE
, mOAuth2ParsedAuthorizationResponse
.get(OAuth2Constants
.KEY_CODE
));
67 nameValuePairs
[2] = new NameValuePair(OAuth2Constants
.KEY_REDIRECT_URI
, mRedirectUri
);
68 nameValuePairs
[3] = new NameValuePair(OAuth2Constants
.KEY_CLIENT_ID
, mClientId
);
69 //nameValuePairs[4] = new NameValuePair(OAuth2Constants.KEY_SCOPE, mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_SCOPE));
71 postMethod
= new PostMethod(client
.getWebdavUri().toString());
72 postMethod
.setRequestBody(nameValuePairs
);
73 int status
= client
.executeMethod(postMethod
);
75 String response
= postMethod
.getResponseBodyAsString();
76 if (response
!= null
&& response
.length() > 0) {
77 JSONObject tokenJson
= new JSONObject(response
);
78 parseAccessTokenResult(tokenJson
);
79 if (mResultTokenMap
.get(OAuth2Constants
.KEY_ERROR
) != null
|| mResultTokenMap
.get(OAuth2Constants
.KEY_ACCESS_TOKEN
) == null
) {
80 result
= new RemoteOperationResult(ResultCode
.OAUTH2_ERROR
);
83 result
= new RemoteOperationResult(true
, status
, postMethod
.getResponseHeaders());
84 ArrayList
<Object
> data
= new ArrayList
<Object
>();
85 data
.add(mResultTokenMap
);
90 client
.exhaustResponse(postMethod
.getResponseBodyAsStream());
91 result
= new RemoteOperationResult(false
, status
, postMethod
.getResponseHeaders());
95 } catch (Exception e
) {
96 result
= new RemoteOperationResult(e
);
99 if (postMethod
!= null
)
100 postMethod
.releaseConnection(); // let the connection available for other methods
102 if (result
.isSuccess()) {
103 Log_OC
.i(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getWebdavUri() + ": " + result
.getLogMessage());
105 } else if (result
.getException() != null
) {
106 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getWebdavUri() + ": " + result
.getLogMessage(), result
.getException());
108 } else if (result
.getCode() == ResultCode
.OAUTH2_ERROR
) {
109 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getWebdavUri() + ": " + ((mResultTokenMap
!= null
) ? mResultTokenMap
.get(OAuth2Constants
.KEY_ERROR
) : "NULL"));
112 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getWebdavUri() + ": " + result
.getLogMessage());
120 private void parseAuthorizationResponse() {
121 String
[] pairs
= mOAuth2AuthorizationResponse
.split("&");
125 StringBuilder sb
= new StringBuilder();
126 while (pairs
.length
> i
) {
128 String
[] part
= pairs
[i
].split("=");
129 while (part
.length
> j
) {
133 sb
.append(key
+ " = ");
136 mOAuth2ParsedAuthorizationResponse
.put(key
, value
);
137 sb
.append(value
+ "\n");
140 Log_OC
.v(TAG
, "[" + i
+ "," + j
+ "] = " + p
);
148 private void parseAccessTokenResult (JSONObject tokenJson
) throws JSONException
{
149 mResultTokenMap
= new HashMap
<String
, String
>();
151 if (tokenJson
.has(OAuth2Constants
.KEY_ACCESS_TOKEN
)) {
152 mResultTokenMap
.put(OAuth2Constants
.KEY_ACCESS_TOKEN
, tokenJson
.getString(OAuth2Constants
.KEY_ACCESS_TOKEN
));
154 if (tokenJson
.has(OAuth2Constants
.KEY_TOKEN_TYPE
)) {
155 mResultTokenMap
.put(OAuth2Constants
.KEY_TOKEN_TYPE
, tokenJson
.getString(OAuth2Constants
.KEY_TOKEN_TYPE
));
157 if (tokenJson
.has(OAuth2Constants
.KEY_EXPIRES_IN
)) {
158 mResultTokenMap
.put(OAuth2Constants
.KEY_EXPIRES_IN
, tokenJson
.getString(OAuth2Constants
.KEY_EXPIRES_IN
));
160 if (tokenJson
.has(OAuth2Constants
.KEY_REFRESH_TOKEN
)) {
161 mResultTokenMap
.put(OAuth2Constants
.KEY_REFRESH_TOKEN
, tokenJson
.getString(OAuth2Constants
.KEY_REFRESH_TOKEN
));
163 if (tokenJson
.has(OAuth2Constants
.KEY_SCOPE
)) {
164 mResultTokenMap
.put(OAuth2Constants
.KEY_SCOPE
, tokenJson
.getString(OAuth2Constants
.KEY_SCOPE
));
166 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR
)) {
167 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR
));
169 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR_DESCRIPTION
)) {
170 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR_DESCRIPTION
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR_DESCRIPTION
));
172 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR_URI
)) {
173 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR_URI
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR_URI
));