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

Commit df8e10f

Browse files
committed
Polish code. Send only info required for each type of request
1 parent 9208af8 commit df8e10f

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,7 @@ class TokenRequestRemoteOperation(
5959
var result: RemoteOperationResult<TokenResponse>
6060

6161
try {
62-
val requestBody = FormBody.Builder()
63-
.add(HEADER_AUTHORIZATION_CODE, tokenRequestParams.authorizationCode)
64-
.add(HEADER_GRANT_TYPE, tokenRequestParams.grantType)
65-
.add(HEADER_REDIRECT_URI, tokenRequestParams.redirectUri)
66-
.add(HEADER_CODE_VERIFIER, tokenRequestParams.codeVerifier)
67-
.add(HEADER_REFRESH_TOKEN, tokenRequestParams.refreshToken.orEmpty())
68-
.build()
62+
val requestBody = tokenRequestParams.toRequestBody()
6963

7064
val postMethod = PostMethod(URL(tokenRequestParams.tokenEndpoint), requestBody)
7165

owncloudComLibrary/src/main/java/com/owncloud/android/lib/resources/oauth/params/TokenRequestParams.kt

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,51 @@
2323
*/
2424
package com.owncloud.android.lib.resources.oauth.params
2525

26-
class TokenRequestParams(
26+
import com.owncloud.android.lib.common.http.HttpConstants
27+
import okhttp3.FormBody
28+
import okhttp3.RequestBody
29+
30+
sealed class TokenRequestParams(
2731
val tokenEndpoint: String,
28-
val authorizationCode: String,
29-
val grantType: String,
30-
val redirectUri: String,
31-
val refreshToken: String? = null,
32-
val codeVerifier: String,
33-
val clientAuth: String
34-
)
32+
val clientAuth: String,
33+
val grantType: String
34+
) {
35+
abstract fun toRequestBody(): RequestBody
36+
37+
class Authorization(
38+
tokenEndpoint: String,
39+
clientAuth: String,
40+
grantType: String,
41+
val authorizationCode: String,
42+
val redirectUri: String,
43+
val codeVerifier: String
44+
) : TokenRequestParams(tokenEndpoint, clientAuth, grantType) {
45+
46+
override fun toRequestBody(): RequestBody {
47+
return FormBody.Builder()
48+
.add(HttpConstants.HEADER_AUTHORIZATION_CODE, authorizationCode)
49+
.add(HttpConstants.HEADER_GRANT_TYPE, grantType)
50+
.add(HttpConstants.HEADER_REDIRECT_URI, redirectUri)
51+
.add(HttpConstants.HEADER_CODE_VERIFIER, codeVerifier)
52+
.build()
53+
}
54+
}
55+
56+
class RefreshToken(
57+
tokenEndpoint: String,
58+
clientAuth: String,
59+
grantType: String,
60+
val refreshToken: String? = null
61+
) : TokenRequestParams(tokenEndpoint, clientAuth, grantType) {
62+
63+
override fun toRequestBody(): RequestBody {
64+
return FormBody.Builder().apply {
65+
add(HttpConstants.HEADER_GRANT_TYPE, grantType)
66+
if (!refreshToken.isNullOrBlank()) {
67+
add(HttpConstants.HEADER_REFRESH_TOKEN, refreshToken)
68+
}
69+
}.build()
70+
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)