1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-08 16:36:13 +00:00

Clean-up and minor refactor after review

This commit is contained in:
David A. Velasco 2017-08-04 14:19:15 +02:00
parent 3ea5ad32f6
commit c7ade613c6
10 changed files with 116 additions and 189 deletions

View File

@ -34,8 +34,6 @@ import org.apache.commons.httpclient.auth.AuthenticationException;
import org.apache.commons.httpclient.auth.InvalidCredentialsException;
import org.apache.commons.httpclient.auth.MalformedChallengeException;
import com.owncloud.android.lib.common.utils.Log_OC;
/**
@ -125,9 +123,7 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
* @return A bearer authorization string
*/
public String authenticate(Credentials credentials, String method, String uri) throws AuthenticationException {
Log_OC.d(TAG, "enter BearerScheme.authenticate(Credentials, String, String)");
BearerCredentials bearer = null;
BearerCredentials bearer;
try {
bearer = (BearerCredentials) credentials;
} catch (ClassCastException e) {
@ -160,8 +156,6 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
* @return a basic authorization string
*/
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
Log_OC.d(TAG, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
if (method == null) {
throw new IllegalArgumentException("Method may not be null");
}
@ -202,7 +196,6 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
* @since 3.0
*/
public static String authenticate(BearerCredentials credentials, String charset) {
Log_OC.d(TAG, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
if (credentials == null) {
throw new IllegalArgumentException("Credentials may not be null");
@ -213,14 +206,7 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
StringBuffer buffer = new StringBuffer();
buffer.append(credentials.getAccessToken());
Log_OC.v(TAG, "OAUTH2: string to authorize: " + "Bearer " + buffer.toString());
return "Bearer " + buffer.toString();
//return "Bearer " + EncodingUtil.getAsciiString(EncodingUtil.getBytes(buffer.toString(), charset));
/*return "Bearer " + EncodingUtil.getAsciiString(
Base64.encodeBase64(
EncodingUtil.getBytes(buffer.toString(), charset)
)
);*/
}
/**

View File

@ -45,7 +45,7 @@ public class OAuth2ClientConfiguration {
}
public void setClientId(String clientId) {
mClientId = clientId;
mClientId = (clientId == null) ? "" : clientId;
}
public String getClientSecret() {
@ -53,7 +53,7 @@ public class OAuth2ClientConfiguration {
}
public void setClientSecret(String clientSecret) {
mClientSecret = clientSecret;
mClientSecret = (clientSecret == null) ? "" : clientSecret;
}
public String getRedirectUri() {
@ -61,6 +61,6 @@ public class OAuth2ClientConfiguration {
}
public void setRedirectUri(String redirectUri) {
this.mRedirectUri = redirectUri;
this.mRedirectUri = (redirectUri == null) ? "" : redirectUri;
}
}

View File

@ -54,7 +54,7 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
private String mRedirectUri;
private final String mAccessTokenEndpointPath;
private Map<String, String> mResultTokenMap;
private final OAuth2ResponseParser mResponseParser;
public OAuth2GetAccessTokenOperation(
@ -76,14 +76,9 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
accessTokenEndpointPath :
OwnCloudOAuth2Provider.ACCESS_TOKEN_ENDPOINT_PATH
;
mResultTokenMap = null;
}
/*
public Map<String, String> getResultTokenMap() {
return mResultTokenMap;
mResponseParser = new OAuth2ResponseParser();
}
*/
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
@ -115,15 +110,16 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
String response = postMethod.getResponseBodyAsString();
if (response != null && response.length() > 0) {
JSONObject tokenJson = new JSONObject(response);
parseAccessTokenResult(tokenJson);
if (mResultTokenMap.get(OAuth2Constants.KEY_ERROR) != null ||
mResultTokenMap.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
Map<String, String> accessTokenResult =
mResponseParser.parseAccessTokenResult(tokenJson);
if (accessTokenResult.get(OAuth2Constants.KEY_ERROR) != null ||
accessTokenResult.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
} else {
result = new RemoteOperationResult(true, postMethod);
ArrayList<Object> data = new ArrayList<>();
data.add(mResultTokenMap);
data.add(accessTokenResult);
result.setData(data);
}
@ -138,82 +134,15 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
} finally {
if (postMethod != null)
postMethod.releaseConnection(); // let the connection available for other methods
/*
if (result.isSuccess()) {
Log_OC.i(TAG, "OAuth2 TOKEN REQUEST with auth code " +
mCode + " to " +
client.getWebdavUri() + ": " + result.getLogMessage());
} else if (result.getException() != null) {
Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " +
mCode + " to " + client.
getWebdavUri() + ": " + result.getLogMessage(), result.getException());
} else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " +
mCode + " to " + client.
getWebdavUri() + ": " + ((mResultTokenMap != null) ? mResultTokenMap.
get(OAuth2Constants.KEY_ERROR) : "NULL"));
} else {
Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with auth code " +
mCode + " to " + client.
getWebdavUri() + ": " + result.getLogMessage());
}
*/
}
return result;
}
private OwnCloudCredentials switchClientCredentials(OwnCloudCredentials newCredentials) {
// work-around for POC with owncloud/oauth2 app, that doesn't allow client
OwnCloudCredentials previousCredentials = getClient().getCredentials();
getClient().setCredentials(newCredentials);
return previousCredentials;
}
private void parseAccessTokenResult (JSONObject tokenJson) throws JSONException {
mResultTokenMap = new HashMap<>();
if (tokenJson.has(OAuth2Constants.KEY_ACCESS_TOKEN)) {
mResultTokenMap.put(OAuth2Constants.KEY_ACCESS_TOKEN, tokenJson.
getString(OAuth2Constants.KEY_ACCESS_TOKEN));
}
if (tokenJson.has(OAuth2Constants.KEY_TOKEN_TYPE)) {
mResultTokenMap.put(OAuth2Constants.KEY_TOKEN_TYPE, tokenJson.
getString(OAuth2Constants.KEY_TOKEN_TYPE));
}
if (tokenJson.has(OAuth2Constants.KEY_EXPIRES_IN)) {
mResultTokenMap.put(OAuth2Constants.KEY_EXPIRES_IN, tokenJson.
getString(OAuth2Constants.KEY_EXPIRES_IN));
}
if (tokenJson.has(OAuth2Constants.KEY_REFRESH_TOKEN)) {
mResultTokenMap.put(OAuth2Constants.KEY_REFRESH_TOKEN, tokenJson.
getString(OAuth2Constants.KEY_REFRESH_TOKEN));
}
if (tokenJson.has(OAuth2Constants.KEY_SCOPE)) {
mResultTokenMap.put(OAuth2Constants.KEY_SCOPE, tokenJson.
getString(OAuth2Constants.KEY_SCOPE));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR)) {
mResultTokenMap.put(OAuth2Constants.KEY_ERROR, tokenJson.
getString(OAuth2Constants.KEY_ERROR));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR_DESCRIPTION)) {
mResultTokenMap.put(OAuth2Constants.KEY_ERROR_DESCRIPTION, tokenJson.
getString(OAuth2Constants.KEY_ERROR_DESCRIPTION));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR_URI)) {
mResultTokenMap.put(OAuth2Constants.KEY_ERROR_URI, tokenJson.
getString(OAuth2Constants.KEY_ERROR_URI));
}
if (tokenJson.has(OAuth2Constants.KEY_USER_ID)) { // not standard
mResultTokenMap.put(OAuth2Constants.KEY_USER_ID, tokenJson.
getString(OAuth2Constants.KEY_USER_ID));
}
}
}

View File

@ -28,10 +28,11 @@ package com.owncloud.android.lib.common.authentication.oauth;
public enum OAuth2GrantType {
AUTHORIZATION_CODE("authorization_code"),
REFRESH_TOKEN("refresh_token"),
IMPLICIT("implicit"),
PASSWORD("password"),
CLIENT_CREDENTIAL("client_credentials");
CLIENT_CREDENTIAL("client_credentials"),
REFRESH_TOKEN("refresh_token") // not a grant type conceptually, but used as such to refresh access tokens
;
private String mValue;

View File

@ -33,45 +33,41 @@ import com.owncloud.android.lib.common.utils.Log_OC;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation {
public class OAuth2RefreshAccessTokenOperation extends RemoteOperation {
private static final String TAG = OAuth2GetRefreshedAccessTokenOperation.class.getSimpleName();
private static final String TAG = OAuth2RefreshAccessTokenOperation.class.getSimpleName();
private String mGrantType;
private String mClientId;
private String mClientSecret;
private String mRefreshToken;
private Map<String, String> mResultTokenMap;
private final String mAccessTokenEndpointPath;
private final OAuth2ResponseParser mResponseParser;
public OAuth2GetRefreshedAccessTokenOperation(
String grantType,
public OAuth2RefreshAccessTokenOperation(
String clientId,
String secretId,
String refreshToken,
String accessTokenEndpointPath
) {
mGrantType = grantType;
mClientId = clientId;
mClientSecret = secretId;
mRefreshToken = refreshToken;
mResultTokenMap = null;
mAccessTokenEndpointPath =
accessTokenEndpointPath != null ?
accessTokenEndpointPath :
OwnCloudOAuth2Provider.ACCESS_TOKEN_ENDPOINT_PATH
;
mResponseParser = new OAuth2ResponseParser();
}
@Override
@ -82,7 +78,10 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation {
try {
NameValuePair[] nameValuePairs = new NameValuePair[3];
nameValuePairs[0] = new NameValuePair(OAuth2Constants.KEY_GRANT_TYPE, mGrantType);
nameValuePairs[0] = new NameValuePair(
OAuth2Constants.KEY_GRANT_TYPE,
OAuth2GrantType.REFRESH_TOKEN.getValue() // always for this operation
);
nameValuePairs[1] = new NameValuePair(OAuth2Constants.KEY_CLIENT_ID, mClientId);
nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REFRESH_TOKEN, mRefreshToken);
@ -108,15 +107,16 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation {
if (response != null && response.length() > 0) {
JSONObject tokenJson = new JSONObject(response);
parseNewAccessTokenResult(tokenJson);
if (mResultTokenMap.get(OAuth2Constants.KEY_ERROR) != null ||
mResultTokenMap.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
Map<String, String> accessTokenResult =
mResponseParser.parseAccessTokenResult(tokenJson);
if (accessTokenResult.get(OAuth2Constants.KEY_ERROR) != null ||
accessTokenResult.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
} else {
result = new RemoteOperationResult(true, postMethod);
ArrayList<Object> data = new ArrayList<>();
data.add(mResultTokenMap);
data.add(accessTokenResult);
result.setData(data);
}
@ -129,83 +129,18 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation {
result = new RemoteOperationResult(e);
} finally {
if (postMethod != null)
if (postMethod != null) {
postMethod.releaseConnection(); // let the connection available for other methods
/*
if (result.isSuccess()) {
Log_OC.i(TAG, "OAuth2 TOKEN REQUEST with refresh token " +
mRefreshToken + " to " +
client.getWebdavUri() + ": " + result.getLogMessage());
} else if (result.getException() != null) {
Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with refresh token " +
mRefreshToken + " to " + client.
getWebdavUri() + ": " + result.getLogMessage(), result.getException());
} else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with refresh token " +
mRefreshToken + " to " + client.
getWebdavUri() + ": " + ((mResultTokenMap != null) ? mResultTokenMap.
get(OAuth2Constants.KEY_ERROR) : "NULL"));
} else {
Log_OC.e(TAG, "OAuth2 TOKEN REQUEST with refresh token " +
mRefreshToken + " to " + client.
getWebdavUri() + ": " + result.getLogMessage());
}
*/
}
return result;
}
private OwnCloudCredentials switchClientCredentials(OwnCloudCredentials newCredentials) {
// work-around for POC with owncloud/oauth2 app, that doesn't allow client
OwnCloudCredentials previousCredentials = getClient().getCredentials();
getClient().setCredentials(newCredentials);
return previousCredentials;
}
private void parseNewAccessTokenResult(JSONObject tokenJson) throws JSONException {
mResultTokenMap = new HashMap<>();
if (tokenJson.has(OAuth2Constants.KEY_ACCESS_TOKEN)) {
mResultTokenMap.put(OAuth2Constants.KEY_ACCESS_TOKEN, tokenJson.
getString(OAuth2Constants.KEY_ACCESS_TOKEN));
}
if (tokenJson.has(OAuth2Constants.KEY_TOKEN_TYPE)) {
mResultTokenMap.put(OAuth2Constants.KEY_TOKEN_TYPE, tokenJson.
getString(OAuth2Constants.KEY_TOKEN_TYPE));
}
if (tokenJson.has(OAuth2Constants.KEY_EXPIRES_IN)) {
mResultTokenMap.put(OAuth2Constants.KEY_EXPIRES_IN, tokenJson.
getString(OAuth2Constants.KEY_EXPIRES_IN));
}
if (tokenJson.has(OAuth2Constants.KEY_REFRESH_TOKEN)) {
mResultTokenMap.put(OAuth2Constants.KEY_REFRESH_TOKEN, tokenJson.
getString(OAuth2Constants.KEY_REFRESH_TOKEN));
}
if (tokenJson.has(OAuth2Constants.KEY_SCOPE)) {
mResultTokenMap.put(OAuth2Constants.KEY_SCOPE, tokenJson.
getString(OAuth2Constants.KEY_SCOPE));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR)) {
mResultTokenMap.put(OAuth2Constants.KEY_ERROR, tokenJson.
getString(OAuth2Constants.KEY_ERROR));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR_DESCRIPTION)) {
mResultTokenMap.put(OAuth2Constants.KEY_ERROR_DESCRIPTION, tokenJson.
getString(OAuth2Constants.KEY_ERROR_DESCRIPTION));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR_URI)) {
mResultTokenMap.put(OAuth2Constants.KEY_ERROR_URI, tokenJson.
getString(OAuth2Constants.KEY_ERROR_URI));
}
if (tokenJson.has(OAuth2Constants.KEY_USER_ID)) { // not standard
mResultTokenMap.put(OAuth2Constants.KEY_USER_ID, tokenJson.
getString(OAuth2Constants.KEY_USER_ID));
}
}
}

View File

@ -0,0 +1,77 @@
/**
* ownCloud Android client application
*
* @author David A. Velasco
*
* Copyright (C) 2017 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.owncloud.android.lib.common.authentication.oauth;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
class OAuth2ResponseParser {
Map<String, String> parseAccessTokenResult(JSONObject tokenJson) throws JSONException {
Map<String, String> resultTokenMap = new HashMap<>();
if (tokenJson.has(OAuth2Constants.KEY_ACCESS_TOKEN)) {
resultTokenMap.put(OAuth2Constants.KEY_ACCESS_TOKEN, tokenJson.
getString(OAuth2Constants.KEY_ACCESS_TOKEN));
}
if (tokenJson.has(OAuth2Constants.KEY_TOKEN_TYPE)) {
resultTokenMap.put(OAuth2Constants.KEY_TOKEN_TYPE, tokenJson.
getString(OAuth2Constants.KEY_TOKEN_TYPE));
}
if (tokenJson.has(OAuth2Constants.KEY_EXPIRES_IN)) {
resultTokenMap.put(OAuth2Constants.KEY_EXPIRES_IN, tokenJson.
getString(OAuth2Constants.KEY_EXPIRES_IN));
}
if (tokenJson.has(OAuth2Constants.KEY_REFRESH_TOKEN)) {
resultTokenMap.put(OAuth2Constants.KEY_REFRESH_TOKEN, tokenJson.
getString(OAuth2Constants.KEY_REFRESH_TOKEN));
}
if (tokenJson.has(OAuth2Constants.KEY_SCOPE)) {
resultTokenMap.put(OAuth2Constants.KEY_SCOPE, tokenJson.
getString(OAuth2Constants.KEY_SCOPE));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR)) {
resultTokenMap.put(OAuth2Constants.KEY_ERROR, tokenJson.
getString(OAuth2Constants.KEY_ERROR));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR_DESCRIPTION)) {
resultTokenMap.put(OAuth2Constants.KEY_ERROR_DESCRIPTION, tokenJson.
getString(OAuth2Constants.KEY_ERROR_DESCRIPTION));
}
if (tokenJson.has(OAuth2Constants.KEY_ERROR_URI)) {
resultTokenMap.put(OAuth2Constants.KEY_ERROR_URI, tokenJson.
getString(OAuth2Constants.KEY_ERROR_URI));
}
if (tokenJson.has(OAuth2Constants.KEY_USER_ID)) { // not standard
resultTokenMap.put(OAuth2Constants.KEY_USER_ID, tokenJson.
getString(OAuth2Constants.KEY_USER_ID));
}
return resultTokenMap;
}
}

View File

@ -87,8 +87,7 @@ public class OwnCloudOAuth2RequestBuilder implements OAuth2RequestBuilder {
);
case REFRESH_ACCESS_TOKEN:
return new OAuth2GetRefreshedAccessTokenOperation(
mGrantType.getValue(),
return new OAuth2RefreshAccessTokenOperation(
clientConfiguration.getClientId(),
clientConfiguration.getClientSecret(),
mRefreshToken,

View File

@ -189,10 +189,10 @@ public abstract class RemoteOperation implements Runnable {
if (context == null)
throw new IllegalArgumentException
("Trying to execute a remote operation with a NULL Context");
// mAccount and mContext in the runnerThread to create below
mAccount = account;
mContext = context.getApplicationContext();
mClient = null; // the client instance will be created from
// mAccount and mContext in the runnerThread to create below
mListener = listener;

View File

@ -25,9 +25,9 @@
<resources>
<string name="build_number"></string>
<string name="server_base_url"></string>
<string name="server_base_url_2"></string>
<string name="username"></string>
<string name="password"></string>
<string name="server_base_url">https://qa.oc.solidgear.es</string>
<string name="server_base_url_2">https://qa2.oc.solidgear.es</string>
<string name="username">android-library-test</string>
<string name="password">letitgo,letitgo,thatperfectappisgone</string>
<string name ="user_agent">Mozilla/5.0 (Android) ownCloud test project</string>
</resources>

View File

@ -130,7 +130,7 @@ public class OwnCloudClientTest extends AndroidTestCase {
client.setCredentials(credentials);
assertEquals("Basic credentials not set", credentials, client.getCredentials());
credentials = OwnCloudCredentialsFactory.newBearerCredentials("bearerToken");
credentials = OwnCloudCredentialsFactory.newBearerCredentials("user", "bearerToken");
client.setCredentials(credentials);
assertEquals("Bearer credentials not set", credentials, client.getCredentials());
@ -294,7 +294,7 @@ public class OwnCloudClientTest extends AndroidTestCase {
public void testGetWebdavUri() {
OwnCloudClient client =
new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager());
client.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials("fakeToken"));
client.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials("user", "fakeToken"));
Uri webdavUri = client.getWebdavUri();
assertTrue("WebDAV URI does not point to the right entry point",
webdavUri.getPath().endsWith(AccountUtils.WEBDAV_PATH_4_0));