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:
parent
3ea5ad32f6
commit
c7ade613c6
@ -34,8 +34,6 @@ import org.apache.commons.httpclient.auth.AuthenticationException;
|
|||||||
import org.apache.commons.httpclient.auth.InvalidCredentialsException;
|
import org.apache.commons.httpclient.auth.InvalidCredentialsException;
|
||||||
import org.apache.commons.httpclient.auth.MalformedChallengeException;
|
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
|
* @return A bearer authorization string
|
||||||
*/
|
*/
|
||||||
public String authenticate(Credentials credentials, String method, String uri) throws AuthenticationException {
|
public String authenticate(Credentials credentials, String method, String uri) throws AuthenticationException {
|
||||||
Log_OC.d(TAG, "enter BearerScheme.authenticate(Credentials, String, String)");
|
BearerCredentials bearer;
|
||||||
|
|
||||||
BearerCredentials bearer = null;
|
|
||||||
try {
|
try {
|
||||||
bearer = (BearerCredentials) credentials;
|
bearer = (BearerCredentials) credentials;
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
@ -160,8 +156,6 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
|
|||||||
* @return a basic authorization string
|
* @return a basic authorization string
|
||||||
*/
|
*/
|
||||||
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
|
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
|
||||||
Log_OC.d(TAG, "enter BearerScheme.authenticate(Credentials, HttpMethod)");
|
|
||||||
|
|
||||||
if (method == null) {
|
if (method == null) {
|
||||||
throw new IllegalArgumentException("Method may not be null");
|
throw new IllegalArgumentException("Method may not be null");
|
||||||
}
|
}
|
||||||
@ -202,7 +196,6 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
|
|||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public static String authenticate(BearerCredentials credentials, String charset) {
|
public static String authenticate(BearerCredentials credentials, String charset) {
|
||||||
Log_OC.d(TAG, "enter BearerAuthScheme.authenticate(BearerCredentials, String)");
|
|
||||||
|
|
||||||
if (credentials == null) {
|
if (credentials == null) {
|
||||||
throw new IllegalArgumentException("Credentials may not be null");
|
throw new IllegalArgumentException("Credentials may not be null");
|
||||||
@ -213,14 +206,7 @@ public class BearerAuthScheme implements AuthScheme /*extends RFC2617Scheme*/ {
|
|||||||
StringBuffer buffer = new StringBuffer();
|
StringBuffer buffer = new StringBuffer();
|
||||||
buffer.append(credentials.getAccessToken());
|
buffer.append(credentials.getAccessToken());
|
||||||
|
|
||||||
Log_OC.v(TAG, "OAUTH2: string to authorize: " + "Bearer " + buffer.toString());
|
|
||||||
return "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)
|
|
||||||
)
|
|
||||||
);*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,7 +45,7 @@ public class OAuth2ClientConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setClientId(String clientId) {
|
public void setClientId(String clientId) {
|
||||||
mClientId = clientId;
|
mClientId = (clientId == null) ? "" : clientId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getClientSecret() {
|
public String getClientSecret() {
|
||||||
@ -53,7 +53,7 @@ public class OAuth2ClientConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setClientSecret(String clientSecret) {
|
public void setClientSecret(String clientSecret) {
|
||||||
mClientSecret = clientSecret;
|
mClientSecret = (clientSecret == null) ? "" : clientSecret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRedirectUri() {
|
public String getRedirectUri() {
|
||||||
@ -61,6 +61,6 @@ public class OAuth2ClientConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setRedirectUri(String redirectUri) {
|
public void setRedirectUri(String redirectUri) {
|
||||||
this.mRedirectUri = redirectUri;
|
this.mRedirectUri = (redirectUri == null) ? "" : redirectUri;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
|
|||||||
private String mRedirectUri;
|
private String mRedirectUri;
|
||||||
private final String mAccessTokenEndpointPath;
|
private final String mAccessTokenEndpointPath;
|
||||||
|
|
||||||
private Map<String, String> mResultTokenMap;
|
private final OAuth2ResponseParser mResponseParser;
|
||||||
|
|
||||||
|
|
||||||
public OAuth2GetAccessTokenOperation(
|
public OAuth2GetAccessTokenOperation(
|
||||||
@ -76,14 +76,9 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
|
|||||||
accessTokenEndpointPath :
|
accessTokenEndpointPath :
|
||||||
OwnCloudOAuth2Provider.ACCESS_TOKEN_ENDPOINT_PATH
|
OwnCloudOAuth2Provider.ACCESS_TOKEN_ENDPOINT_PATH
|
||||||
;
|
;
|
||||||
mResultTokenMap = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
mResponseParser = new OAuth2ResponseParser();
|
||||||
public Map<String, String> getResultTokenMap() {
|
|
||||||
return mResultTokenMap;
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected RemoteOperationResult run(OwnCloudClient client) {
|
protected RemoteOperationResult run(OwnCloudClient client) {
|
||||||
@ -115,15 +110,16 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
|
|||||||
String response = postMethod.getResponseBodyAsString();
|
String response = postMethod.getResponseBodyAsString();
|
||||||
if (response != null && response.length() > 0) {
|
if (response != null && response.length() > 0) {
|
||||||
JSONObject tokenJson = new JSONObject(response);
|
JSONObject tokenJson = new JSONObject(response);
|
||||||
parseAccessTokenResult(tokenJson);
|
Map<String, String> accessTokenResult =
|
||||||
if (mResultTokenMap.get(OAuth2Constants.KEY_ERROR) != null ||
|
mResponseParser.parseAccessTokenResult(tokenJson);
|
||||||
mResultTokenMap.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
|
if (accessTokenResult.get(OAuth2Constants.KEY_ERROR) != null ||
|
||||||
|
accessTokenResult.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
|
||||||
result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
|
result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result = new RemoteOperationResult(true, postMethod);
|
result = new RemoteOperationResult(true, postMethod);
|
||||||
ArrayList<Object> data = new ArrayList<>();
|
ArrayList<Object> data = new ArrayList<>();
|
||||||
data.add(mResultTokenMap);
|
data.add(accessTokenResult);
|
||||||
result.setData(data);
|
result.setData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,82 +134,15 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation {
|
|||||||
} finally {
|
} finally {
|
||||||
if (postMethod != null)
|
if (postMethod != null)
|
||||||
postMethod.releaseConnection(); // let the connection available for other methods
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OwnCloudCredentials switchClientCredentials(OwnCloudCredentials newCredentials) {
|
private OwnCloudCredentials switchClientCredentials(OwnCloudCredentials newCredentials) {
|
||||||
// work-around for POC with owncloud/oauth2 app, that doesn't allow client
|
|
||||||
OwnCloudCredentials previousCredentials = getClient().getCredentials();
|
OwnCloudCredentials previousCredentials = getClient().getCredentials();
|
||||||
getClient().setCredentials(newCredentials);
|
getClient().setCredentials(newCredentials);
|
||||||
return previousCredentials;
|
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,11 @@ package com.owncloud.android.lib.common.authentication.oauth;
|
|||||||
|
|
||||||
public enum OAuth2GrantType {
|
public enum OAuth2GrantType {
|
||||||
AUTHORIZATION_CODE("authorization_code"),
|
AUTHORIZATION_CODE("authorization_code"),
|
||||||
REFRESH_TOKEN("refresh_token"),
|
|
||||||
IMPLICIT("implicit"),
|
IMPLICIT("implicit"),
|
||||||
PASSWORD("password"),
|
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;
|
private String mValue;
|
||||||
|
|
||||||
|
@ -33,45 +33,41 @@ import com.owncloud.android.lib.common.utils.Log_OC;
|
|||||||
|
|
||||||
import org.apache.commons.httpclient.NameValuePair;
|
import org.apache.commons.httpclient.NameValuePair;
|
||||||
import org.apache.commons.httpclient.methods.PostMethod;
|
import org.apache.commons.httpclient.methods.PostMethod;
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
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 mClientId;
|
||||||
private String mClientSecret;
|
private String mClientSecret;
|
||||||
private String mRefreshToken;
|
private String mRefreshToken;
|
||||||
private Map<String, String> mResultTokenMap;
|
|
||||||
|
|
||||||
private final String mAccessTokenEndpointPath;
|
private final String mAccessTokenEndpointPath;
|
||||||
|
|
||||||
|
private final OAuth2ResponseParser mResponseParser;
|
||||||
|
|
||||||
public OAuth2GetRefreshedAccessTokenOperation(
|
public OAuth2RefreshAccessTokenOperation(
|
||||||
String grantType,
|
|
||||||
String clientId,
|
String clientId,
|
||||||
String secretId,
|
String secretId,
|
||||||
String refreshToken,
|
String refreshToken,
|
||||||
String accessTokenEndpointPath
|
String accessTokenEndpointPath
|
||||||
) {
|
) {
|
||||||
|
|
||||||
mGrantType = grantType;
|
|
||||||
mClientId = clientId;
|
mClientId = clientId;
|
||||||
mClientSecret = secretId;
|
mClientSecret = secretId;
|
||||||
mRefreshToken = refreshToken;
|
mRefreshToken = refreshToken;
|
||||||
mResultTokenMap = null;
|
|
||||||
|
|
||||||
mAccessTokenEndpointPath =
|
mAccessTokenEndpointPath =
|
||||||
accessTokenEndpointPath != null ?
|
accessTokenEndpointPath != null ?
|
||||||
accessTokenEndpointPath :
|
accessTokenEndpointPath :
|
||||||
OwnCloudOAuth2Provider.ACCESS_TOKEN_ENDPOINT_PATH
|
OwnCloudOAuth2Provider.ACCESS_TOKEN_ENDPOINT_PATH
|
||||||
;
|
;
|
||||||
|
|
||||||
|
mResponseParser = new OAuth2ResponseParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,7 +78,10 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
NameValuePair[] nameValuePairs = new NameValuePair[3];
|
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[1] = new NameValuePair(OAuth2Constants.KEY_CLIENT_ID, mClientId);
|
||||||
nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REFRESH_TOKEN, mRefreshToken);
|
nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REFRESH_TOKEN, mRefreshToken);
|
||||||
|
|
||||||
@ -108,15 +107,16 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation {
|
|||||||
|
|
||||||
if (response != null && response.length() > 0) {
|
if (response != null && response.length() > 0) {
|
||||||
JSONObject tokenJson = new JSONObject(response);
|
JSONObject tokenJson = new JSONObject(response);
|
||||||
parseNewAccessTokenResult(tokenJson);
|
Map<String, String> accessTokenResult =
|
||||||
if (mResultTokenMap.get(OAuth2Constants.KEY_ERROR) != null ||
|
mResponseParser.parseAccessTokenResult(tokenJson);
|
||||||
mResultTokenMap.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
|
if (accessTokenResult.get(OAuth2Constants.KEY_ERROR) != null ||
|
||||||
|
accessTokenResult.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) {
|
||||||
result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
|
result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result = new RemoteOperationResult(true, postMethod);
|
result = new RemoteOperationResult(true, postMethod);
|
||||||
ArrayList<Object> data = new ArrayList<>();
|
ArrayList<Object> data = new ArrayList<>();
|
||||||
data.add(mResultTokenMap);
|
data.add(accessTokenResult);
|
||||||
result.setData(data);
|
result.setData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,83 +129,18 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation {
|
|||||||
result = new RemoteOperationResult(e);
|
result = new RemoteOperationResult(e);
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
if (postMethod != null)
|
if (postMethod != null) {
|
||||||
postMethod.releaseConnection(); // let the connection available for other methods
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OwnCloudCredentials switchClientCredentials(OwnCloudCredentials newCredentials) {
|
private OwnCloudCredentials switchClientCredentials(OwnCloudCredentials newCredentials) {
|
||||||
// work-around for POC with owncloud/oauth2 app, that doesn't allow client
|
|
||||||
OwnCloudCredentials previousCredentials = getClient().getCredentials();
|
OwnCloudCredentials previousCredentials = getClient().getCredentials();
|
||||||
getClient().setCredentials(newCredentials);
|
getClient().setCredentials(newCredentials);
|
||||||
return previousCredentials;
|
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -87,8 +87,7 @@ public class OwnCloudOAuth2RequestBuilder implements OAuth2RequestBuilder {
|
|||||||
);
|
);
|
||||||
|
|
||||||
case REFRESH_ACCESS_TOKEN:
|
case REFRESH_ACCESS_TOKEN:
|
||||||
return new OAuth2GetRefreshedAccessTokenOperation(
|
return new OAuth2RefreshAccessTokenOperation(
|
||||||
mGrantType.getValue(),
|
|
||||||
clientConfiguration.getClientId(),
|
clientConfiguration.getClientId(),
|
||||||
clientConfiguration.getClientSecret(),
|
clientConfiguration.getClientSecret(),
|
||||||
mRefreshToken,
|
mRefreshToken,
|
||||||
|
@ -189,10 +189,10 @@ public abstract class RemoteOperation implements Runnable {
|
|||||||
if (context == null)
|
if (context == null)
|
||||||
throw new IllegalArgumentException
|
throw new IllegalArgumentException
|
||||||
("Trying to execute a remote operation with a NULL Context");
|
("Trying to execute a remote operation with a NULL Context");
|
||||||
|
// mAccount and mContext in the runnerThread to create below
|
||||||
mAccount = account;
|
mAccount = account;
|
||||||
mContext = context.getApplicationContext();
|
mContext = context.getApplicationContext();
|
||||||
mClient = null; // the client instance will be created from
|
mClient = null; // the client instance will be created from
|
||||||
// mAccount and mContext in the runnerThread to create below
|
|
||||||
|
|
||||||
mListener = listener;
|
mListener = listener;
|
||||||
|
|
||||||
|
@ -25,9 +25,9 @@
|
|||||||
|
|
||||||
<resources>
|
<resources>
|
||||||
<string name="build_number"></string>
|
<string name="build_number"></string>
|
||||||
<string name="server_base_url"></string>
|
<string name="server_base_url">https://qa.oc.solidgear.es</string>
|
||||||
<string name="server_base_url_2"></string>
|
<string name="server_base_url_2">https://qa2.oc.solidgear.es</string>
|
||||||
<string name="username"></string>
|
<string name="username">android-library-test</string>
|
||||||
<string name="password"></string>
|
<string name="password">letitgo,letitgo,thatperfectappisgone</string>
|
||||||
<string name ="user_agent">Mozilla/5.0 (Android) ownCloud test project</string>
|
<string name ="user_agent">Mozilla/5.0 (Android) ownCloud test project</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -130,7 +130,7 @@ public class OwnCloudClientTest extends AndroidTestCase {
|
|||||||
client.setCredentials(credentials);
|
client.setCredentials(credentials);
|
||||||
assertEquals("Basic credentials not set", credentials, client.getCredentials());
|
assertEquals("Basic credentials not set", credentials, client.getCredentials());
|
||||||
|
|
||||||
credentials = OwnCloudCredentialsFactory.newBearerCredentials("bearerToken");
|
credentials = OwnCloudCredentialsFactory.newBearerCredentials("user", "bearerToken");
|
||||||
client.setCredentials(credentials);
|
client.setCredentials(credentials);
|
||||||
assertEquals("Bearer credentials not set", credentials, client.getCredentials());
|
assertEquals("Bearer credentials not set", credentials, client.getCredentials());
|
||||||
|
|
||||||
@ -294,7 +294,7 @@ public class OwnCloudClientTest extends AndroidTestCase {
|
|||||||
public void testGetWebdavUri() {
|
public void testGetWebdavUri() {
|
||||||
OwnCloudClient client =
|
OwnCloudClient client =
|
||||||
new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager());
|
new OwnCloudClient(mServerUri, NetworkUtils.getMultiThreadedConnManager());
|
||||||
client.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials("fakeToken"));
|
client.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials("user", "fakeToken"));
|
||||||
Uri webdavUri = client.getWebdavUri();
|
Uri webdavUri = client.getWebdavUri();
|
||||||
assertTrue("WebDAV URI does not point to the right entry point",
|
assertTrue("WebDAV URI does not point to the right entry point",
|
||||||
webdavUri.getPath().endsWith(AccountUtils.WEBDAV_PATH_4_0));
|
webdavUri.getPath().endsWith(AccountUtils.WEBDAV_PATH_4_0));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user