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
;
15 import eu
.alefzero
.webdav
.WebdavClient
;
17 public class OAuth2GetAccessToken
extends RemoteOperation
{
19 private static final String TAG
= OAuth2GetAccessToken
.class.getSimpleName();
21 private String mClientId
;
22 private String mRedirectUri
;
23 private String mGrantType
;
25 private String mOAuth2AuthorizationResponse
;
26 private Map
<String
, String
> mOAuth2ParsedAuthorizationResponse
;
27 private Map
<String
, String
> mResultTokenMap
;
30 public OAuth2GetAccessToken(String clientId
, String redirectUri
, String grantType
, String oAuth2AuthorizationResponse
) {
32 mRedirectUri
= redirectUri
;
33 mGrantType
= grantType
;
34 mOAuth2AuthorizationResponse
= oAuth2AuthorizationResponse
;
35 mOAuth2ParsedAuthorizationResponse
= new HashMap
<String
, String
>();
36 mResultTokenMap
= null
;
40 public Map
<String
, String
> getOauth2AutorizationResponse() {
41 return mOAuth2ParsedAuthorizationResponse
;
44 public Map
<String
, String
> getResultTokenMap() {
45 return mResultTokenMap
;
49 protected RemoteOperationResult
run(WebdavClient 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
.getBaseUri().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());
87 client
.exhaustResponse(postMethod
.getResponseBodyAsStream());
88 result
= new RemoteOperationResult(false
, status
, postMethod
.getResponseHeaders());
92 } catch (Exception e
) {
93 result
= new RemoteOperationResult(e
);
96 if (postMethod
!= null
)
97 postMethod
.releaseConnection(); // let the connection available for other methods
99 if (result
.isSuccess()) {
100 Log_OC
.i(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + result
.getLogMessage());
102 } else if (result
.getException() != null
) {
103 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + result
.getLogMessage(), result
.getException());
105 } else if (result
.getCode() == ResultCode
.OAUTH2_ERROR
) {
106 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + ((mResultTokenMap
!= null
) ? mResultTokenMap
.get(OAuth2Constants
.KEY_ERROR
) : "NULL"));
109 Log_OC
.e(TAG
, "OAuth2 TOKEN REQUEST with auth code " + mOAuth2ParsedAuthorizationResponse
.get("code") + " to " + client
.getBaseUri() + ": " + result
.getLogMessage());
117 private void parseAuthorizationResponse() {
118 String
[] pairs
= mOAuth2AuthorizationResponse
.split("&");
122 StringBuilder sb
= new StringBuilder();
123 while (pairs
.length
> i
) {
125 String
[] part
= pairs
[i
].split("=");
126 while (part
.length
> j
) {
130 sb
.append(key
+ " = ");
133 mOAuth2ParsedAuthorizationResponse
.put(key
, value
);
134 sb
.append(value
+ "\n");
137 Log_OC
.v(TAG
, "[" + i
+ "," + j
+ "] = " + p
);
145 private void parseAccessTokenResult (JSONObject tokenJson
) throws JSONException
{
146 mResultTokenMap
= new HashMap
<String
, String
>();
148 if (tokenJson
.has(OAuth2Constants
.KEY_ACCESS_TOKEN
)) {
149 mResultTokenMap
.put(OAuth2Constants
.KEY_ACCESS_TOKEN
, tokenJson
.getString(OAuth2Constants
.KEY_ACCESS_TOKEN
));
151 if (tokenJson
.has(OAuth2Constants
.KEY_TOKEN_TYPE
)) {
152 mResultTokenMap
.put(OAuth2Constants
.KEY_TOKEN_TYPE
, tokenJson
.getString(OAuth2Constants
.KEY_TOKEN_TYPE
));
154 if (tokenJson
.has(OAuth2Constants
.KEY_EXPIRES_IN
)) {
155 mResultTokenMap
.put(OAuth2Constants
.KEY_EXPIRES_IN
, tokenJson
.getString(OAuth2Constants
.KEY_EXPIRES_IN
));
157 if (tokenJson
.has(OAuth2Constants
.KEY_REFRESH_TOKEN
)) {
158 mResultTokenMap
.put(OAuth2Constants
.KEY_REFRESH_TOKEN
, tokenJson
.getString(OAuth2Constants
.KEY_REFRESH_TOKEN
));
160 if (tokenJson
.has(OAuth2Constants
.KEY_SCOPE
)) {
161 mResultTokenMap
.put(OAuth2Constants
.KEY_SCOPE
, tokenJson
.getString(OAuth2Constants
.KEY_SCOPE
));
163 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR
)) {
164 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR
));
166 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR_DESCRIPTION
)) {
167 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR_DESCRIPTION
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR_DESCRIPTION
));
169 if (tokenJson
.has(OAuth2Constants
.KEY_ERROR_URI
)) {
170 mResultTokenMap
.put(OAuth2Constants
.KEY_ERROR_URI
, tokenJson
.getString(OAuth2Constants
.KEY_ERROR_URI
));