Skip to content

Commit 65684fb

Browse files
authored
Merge pull request #4671 from owncloud/fix/set_authorization_header_when_necessary
[FIX] Set authorization header only when necessary
2 parents 51bec32 + ebe4f6f commit 65684fb

10 files changed

Lines changed: 59 additions & 17 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ ownCloud admins and users.
3939

4040
## Summary
4141

42+
* Bugfix - Set authorization header for token request only when necessary: [#4575](https://github.com/owncloud/android/issues/4575)
4243
* Bugfix - Add explicit permission to Detekt workflow: [#4675](https://github.com/owncloud/android/pull/4675)
4344
* Change - SBOM workflow runs on dependabot PRs: [#4664](https://github.com/owncloud/android/pull/4664)
4445
* Enhancement - New layout for spaces list: [#4604](https://github.com/owncloud/android/issues/4604)
@@ -48,6 +49,15 @@ ownCloud admins and users.
4849

4950
## Details
5051

52+
* Bugfix - Set authorization header for token request only when necessary: [#4575](https://github.com/owncloud/android/issues/4575)
53+
54+
A new field has been added to the token request to indicate whether the
55+
authorization header should be applied in case `client_secret_post` method is
56+
supported or not.
57+
58+
https://github.com/owncloud/android/issues/4575
59+
https://github.com/owncloud/android/pull/4671
60+
5161
* Bugfix - Add explicit permission to Detekt workflow: [#4675](https://github.com/owncloud/android/pull/4675)
5262

5363
Read permission has been added to the Detekt workflow. Without this explicit

‎changelog/unreleased/4671‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Bugfix: Set authorization header for token request only when necessary
2+
3+
A new field has been added to the token request to indicate whether the authorization
4+
header should be applied in case `client_secret_post` method is supported or not.
5+
6+
https://github.com/owncloud/android/issues/4575
7+
https://github.com/owncloud/android/pull/4671

‎owncloudApp/src/main/java/com/owncloud/android/presentation/authentication/AccountAuthenticator.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ private String refreshToken(
346346

347347
String clientIdForRequest = null;
348348
String clientSecretForRequest = null;
349+
Boolean useAuthorizationHeader = true;
349350

350351
boolean isKiteworksServer = Boolean.parseBoolean(accountManager.getUserData(account, AccountUtils.Constants.KEY_IS_KITEWORKS_SERVER));
351352

@@ -377,6 +378,7 @@ private String refreshToken(
377378
oidcServerConfigurationUseCaseResult.getDataOrNull().isTokenEndpointAuthMethodSupportedClientSecretPost()) {
378379
clientIdForRequest = clientId;
379380
clientSecretForRequest = clientSecret;
381+
useAuthorizationHeader = false;
380382
}
381383
} else {
382384
Timber.d("OIDC Discovery failed. Server discovery info: [ %s ]",
@@ -401,6 +403,7 @@ private String refreshToken(
401403
scope,
402404
clientIdForRequest,
403405
clientSecretForRequest,
406+
useAuthorizationHeader,
404407
refreshToken
405408
);
406409

‎owncloudApp/src/main/java/com/owncloud/android/presentation/authentication/LoginActivity.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ class LoginActivity : AppCompatActivity(), SslUntrustedCertDialog.OnSslUntrusted
662662

663663
var clientId: String? = null
664664
var clientSecret: String? = null
665+
var useAuthorizationHeader = true
665666

666667
val serverInfo = authenticationViewModel.serverInfo.value?.peekContent()?.getStoredData()
667668
if (serverInfo is ServerInfo.OIDCServer) {
@@ -672,6 +673,7 @@ class LoginActivity : AppCompatActivity(), SslUntrustedCertDialog.OnSslUntrusted
672673

673674
clientId = clientRegistrationInfo?.clientId ?: contextProvider.getString(defaultClientId)
674675
clientSecret = clientRegistrationInfo?.clientSecret ?: contextProvider.getString(defaultClientSecret)
676+
useAuthorizationHeader = false
675677
}
676678
} else {
677679
tokenEndPoint = "$serverBaseUrl${File.separator}${contextProvider.getString(R.string.oauth2_url_endpoint_access)}"
@@ -686,7 +688,8 @@ class LoginActivity : AppCompatActivity(), SslUntrustedCertDialog.OnSslUntrusted
686688
clientSecret = clientSecret,
687689
authorizationCode = authorizationCode,
688690
redirectUri = OAuthUtils.buildRedirectUri(applicationContext, isKiteworksServer).toString(),
689-
codeVerifier = authenticationViewModel.codeVerifier
691+
codeVerifier = authenticationViewModel.codeVerifier,
692+
useAuthorizationHeader = useAuthorizationHeader
690693
)
691694

692695
authenticationViewModel.requestToken(requestToken)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* @author Abel García de Prada
44
*
5-
* Copyright (C) 2020 ownCloud GmbH.
5+
* Copyright (C) 2025 ownCloud GmbH.
66
*
77
* Permission is hereby granted, free of charge, to any person obtaining a copy
88
* of this software and associated documentation files (the "Software"), to deal
@@ -54,7 +54,9 @@ class TokenRequestRemoteOperation(
5454

5555
val postMethod = PostMethod(URL(tokenRequestParams.tokenEndpoint), requestBody)
5656

57-
postMethod.addRequestHeader(AUTHORIZATION_HEADER, tokenRequestParams.clientAuth)
57+
if (tokenRequestParams.useAuthorizationHeader) {
58+
postMethod.addRequestHeader(AUTHORIZATION_HEADER, tokenRequestParams.clientAuth)
59+
}
5860

5961
val status = client.executeHttpMethod(postMethod)
6062

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* ownCloud Android Library is available under MIT license
22
*
3-
* Copyright (C) 2024 ownCloud GmbH.
3+
* Copyright (C) 2025 ownCloud GmbH.
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining a copy
66
* of this software and associated documentation files (the "Software"), to deal
@@ -35,6 +35,7 @@ sealed class TokenRequestParams(
3535
val scope: String,
3636
val clientId: String?,
3737
val clientSecret: String?,
38+
val useAuthorizationHeader: Boolean,
3839
) {
3940
abstract fun toRequestBody(): RequestBody
4041

@@ -45,10 +46,11 @@ sealed class TokenRequestParams(
4546
scope: String,
4647
clientId: String?,
4748
clientSecret: String?,
49+
useAuthorizationHeader: Boolean,
4850
val authorizationCode: String,
4951
val redirectUri: String,
5052
val codeVerifier: String,
51-
) : TokenRequestParams(tokenEndpoint, clientAuth, grantType, scope, clientId, clientSecret) {
53+
) : TokenRequestParams(tokenEndpoint, clientAuth, grantType, scope, clientId, clientSecret, useAuthorizationHeader) {
5254

5355
override fun toRequestBody(): RequestBody =
5456
FormBody.Builder().apply {
@@ -70,8 +72,9 @@ sealed class TokenRequestParams(
7072
scope: String,
7173
clientId: String?,
7274
clientSecret: String?,
75+
useAuthorizationHeader: Boolean,
7376
val refreshToken: String? = null
74-
) : TokenRequestParams(tokenEndpoint, clientAuth, grantType, scope, clientId, clientSecret) {
77+
) : TokenRequestParams(tokenEndpoint, clientAuth, grantType, scope, clientId, clientSecret, useAuthorizationHeader) {
7578

7679
override fun toRequestBody(): RequestBody =
7780
FormBody.Builder().apply {

‎owncloudData/src/main/java/com/owncloud/android/data/oauth/datasources/implementation/OCRemoteOAuthDataSource.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ class OCRemoteOAuthDataSource(
112112
clientSecret = this.clientSecret,
113113
redirectUri = this.redirectUri,
114114
clientAuth = this.clientAuth,
115-
codeVerifier = this.codeVerifier
115+
codeVerifier = this.codeVerifier,
116+
useAuthorizationHeader = this.useAuthorizationHeader
116117
)
117118
is TokenRequest.RefreshToken ->
118119
TokenRequestParams.RefreshToken(
@@ -122,7 +123,8 @@ class OCRemoteOAuthDataSource(
122123
clientId = this.clientId,
123124
clientSecret = this.clientSecret,
124125
clientAuth = this.clientAuth,
125-
refreshToken = this.refreshToken
126+
refreshToken = this.refreshToken,
127+
useAuthorizationHeader = this.useAuthorizationHeader
126128
)
127129
}
128130

‎owncloudData/src/test/java/com/owncloud/android/data/oauth/RemoteOAuthUtils.kt‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* ownCloud Android client application
33
*
44
* @author Abel García de Prada
5-
* Copyright (C) 2021 ownCloud GmbH.
5+
* @author Jorge Aguado Recio
6+
*
7+
* Copyright (C) 2025 ownCloud GmbH.
68
*
79
* This program is free software: you can redistribute it and/or modify
810
* it under the terms of the GNU General Public License version 2,
@@ -52,7 +54,8 @@ val OC_REMOTE_TOKEN_REQUEST_PARAMS_ACCESS = TokenRequestParams.Authorization(
5254
clientSecret = null,
5355
authorizationCode = OC_TOKEN_REQUEST_ACCESS.authorizationCode,
5456
redirectUri = OC_TOKEN_REQUEST_ACCESS.redirectUri,
55-
codeVerifier = OC_TOKEN_REQUEST_ACCESS.codeVerifier
57+
codeVerifier = OC_TOKEN_REQUEST_ACCESS.codeVerifier,
58+
useAuthorizationHeader = true
5659
)
5760

5861
val OC_REMOTE_TOKEN_REQUEST_PARAMS_REFRESH = TokenRequestParams.RefreshToken(
@@ -62,7 +65,8 @@ val OC_REMOTE_TOKEN_REQUEST_PARAMS_REFRESH = TokenRequestParams.RefreshToken(
6265
scope = OC_TOKEN_REQUEST_REFRESH.scope,
6366
clientId = null,
6467
clientSecret = null,
65-
refreshToken = OC_TOKEN_REQUEST_REFRESH.refreshToken
68+
refreshToken = OC_TOKEN_REQUEST_REFRESH.refreshToken,
69+
useAuthorizationHeader = true
6670
)
6771

6872
val OC_REMOTE_TOKEN_RESPONSE = TokenResponse(

‎owncloudDomain/src/main/java/com/owncloud/android/domain/authentication/oauth/model/TokenRequest.kt‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
*
44
* @author Abel García de Prada
55
* @author Juan Carlos Garrote Gascón
6+
* @author Jorge Aguado Recio
67
*
7-
* Copyright (C) 2024 ownCloud GmbH.
8+
* Copyright (C) 2025 ownCloud GmbH.
89
*
910
* This program is free software: you can redistribute it and/or modify
1011
* it under the terms of the GNU General Public License version 2,
@@ -29,6 +30,7 @@ sealed class TokenRequest(
2930
val scope: String,
3031
val clientId: String?,
3132
val clientSecret: String?,
33+
val useAuthorizationHeader: Boolean,
3234
) {
3335
class AccessToken(
3436
baseUrl: String,
@@ -37,10 +39,11 @@ sealed class TokenRequest(
3739
scope: String,
3840
clientId: String? = null,
3941
clientSecret: String? = null,
42+
useAuthorizationHeader: Boolean,
4043
val authorizationCode: String,
4144
val redirectUri: String,
4245
val codeVerifier: String
43-
) : TokenRequest(baseUrl, tokenEndpoint, clientAuth, GrantType.ACCESS_TOKEN.string, scope, clientId, clientSecret)
46+
) : TokenRequest(baseUrl, tokenEndpoint, clientAuth, GrantType.ACCESS_TOKEN.string, scope, clientId, clientSecret, useAuthorizationHeader)
4447

4548
class RefreshToken(
4649
baseUrl: String,
@@ -49,8 +52,9 @@ sealed class TokenRequest(
4952
scope: String,
5053
clientId: String? = null,
5154
clientSecret: String? = null,
55+
useAuthorizationHeader: Boolean,
5256
val refreshToken: String? = null
53-
) : TokenRequest(baseUrl, tokenEndpoint, clientAuth, GrantType.REFRESH_TOKEN.string, scope, clientId, clientSecret)
57+
) : TokenRequest(baseUrl, tokenEndpoint, clientAuth, GrantType.REFRESH_TOKEN.string, scope, clientId, clientSecret, useAuthorizationHeader)
5458

5559
enum class GrantType(val string: String) {
5660
/** Request access token. [More info](https://tools.ietf.org/html/rfc6749#section-4.1.3) */

‎owncloudTestUtil/src/main/java/com/owncloud/android/testutil/oauth/TokenRequest.kt‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* ownCloud Android client application
33
*
44
* @author Abel García de Prada
5-
* Copyright (C) 2021 ownCloud GmbH.
5+
* @author Jorge Aguado Recio
6+
*
7+
* Copyright (C) 2025 ownCloud GmbH.
68
*
79
* This program is free software: you can redistribute it and/or modify
810
* it under the terms of the GNU General Public License version 2,
@@ -31,7 +33,8 @@ val OC_TOKEN_REQUEST_REFRESH = TokenRequest.RefreshToken(
3133
tokenEndpoint = OC_TOKEN_ENDPOINT,
3234
clientAuth = OC_CLIENT_AUTH,
3335
scope = OC_SCOPE,
34-
refreshToken = OC_REFRESH_TOKEN
36+
refreshToken = OC_REFRESH_TOKEN,
37+
useAuthorizationHeader = true
3538
)
3639

3740
val OC_TOKEN_REQUEST_ACCESS = TokenRequest.AccessToken(
@@ -41,5 +44,6 @@ val OC_TOKEN_REQUEST_ACCESS = TokenRequest.AccessToken(
4144
scope = OC_SCOPE,
4245
authorizationCode = "4uth0r1z4t10nC0d3",
4346
redirectUri = OC_REDIRECT_URI,
44-
codeVerifier = "A high-entropy cryptographic random STRING using the unreserved characters"
47+
codeVerifier = "A high-entropy cryptographic random STRING using the unreserved characters",
48+
useAuthorizationHeader = true
4549
)

0 commit comments

Comments
 (0)