Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Oauth token refresh & Quarkus Log exception #75

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ void init(MessagingService service) {
OidcClient client = getClient();
Multi.createFrom().ticks().every(duration)
.emitOn(Infrastructure.getDefaultWorkerPool())
.filter(x -> lastToken == null
|| lastToken.getRefreshTokenTimeSkew() == null
|| lastToken.isAccessTokenWithinRefreshInterval())
.call(() -> {
if (lastToken != null && lastToken.getRefreshToken() != null) {
if (lastToken != null && lastToken.getRefreshToken() != null
&& lastToken.isAccessTokenWithinRefreshInterval()) {
Log.info("Refreshing access token for Solace connection");
return client.refreshTokens(lastToken.getRefreshToken()).invoke(tokens -> lastToken = tokens);
} else {
Expand All @@ -64,6 +62,7 @@ void init(MessagingService service) {
.subscribe().with(x -> {
if (service.isConnected()) {
service.updateProperty(SCHEME_OAUTH2_ACCESS_TOKEN, lastToken.getAccessToken());
Log.info("Updated Solace Session with latest access token");
} else {
Log.info("Solace service is not connected, cannot update access token without valid connection");
}
Expand All @@ -75,8 +74,4 @@ OidcClient getClient() {
.orElseGet(clients::getClient);
}

public Tokens getLastToken() {
return lastToken;
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.solace.quarkus.runtime;

import static com.solace.messaging.config.SolaceProperties.AuthenticationProperties.SCHEME_OAUTH2_ACCESS_TOKEN;

import java.util.Map;
import java.util.Properties;
import java.util.function.Function;
Expand All @@ -14,6 +16,7 @@
import com.solace.quarkus.MessagingServiceClientCustomizer;

import io.quarkus.arc.SyntheticCreationalContext;
import io.quarkus.logging.Log;
import io.quarkus.runtime.ShutdownContext;
import io.quarkus.runtime.annotations.Recorder;

Expand Down Expand Up @@ -71,6 +74,14 @@ public MessagingService apply(SyntheticCreationalContext<MessagingService> conte
}
});

// Update access token on reconnect to make sure invalid token is not sent. This can happen when a reconnection happens event before scheduled token expiry.
service.addReconnectionAttemptListener(serviceEvent -> {
Log.info("Reconnecting to Solace broker due to " + serviceEvent.getMessage());
if (oidcProvider != null && authScheme != null && "AUTHENTICATION_SCHEME_OAUTH2".equals(authScheme)) {
service.updateProperty(SCHEME_OAUTH2_ACCESS_TOKEN, oidcProvider.getToken().getAccessToken());
}
});

return service.connect();
}
};
Expand Down
Empty file.
Loading