Skip to content

Commit af5e86a

Browse files
committed
fix: prevent endless authentication loops
- docs: document that OkHttp try requests always without Authenticator and when 401 returns, it retries the request with Authenticator
1 parent 428416a commit af5e86a

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
## Release (2025-MM-DD)
2+
- `core`: [v0.4.1](core/CHANGELOG.md/#v041)
3+
- **Bugfix:** Add check in `KeyFlowAuthenticator` to prevent endless loops
24
- `objectstorage`: [v0.1.0](services/objectstorage/CHANGELOG.md#v010)
35
- Initial onboarding of STACKIT Java SDK for Object storage service
46

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.4.1
2+
- **Bugfix:** Add check in `KeyFlowAuthenticator` to prevent endless loops
3+
14
## v0.4.0
25
- **Feature:** Added core wait handler structure which can be used by every service waiter implementation.
36

core/src/main/java/cloud/stackit/sdk/core/KeyFlowAuthenticator.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public class KeyFlowAuthenticator implements Authenticator {
5252
/**
5353
* Creates the initial service account and refreshes expired access token.
5454
*
55+
* NOTE: It's normal that 2 requests are sent, it's regular OkHttp Authenticator behavior. The first
56+
* request is always attempted without the authenticator and in case the response is Unauthorized(=401),
57+
* OkHttp reattempt the request with the authenticator. See <a href="https://square.github.io/okhttp/recipes/#handling-authentication-kt-java">OkHttp Docs</a>
58+
*
5559
* @deprecated use constructor with OkHttpClient instead to prevent resource leaks. Will be
5660
* removed in April 2026.
5761
* @param cfg Configuration to set a custom token endpoint and the token expiration leeway.
@@ -65,6 +69,10 @@ public KeyFlowAuthenticator(CoreConfiguration cfg, ServiceAccountKey saKey) {
6569
/**
6670
* Creates the initial service account and refreshes expired access token.
6771
*
72+
* NOTE: It's normal that 2 requests are sent, it's regular OkHttp Authenticator behavior. The first
73+
* request is always attempted without the authenticator and in case the response is Unauthorized(=401),
74+
* OkHttp reattempt the request with the authenticator. See <a href="https://square.github.io/okhttp/recipes/#handling-authentication-kt-java">OkHttp Docs</a>
75+
*
6876
* @deprecated use constructor with OkHttpClient instead to prevent resource leaks. Will be
6977
* removed in April 2026.
7078
* @param cfg Configuration to set a custom token endpoint and the token expiration leeway.
@@ -81,6 +89,10 @@ public KeyFlowAuthenticator(
8189
/**
8290
* Creates the initial service account and refreshes expired access token.
8391
*
92+
* NOTE: It's normal that 2 requests are sent, it's regular OkHttp Authenticator behavior. The first
93+
* request is always attempted without the authenticator and in case the response is Unauthorized(=401),
94+
* OkHttp reattempt the request with the authenticator. See <a href="https://square.github.io/okhttp/recipes/#handling-authentication-kt-java">OkHttp Docs</a>
95+
*
8496
* @param httpClient OkHttpClient object
8597
* @param cfg Configuration to set a custom token endpoint and the token expiration leeway.
8698
*/
@@ -89,12 +101,16 @@ public KeyFlowAuthenticator(OkHttpClient httpClient, CoreConfiguration cfg) thro
89101
}
90102

91103
/**
92-
* Creates the initial service account and refreshes expired access token.
93-
*
94-
* @param httpClient OkHttpClient object
95-
* @param cfg Configuration to set a custom token endpoint and the token expiration leeway.
96-
* @param saKey Service Account Key, which should be used for the authentication
97-
*/
104+
* Creates the initial service account and refreshes expired access token.
105+
*
106+
* NOTE: It's normal that 2 requests are sent, it's regular OkHttp Authenticator behavior. The first
107+
* request is always attempted without the authenticator and in case the response is Unauthorized(=401),
108+
* OkHttp reattempt the request with the authenticator. See <a href="https://square.github.io/okhttp/recipes/#handling-authentication-kt-java">OkHttp Docs</a>
109+
*
110+
* @param httpClient OkHttpClient object
111+
* @param cfg Configuration to set a custom token endpoint and the token expiration leeway.
112+
* @param saKey Service Account Key, which should be used for the authentication
113+
*/
98114
public KeyFlowAuthenticator(
99115
OkHttpClient httpClient, CoreConfiguration cfg, ServiceAccountKey saKey) {
100116
this(httpClient, cfg, saKey, new EnvironmentVariables());
@@ -129,6 +145,9 @@ protected KeyFlowAuthenticator(
129145

130146
@Override
131147
public Request authenticate(Route route, @NotNull Response response) throws IOException {
148+
if (response.request().header("Authorization") != null ){
149+
return null; // Give up, we've already attempted to authenticate.
150+
}
132151
String accessToken;
133152
try {
134153
accessToken = getAccessToken();

0 commit comments

Comments
 (0)