Skip to content

Commit 52951fe

Browse files
committed
add exception for authentication related issues
1 parent 05d29e0 commit 52951fe

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cloud.stackit.sdk.core.config.CoreConfiguration;
55
import cloud.stackit.sdk.core.config.EnvironmentVariables;
66
import cloud.stackit.sdk.core.exception.ApiException;
7+
import cloud.stackit.sdk.core.exception.AuthenticationException;
78
import cloud.stackit.sdk.core.model.ServiceAccountKey;
89
import cloud.stackit.sdk.core.utils.Utils;
910
import com.auth0.jwt.JWT;
@@ -132,7 +133,7 @@ public Request authenticate(Route route, @NotNull Response response) throws IOEx
132133
try {
133134
accessToken = getAccessToken();
134135
} catch (ApiException | InvalidKeySpecException e) {
135-
throw new IllegalStateException(e);
136+
throw new AuthenticationException("Failed to obtain access token", e);
136137
}
137138

138139
// Return a new request with the refreshed token
@@ -215,7 +216,7 @@ protected void createAccessToken() throws InvalidKeySpecException, IOException,
215216
try {
216217
assertion = generateSelfSignedJWT();
217218
} catch (NoSuchAlgorithmException e) {
218-
throw new IllegalStateException(
219+
throw new AuthenticationException(
219220
"could not find required algorithm for jwt signing. This should not happen and should be reported on https://github.com/stackitcloud/stackit-sdk-java/issues",
220221
e);
221222
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cloud.stackit.sdk.core;
22

33
import cloud.stackit.sdk.core.exception.ApiException;
4+
import cloud.stackit.sdk.core.exception.AuthenticationException;
45
import java.io.IOException;
56
import java.security.spec.InvalidKeySpecException;
67
import okhttp3.Interceptor;
@@ -37,7 +38,7 @@ public Response intercept(Chain chain) throws IOException {
3738
} catch (InvalidKeySpecException | ApiException e) {
3839
// try-catch required, because ApiException can not be thrown in the implementation
3940
// of Interceptor.intercept(Chain chain)
40-
throw new IllegalStateException(e);
41+
throw new AuthenticationException("Failed to obtain access token for request authentication", e);
4142
}
4243

4344
Request authenticatedRequest =
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cloud.stackit.sdk.core.exception;
2+
3+
/**
4+
* Exception thrown when authentication operations fail.
5+
* This includes token generation, refresh, and validation failures.
6+
*/
7+
public class AuthenticationException extends RuntimeException {
8+
private static final long serialVersionUID = 1L;
9+
10+
/**
11+
* Constructs a new AuthenticationException with the specified detail message.
12+
*
13+
* @param message the detail message
14+
*/
15+
public AuthenticationException(String message) {
16+
super(message);
17+
}
18+
19+
/**
20+
* Constructs a new AuthenticationException with the specified detail message and cause.
21+
*
22+
* @param message the detail message
23+
* @param cause the cause of this exception
24+
*/
25+
public AuthenticationException(String message, Throwable cause) {
26+
super(message, cause);
27+
}
28+
29+
/**
30+
* Constructs a new AuthenticationException with the specified cause.
31+
*
32+
* @param cause the cause of this exception
33+
*/
34+
public AuthenticationException(Throwable cause) {
35+
super(cause);
36+
}
37+
}

0 commit comments

Comments
 (0)