Skip to content

Commit 715b1e4

Browse files
Improve error handling (#9)
1 parent 0d88360 commit 715b1e4

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
}
1111

1212
group 'com.authsignal'
13-
version '2.0.0'
13+
version '2.0.1'
1414

1515
repositories {
1616
mavenCentral()

app/src/main/java/com/authsignal/keycloak/AuthsignalAuthenticator.java

+26-6
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ public void authenticate(AuthenticationFlowContext context) {
7474
TrackRequest request = new TrackRequest();
7575
request.action = actionCode(context);
7676

77-
request.userId = context.getUser().getId();
7877
request.attributes = new TrackAttributes();
7978
request.attributes.redirectUrl = redirectUrl;
8079
request.attributes.ipAddress = context.getConnection().getRemoteAddr();
8180
request.attributes.userAgent =
8281
context.getHttpRequest().getHttpHeaders().getHeaderString("User-Agent");
82+
request.userId = context.getUser().getId();
8383

8484
try {
8585
CompletableFuture<TrackResponse> responseFuture = authsignalClient.track(request);
@@ -150,21 +150,41 @@ public void close() {
150150
// Cleanup if needed
151151
}
152152

153+
private String generateConfigErrorMessage(String prefix) {
154+
return prefix + " Add provider details in your Keycloak admin portal.";
155+
}
156+
153157
private String secretKey(AuthenticationFlowContext context) {
154158
AuthenticatorConfigModel config = context.getAuthenticatorConfig();
155159
if (config == null) {
156-
return "";
160+
throw new IllegalStateException(
161+
generateConfigErrorMessage("Authsignal provider config is missing."));
157162
}
158-
return String.valueOf(config.getConfig().get(AuthsignalAuthenticatorFactory.PROP_SECRET_KEY));
163+
Object secretKeyObj = config.getConfig().get(AuthsignalAuthenticatorFactory.PROP_SECRET_KEY);
164+
String tenantSecretKey = (secretKeyObj != null) ? secretKeyObj.toString() : null;
165+
166+
if (tenantSecretKey == null || tenantSecretKey.isEmpty()) {
167+
throw new IllegalStateException(
168+
generateConfigErrorMessage("Authsignal Tenant Secret Key is not configured."));
169+
}
170+
return tenantSecretKey;
159171
}
160172

161173
private String baseUrl(AuthenticationFlowContext context) {
162174
AuthenticatorConfigModel config = context.getAuthenticatorConfig();
163175
if (config == null) {
164-
return "";
176+
throw new IllegalStateException(
177+
generateConfigErrorMessage("Authsignal provider config is missing."));
178+
}
179+
Object apiUrlObj =
180+
config.getConfig().get(AuthsignalAuthenticatorFactory.PROP_API_HOST_BASE_URL);
181+
String apiUrl = (apiUrlObj != null) ? apiUrlObj.toString() : null;
182+
183+
if (apiUrl == null || apiUrl.isEmpty()) {
184+
throw new IllegalStateException(
185+
generateConfigErrorMessage("Authsignal API URL is not configured."));
165186
}
166-
return String
167-
.valueOf(config.getConfig().get(AuthsignalAuthenticatorFactory.PROP_API_HOST_BASE_URL));
187+
return apiUrl;
168188
}
169189

170190
private String actionCode(AuthenticationFlowContext context) {

0 commit comments

Comments
 (0)