Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 44967be

Browse files
committed
First draft of request token implementation
1 parent f289746 commit 44967be

File tree

8 files changed

+210
-10
lines changed

8 files changed

+210
-10
lines changed

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/OwnCloudClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@
4848
import java.io.InputStream;
4949
import java.util.List;
5050

51+
import static com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER;
5152
import static com.owncloud.android.lib.common.http.HttpConstants.OC_X_REQUEST_ID;
5253

5354
public class OwnCloudClient extends HttpClient {
5455

5556
public static final String WEBDAV_FILES_PATH_4_0 = "/remote.php/dav/files/";
5657
public static final String WEBDAV_PATH_4_0_AND_LATER = "/remote.php/dav";
57-
private static final String WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/";
5858
public static final String STATUS_PATH = "/status.php";
59-
59+
private static final String WEBDAV_UPLOADS_PATH_4_0 = "/remote.php/dav/uploads/";
6060
private static final int MAX_REDIRECTIONS_COUNT = 3;
6161
private static final int MAX_REPEAT_COUNT_WITH_FRESH_CREDENTIALS = 1;
6262

@@ -104,8 +104,8 @@ public int executeHttpMethod(HttpBaseMethod method) throws Exception {
104104
method.setRequestHeader(HttpConstants.OC_X_REQUEST_ID, requestId);
105105
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
106106
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
107-
if (mCredentials.getHeaderAuth() != null) {
108-
method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
107+
if (mCredentials.getHeaderAuth() != null && method.getRequestHeader(AUTHORIZATION_HEADER) == null) {
108+
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
109109
}
110110
status = method.execute();
111111

@@ -136,7 +136,7 @@ private int executeRedirectedHttpMethod(HttpBaseMethod method) throws Exception
136136
method.setRequestHeader(HttpConstants.USER_AGENT_HEADER, SingleSessionManager.getUserAgent());
137137
method.setRequestHeader(HttpConstants.ACCEPT_ENCODING_HEADER, HttpConstants.ACCEPT_ENCODING_IDENTITY);
138138
if (mCredentials.getHeaderAuth() != null) {
139-
method.setRequestHeader(HttpConstants.AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
139+
method.setRequestHeader(AUTHORIZATION_HEADER, mCredentials.getHeaderAuth());
140140
}
141141
status = method.execute();
142142

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/authentication/OwnCloudCredentialsFactory.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
package com.owncloud.android.lib.common.authentication;
2626

27-
import com.owncloud.android.lib.common.OwnCloudClient;
28-
import com.owncloud.android.lib.common.http.HttpClient;
29-
import com.owncloud.android.lib.common.http.HttpConstants;
30-
3127
public class OwnCloudCredentialsFactory {
3228

3329
public static final String CREDENTIAL_CHARSET = "UTF-8";

owncloudComLibrary/src/main/java/com/owncloud/android/lib/common/http/HttpConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public class HttpConstants {
5151
public static final String ACCEPT_ENCODING_IDENTITY = "identity";
5252
public static final String OC_FILE_REMOTE_ID = "OC-FileId";
5353

54+
// OAuth
55+
public static final String HEADER_AUTHORIZATION_CODE = "code";
56+
public static final String HEADER_GRANT_TYPE = "grant_type";
57+
public static final String HEADER_REDIRECT_URI = "redirect_uri";
58+
public static final String HEADER_CODE_VERIFIER = "code_verifier";
59+
5460
/***********************************************************************************************************
5561
************************************************ CONTENT TYPES ********************************************
5662
***********************************************************************************************************/
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* @author Abel García de Prada
4+
*
5+
* Copyright (C) 2020 ownCloud GmbH.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*
26+
*/
27+
package com.owncloud.android.lib.resources.oauth
28+
29+
import com.owncloud.android.lib.common.OwnCloudClient
30+
import com.owncloud.android.lib.common.http.HttpConstants.AUTHORIZATION_HEADER
31+
import com.owncloud.android.lib.common.http.HttpConstants.HEADER_AUTHORIZATION_CODE
32+
import com.owncloud.android.lib.common.http.HttpConstants.HEADER_CODE_VERIFIER
33+
import com.owncloud.android.lib.common.http.HttpConstants.HEADER_GRANT_TYPE
34+
import com.owncloud.android.lib.common.http.HttpConstants.HEADER_REDIRECT_URI
35+
import com.owncloud.android.lib.common.http.HttpConstants.HTTP_OK
36+
import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod
37+
import com.owncloud.android.lib.common.operations.RemoteOperation
38+
import com.owncloud.android.lib.common.operations.RemoteOperationResult
39+
import com.owncloud.android.lib.resources.oauth.params.TokenRequestParams
40+
import com.owncloud.android.lib.resources.oauth.responses.TokenResponse
41+
import com.squareup.moshi.JsonAdapter
42+
import com.squareup.moshi.Moshi
43+
import okhttp3.FormBody
44+
import okio.ByteString.Companion.encodeUtf8
45+
import timber.log.Timber
46+
import java.net.URL
47+
48+
/**
49+
* Get OIDC Discovery
50+
*
51+
* @author Abel García de Prada
52+
*/
53+
class TokenRequestRemoteOperation(
54+
private val tokenRequestParams: TokenRequestParams
55+
) : RemoteOperation<TokenResponse>() {
56+
57+
override fun run(client: OwnCloudClient): RemoteOperationResult<TokenResponse> {
58+
var result: RemoteOperationResult<TokenResponse>
59+
60+
try {
61+
val uriBuilder = client.baseUri.buildUpon().apply {
62+
appendEncodedPath(tokenRequestParams.tokenEndpoint)
63+
}.build()
64+
65+
val requestBody = FormBody.Builder()
66+
.add(HEADER_AUTHORIZATION_CODE, tokenRequestParams.authorizationCode)
67+
.add(HEADER_GRANT_TYPE, tokenRequestParams.grantType)
68+
.add(HEADER_REDIRECT_URI, tokenRequestParams.redirectUri)
69+
.add(HEADER_CODE_VERIFIER, tokenRequestParams.codeVerifier)
70+
.build()
71+
72+
val postMethod = PostMethod(URL(uriBuilder.toString()), requestBody)
73+
74+
postMethod.addRequestHeader(AUTHORIZATION_HEADER, tokenRequestParams.clientAuth)
75+
76+
val status = client.executeHttpMethod(postMethod)
77+
78+
val responseBody = postMethod.getResponseBodyAsString()
79+
80+
if (status == HTTP_OK && responseBody != null) {
81+
Timber.d("Successful response $responseBody")
82+
83+
// Parse the response
84+
val moshi: Moshi = Moshi.Builder().build()
85+
val jsonAdapter: JsonAdapter<TokenResponse> = moshi.adapter(TokenResponse::class.java)
86+
val tokenResponse: TokenResponse? = jsonAdapter.fromJson(responseBody)
87+
88+
result = RemoteOperationResult(RemoteOperationResult.ResultCode.OK)
89+
result.data = tokenResponse
90+
91+
Timber.d("Get tokens completed and parsed to $tokenResponse")
92+
} else {
93+
result = RemoteOperationResult(postMethod)
94+
Timber.e("Failed response while getting tokens from the server status code: $status; response message: $responseBody")
95+
}
96+
97+
} catch (e: Exception) {
98+
result = RemoteOperationResult(e)
99+
Timber.e(e, "Exception while getting tokens")
100+
}
101+
102+
return result
103+
}
104+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* Copyright (C) 2020 ownCloud GmbH.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.owncloud.android.lib.resources.oauth.params
25+
26+
class TokenRequestParams(
27+
val tokenEndpoint: String,
28+
val authorizationCode: String,
29+
val grantType: String,
30+
val redirectUri: String,
31+
val codeVerifier: String,
32+
val clientAuth: String
33+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* ownCloud Android Library is available under MIT license
2+
*
3+
* Copyright (C) 2020 ownCloud GmbH.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.owncloud.android.lib.resources.oauth.responses
26+
27+
import com.squareup.moshi.Json
28+
import com.squareup.moshi.JsonClass
29+
30+
@JsonClass(generateAdapter = true)
31+
data class TokenResponse(
32+
@Json(name = "access_token")
33+
val accessToken: String,
34+
@Json(name = "expires_in")
35+
val expiresIn: Int,
36+
@Json(name = "refresh_token")
37+
val refreshToken: String,
38+
@Json(name = "token_type")
39+
val tokenType: String,
40+
@Json(name = "user_id")
41+
val userId: String?,
42+
val scope: String?,
43+
@Json(name = "additional_parameters")
44+
val additionalParameters: Map<String, String>?
45+
)

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/services/OIDCService.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ package com.owncloud.android.lib.resources.oauth.services
2525

2626
import com.owncloud.android.lib.common.OwnCloudClient
2727
import com.owncloud.android.lib.common.operations.RemoteOperationResult
28+
import com.owncloud.android.lib.resources.oauth.params.TokenRequestParams
2829
import com.owncloud.android.lib.resources.oauth.responses.OIDCDiscoveryResponse
30+
import com.owncloud.android.lib.resources.oauth.responses.TokenResponse
2931

3032
interface OIDCService {
3133

3234
fun getOIDCServerDiscovery(ownCloudClient: OwnCloudClient): RemoteOperationResult<OIDCDiscoveryResponse>
3335

36+
fun performTokenRequest(
37+
ownCloudClient: OwnCloudClient,
38+
tokenRequest: TokenRequestParams
39+
): RemoteOperationResult<TokenResponse>
40+
3441
}

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/services/implementation/OCOIDCService.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,23 @@ package com.owncloud.android.lib.resources.oauth.services.implementation
2626
import com.owncloud.android.lib.common.OwnCloudClient
2727
import com.owncloud.android.lib.common.operations.RemoteOperationResult
2828
import com.owncloud.android.lib.resources.oauth.GetOIDCDiscoveryRemoteOperation
29+
import com.owncloud.android.lib.resources.oauth.TokenRequestRemoteOperation
30+
import com.owncloud.android.lib.resources.oauth.params.TokenRequestParams
2931
import com.owncloud.android.lib.resources.oauth.responses.OIDCDiscoveryResponse
32+
import com.owncloud.android.lib.resources.oauth.responses.TokenResponse
3033
import com.owncloud.android.lib.resources.oauth.services.OIDCService
3134

32-
class OCOIDCService() : OIDCService {
35+
class OCOIDCService : OIDCService {
3336

3437
override fun getOIDCServerDiscovery(
3538
ownCloudClient: OwnCloudClient
3639
): RemoteOperationResult<OIDCDiscoveryResponse> =
3740
GetOIDCDiscoveryRemoteOperation().execute(ownCloudClient)
3841

42+
override fun performTokenRequest(
43+
ownCloudClient: OwnCloudClient,
44+
tokenRequest: TokenRequestParams
45+
): RemoteOperationResult<TokenResponse> =
46+
TokenRequestRemoteOperation(tokenRequest).execute(ownCloudClient)
47+
3948
}

0 commit comments

Comments
 (0)