1 package com
.owncloud
.android
.operations
;
3 import java
.util
.HashMap
;
6 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
7 import org
.apache
.commons
.httpclient
.NameValuePair
;
8 import org
.json
.JSONException
;
9 import org
.json
.JSONObject
;
11 import com
.owncloud
.android
.Log_OC
;
12 import com
.owncloud
.android
.authentication
.OAuth2Constants
;
13 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
16 import eu
.alefzero
.webdav
.WebdavClient
;
18 public class OAuth2GetAccessToken
extends RemoteOperation
{
20 private static final String TAG
= OAuth2GetAccessToken
.class.getSimpleName();
22 private String mClientId
;
23 private String mRedirectUri
;
24 private String mGrantType
;
26 private String mOAuth2AuthorizationResponse
;
27 private Map
<String
, String
> mOAuth2ParsedAuthorizationResponse
;
28 private Map
<String
, String
> mResultTokenMap
;
31 public OAuth2GetAccessToken(String clientId
, String redirectUri
, String grantType
, String oAuth2AuthorizationResponse
) {
33 mRedirectUri
= redirectUri
;
34 mGrantType
= grantType
;
35 mOAuth2AuthorizationResponse
= oAuth2AuthorizationResponse
;
36 mOAuth2ParsedAuthorizationResponse
= new HashMap
<String
, String
>();
37 mResultTokenMap
= null
;
41 public Map
<String
, String
> getOauth2AutorizationResponse() {
42 return mOAuth2ParsedAuthorizationResponse
;
45 public Map
<String
, String
> getResultTokenMap() {
46 return mResultTokenMap
;
50 protected RemoteOperationResult
run(WebdavClient client
) {
51 RemoteOperationResult result
= null
;
52 PostMethod postMethod
= null
;
55 parseAuthorizationResponse();
56 if (mOAuth2ParsedAuthorizationResponse
.keySet().contains(OAuth2Constants
.KEY_ERROR
)) {
57 if (OAuth2Constants
.VALUE_ERROR_ACCESS_DENIED
.equals(mOAuth2ParsedAuthorizationResponse
.get(OAuth2Constants
.KEY_ERROR
))) {
58 result
= new RemoteOperationResult(ResultCode
.OAUTH2_ERROR_ACCESS_DENIED
);
60 result
= new RemoteOperationResult(ResultCode
.OAUTH2_ERROR
);
65 NameValuePair
[] nameValuePairs
= new NameValuePair
[4];
66 nameValuePairs
[0] = new NameValuePair(OAuth2Constants
.KEY_GRANT_TYPE
, mGrantType
);
67 nameValuePairs
[1] = new NameValuePair(OAuth2Constants
.KEY_CODE
, mOAuth2ParsedAuthorizationResponse
.get(OAuth2Constants
.KEY_CODE
));
68 nameValuePairs
[2] = new NameValuePair(OAuth2Constants
.KEY_REDIRECT_URI
, mRedirectUri
);
69 nameValuePairs
[3] = new NameValuePair(OAuth2Constants
.KEY_CLIENT_ID
, mClientId
);
70 //nameValuePairs[4] = new NameValuePair(OAuth2Constants.KEY_SCOPE, mOAuth2ParsedAuthorizationResponse.get(OAuth2Constants.KEY_SCOPE));
72 postMethod
= new PostMethod(client
.getBaseUri().toString());
73 postMethod
.setRequestBody(nameValuePairs
);
74 int status
= client
.executeMethod(postMethod
);
76 String response
= postMethod
.getResponseBodyAsString();
77 if (response
!= null
&& response
.length() > 0) {
78 JSONObject tokenJson
= new JSONObject(response
);
79 parseAccessTokenResult(tokenJson
);
80 if (mResultTokenMap
.get(OAuth2Constants
.KEY_ERROR
) != null
|| mResultTokenMap
.get(OAuth2Constants
.KEY_ACCESS_TOKEN
) == null
) {
81 result
= new RemoteOperationResult(ResultCode
.OAUTH2_ERROR
);
84 result
= new RemoteOperationResult(true
, status
, postMethod
.getResponseHeaders());
88 client
.exhaustResponse(postMethod
.getResponseBodyAsStream());
89 result
= new RemoteOperationResult(false
, status
, postMethod
.getResponseHeaders());
93 } catch (Exception e
) {
94 result
= new RemoteOperationResult(e
);
97 if (postMethod
!= null
)
98 postMethod
.releaseConnection(); // let the connection available for other methods
100 if (result
.isSuccess()) {
101 Log_OC
.i(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + result
.getLogMessage());
103 } else if (result
.getException() != null
) {
104 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + result
.getLogMessage(), result
.getException());
106 } else if (result
.getCode() == ResultCode
.OAUTH2_ERROR
) {
107 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + ((mResultTokenMap
!= null
) ? mResultTokenMap
.get(OAuth2Constants
.KEY_ERROR
) : "NULL"));
110 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + result
.getLogMessage());
118 private void parseAuthorizationResponse() {
119 String
[] pairs
= mOAuth2AuthorizationResponse
.split("&");
123 StringBuilder sb
= new StringBuilder();
124 while (pairs
.length
> i
) {
126 String
[] part
= pairs
[i
].split("=");
127 while (part
.length
> j
) {
131 sb
.append(key
+ " = ");
134 mOAuth2ParsedAuthorizationResponse
.put(key
, value
);
135 sb
.append(value
+ "\n");
138 Log_OC
.v(TAG
, "[" + i
+ "," + j
+ "] = " + p
);
146 private void parseAccessTokenResult (JSONObject tokenJson
) throws JSONException
{
147 mResultTokenMap
= new HashMap
<String
, String
>();
149 if (tokenJson
.has(OAuth2Constants
.KEY_ACCESS_TOKEN
)) {
150 mResultTokenMap
.put(OAuth2Constants
.KEY_ACCESS_TOKEN
, tokenJson
.getString(OAuth2Constants
.KEY_ACCESS_TOKEN
));
152 if (tokenJson
.has(OAuth2Constants
.KEY_TOKEN_TYPE
)) {
153 mResultTokenMap
.put(OAuth2Constants
.KEY_TOKEN_TYPE
, tokenJson
.getString(OAuth2Constants
.KEY_TOKEN_TYPE
));
155 if (tokenJson
.has(OAuth2Constants
.KEY_EXPIRES_IN
)) {
156 mResultTokenMap
.put(OAuth2Constants
.KEY_EXPIRES_IN
, tokenJson
.getString(OAuth2Constants
.KEY_EXPIRES_IN
));
158 if (tokenJson
.has(OAuth2Constants
.KEY_REFRESH_TOKEN
)) {
159 mResultTokenMap
.put(OAuth2Constants
.KEY_REFRESH_TOKEN
, tokenJson
.getString(OAuth2Constants
.KEY_REFRESH_TOKEN
));
161 if (tokenJson
.has(OAuth2Constants
.KEY_SCOPE
)) {
162 mResultTokenMap
.put(OAuth2Constants
.KEY_SCOPE
, tokenJson
.getString(OAuth2Constants
.KEY_SCOPE
));
164 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR
)) {
165 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR
));
167 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR_DESCRIPTION
)) {
168 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR_DESCRIPTION
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR_DESCRIPTION
));
170 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR_URI
)) {
171 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR_URI
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR_URI
));