From 99334c2e4547a9b452a1746d14b59e03c2c8d6f6 Mon Sep 17 00:00:00 2001 From: davigonz Date: Tue, 1 Aug 2017 11:55:47 +0200 Subject: [PATCH] Include refresh token operation in library, builder --- ...Auth2GetRefreshedAccessTokenOperation.java | 21 +++++++++++++------ .../oauth/OAuth2RequestBuilder.java | 7 ++++--- .../oauth/OwnCloudOAuth2RequestBuilder.java | 18 +++++++++++++++- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2GetRefreshedAccessTokenOperation.java b/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2GetRefreshedAccessTokenOperation.java index ec95d628..28f072dc 100644 --- a/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2GetRefreshedAccessTokenOperation.java +++ b/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2GetRefreshedAccessTokenOperation.java @@ -44,26 +44,34 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation { private static final String TAG = OAuth2GetRefreshedAccessTokenOperation.class.getSimpleName(); + private String mGrantType; private String mClientId; private String mClientSecret; - private String mGrantType; - private String mRefreshToken; - private Map mResultTokenMap; + private final String mAccessTokenEndpointPath; + + public OAuth2GetRefreshedAccessTokenOperation( + String grantType, String clientId, String secretId, - String grantType, - String refreshToken + String refreshToken, + String accessTokenEndpointPath ) { + mGrantType = grantType; mClientId = clientId; mClientSecret = secretId; - mGrantType = grantType; mRefreshToken = refreshToken; mResultTokenMap = null; + + mAccessTokenEndpointPath = + accessTokenEndpointPath != null ? + accessTokenEndpointPath : + OwnCloudOAuth2Provider.ACCESS_TOKEN_ENDPOINT_PATH + ; } @Override @@ -79,6 +87,7 @@ public class OAuth2GetRefreshedAccessTokenOperation extends RemoteOperation { nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REFRESH_TOKEN, mRefreshToken); Uri.Builder uriBuilder = client.getBaseUri().buildUpon(); + uriBuilder.appendEncodedPath(mAccessTokenEndpointPath); postMethod = new PostMethod(uriBuilder.build().toString()); postMethod.setRequestBody(nameValuePairs); diff --git a/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2RequestBuilder.java b/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2RequestBuilder.java index 9d3008e0..bfb4b681 100644 --- a/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2RequestBuilder.java +++ b/src/com/owncloud/android/lib/common/network/authentication/oauth/OAuth2RequestBuilder.java @@ -31,7 +31,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperation; public interface OAuth2RequestBuilder { enum OAuthRequest { - GET_AUTHORIZATION_CODE, CREATE_ACCESS_TOKEN + GET_AUTHORIZATION_CODE, CREATE_ACCESS_TOKEN, REFRESH_ACCESS_TOKEN } void setRequest(OAuthRequest operation); @@ -40,8 +40,9 @@ public interface OAuth2RequestBuilder { void setAuthorizationCode(String code); + void setRefreshToken(String refreshToken); + RemoteOperation buildOperation(); String buildUri(); - -} +} \ No newline at end of file diff --git a/src/com/owncloud/android/lib/common/network/authentication/oauth/OwnCloudOAuth2RequestBuilder.java b/src/com/owncloud/android/lib/common/network/authentication/oauth/OwnCloudOAuth2RequestBuilder.java index 91266b6d..7647a45c 100644 --- a/src/com/owncloud/android/lib/common/network/authentication/oauth/OwnCloudOAuth2RequestBuilder.java +++ b/src/com/owncloud/android/lib/common/network/authentication/oauth/OwnCloudOAuth2RequestBuilder.java @@ -37,6 +37,7 @@ public class OwnCloudOAuth2RequestBuilder implements OAuth2RequestBuilder { private OAuthRequest mRequest; private OAuth2GrantType mGrantType = OAuth2GrantType.AUTHORIZATION_CODE; private String mCode; + private String mRefreshToken; public OwnCloudOAuth2RequestBuilder(OwnCloudOAuth2Provider ownCloudOAuth2Provider) { mOAuth2Provider = ownCloudOAuth2Provider; @@ -57,6 +58,11 @@ public class OwnCloudOAuth2RequestBuilder implements OAuth2RequestBuilder { mCode = code; } + @Override + public void setRefreshToken(String refreshToken) { + mRefreshToken = refreshToken; + } + @Override public RemoteOperation buildOperation() { if (OAuth2GrantType.AUTHORIZATION_CODE != mGrantType) { @@ -65,9 +71,10 @@ public class OwnCloudOAuth2RequestBuilder implements OAuth2RequestBuilder { OAuth2GrantType.AUTHORIZATION_CODE.getValue() + " is supported" ); } + OAuth2ClientConfiguration clientConfiguration = mOAuth2Provider.getClientConfiguration(); + switch(mRequest) { case CREATE_ACCESS_TOKEN: - OAuth2ClientConfiguration clientConfiguration = mOAuth2Provider.getClientConfiguration(); return new OAuth2GetAccessTokenOperation( mGrantType.getValue(), mCode, @@ -76,6 +83,15 @@ public class OwnCloudOAuth2RequestBuilder implements OAuth2RequestBuilder { clientConfiguration.getRedirectUri(), mOAuth2Provider.getAccessTokenEndpointPath() ); + + case REFRESH_ACCESS_TOKEN: + return new OAuth2GetRefreshedAccessTokenOperation( + mGrantType.getValue(), + clientConfiguration.getClientId(), + clientConfiguration.getClientSecret(), + mRefreshToken, + mOAuth2Provider.getAccessTokenEndpointPath() + ); default: throw new UnsupportedOperationException( "Unsupported request"