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

[FIXES #2408] SSO - Supported OpenID services #372

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -86,7 +86,7 @@ public Authentication authenticate(Authentication authentication)
HttpServletRequest request = getRequest();
// set tokens as request attributes so that can made available in a cookie for the frontend
// on the callback url.
if (accessToken != null) {
if (accessToken != null && !accessToken.isExpired()) {
expiration = accessToken.getExp();
if (request != null) request.setAttribute(ACCESS_TOKEN_PARAM, accessToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected void updateCache(Authentication authentication) {
KeyCloakHelper helper = GeoStoreContext.bean(KeyCloakHelper.class);
KeycloakTokenDetails keycloakDetails = (KeycloakTokenDetails) details;
String accessToken = keycloakDetails.getAccessToken();
if (accessToken != null) {
if (accessToken != null && !accessToken.isEmpty()) {
cache.putCacheEntry(accessToken, authentication);
if (helper != null) {
HttpFacade facade = new SimpleHttpFacade(getRequest(), getResponse());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ private static HttpHeaders getHeaders(String accessToken, OAuth2Configuration co
configuration.clientId,
configuration
.clientSecret); // Set client ID and client secret for authentication
else if (accessToken != null) {
else if (accessToken != null && !accessToken.isEmpty()) {
headers.set("Authorization", "Bearer " + accessToken);
}
return headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ protected String getPreAuthenticatedPrincipal(
LOGGER.debug("About to configure the REST Resource Template");
configureRestTemplate();

if (accessToken != null) {
if (accessToken != null
&& accessToken.getValue() != null
&& !accessToken.getValue().isEmpty()) {
LOGGER.debug("Setting the access token on the OAuth2ClientContext");
restTemplate.getOAuth2ClientContext().setAccessToken(accessToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
import org.springframework.security.oauth2.common.AuthenticationScheme;

/**
* Base abstract class for @Configuration classes providing needed beans from the Spring OAuth2
* mechanism.
* Base abstract class for @Configuration classes providing the necessary beans from the Spring
* OAuth2 mechanism.
*/
@Configuration
public abstract class OAuth2GeoStoreSecurityConfiguration implements ApplicationContextAware {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -90,7 +91,8 @@ public SessionToken refresh(String refreshToken, String accessToken) {

OAuth2AccessToken currentToken = retrieveAccessToken(accessToken);
Date expiresIn = currentToken.getExpiration();
if (refreshToken == null) refreshToken = getParameterValue(REFRESH_TOKEN_PARAM, request);
if (refreshToken == null || refreshToken.isEmpty())
refreshToken = getParameterValue(REFRESH_TOKEN_PARAM, request);
Date fiveMinutesFromNow = fiveMinutesFromNow();
SessionToken sessionToken = null;
OAuth2Configuration configuration = configuration();
Expand All @@ -100,15 +102,19 @@ public SessionToken refresh(String refreshToken, String accessToken) {
if (LOGGER.isDebugEnabled()) LOGGER.info("Going to refresh the token.");
try {
sessionToken = doRefresh(refreshToken, accessToken, configuration);
if (sessionToken == null)
sessionToken =
sessionToken(
accessToken, refreshToken, currentToken.getExpiration());
} catch (NullPointerException npe) {
LOGGER.error("Current configuration wasn't correctly initialized.");
}
}
}
if (sessionToken == null)
sessionToken = sessionToken(accessToken, refreshToken, currentToken.getExpiration());

request.setAttribute(
OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, sessionToken.getAccessToken());
request.setAttribute(
OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, sessionToken.getTokenType());

return sessionToken;
}

Expand All @@ -131,6 +137,7 @@ protected SessionToken doRefresh(
requestBody.add("grant_type", "refresh_token");
requestBody.add("refresh_token", refreshToken);
requestBody.add("client_secret", configuration.getClientSecret());
requestBody.add("client_id", configuration.getClientId());

HttpEntity<MultiValueMap<String, String>> requestEntity =
new HttpEntity<>(requestBody, headers);
Expand All @@ -151,9 +158,21 @@ protected SessionToken doRefresh(
LOGGER.error("Error trying to obtain a refresh token.", ex);
}

if (newToken != null && newToken.getValue() != null) {
if (refreshToken != null
&& accessToken != null
&& !refreshToken.isEmpty()
&& !accessToken.isEmpty()
&& newToken != null
&& newToken.getValue() != null
&& !newToken.getValue().isEmpty()) {
// update the Authentication
updateAuthToken(accessToken, newToken, refreshToken, configuration);
String newRefreshToken =
newToken.getRefreshToken() != null
&& newToken.getRefreshToken().getValue() != null
&& !newToken.getRefreshToken().getValue().isEmpty()
? newToken.getRefreshToken().getValue()
: refreshToken;
updateAuthToken(accessToken, newToken, newRefreshToken, configuration);
sessionToken =
sessionToken(newToken.getValue(), refreshToken, newToken.getExpiration());
} else if (accessToken != null) {
Expand Down Expand Up @@ -190,7 +209,7 @@ private static HttpHeaders getHttpHeaders(
configuration.clientId,
configuration
.clientSecret); // Set client ID and client secret for authentication
else if (accessToken != null) {
else if (accessToken != null && !accessToken.isEmpty()) {
headers.set("Authorization", "Bearer " + accessToken);
}
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // Set content type
Expand Down Expand Up @@ -321,7 +340,10 @@ public void doLogout(String sessionId) {

OAuth2Configuration configuration = configuration();
if (configuration != null && configuration.isEnabled()) {
if (token != null && accessToken != null) {
if (token != null
&& accessToken != null
&& !token.isEmpty()
&& !accessToken.isEmpty()) {
if (configuration.isGlobalLogoutEnabled())
doLogoutInternal(token, configuration, accessToken);
if (configuration.getRevokeEndpoint() != null) clearSession(restTemplate, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ protected String getPreAuthenticatedPrincipal(
}
// we must validate
String token = null;
if (accessToken != null) {
if (accessToken != null
&& !accessToken.isExpired()
&& accessToken.getValue() != null
&& !accessToken.getValue().isEmpty()) {
token = accessToken.getValue();
} else {
token = (String) req.getAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public OpenIdConnectFilter oidcOpenIdFilter() {
oidcTokenServices(),
oauth2RestTemplate(),
configuration(),
oidcCache(),
oAuth2Cache(),
openIdConnectBearerTokenValidator());
}

Expand All @@ -157,7 +157,7 @@ public OpenIdConnectTokenServices oidcTokenServices() {
}

@Bean
public TokenAuthenticationCache oidcCache() {
public TokenAuthenticationCache oAuth2Cache() {
return new TokenAuthenticationCache();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public OAuth2Configuration configuration() {
securityConfiguration.oidcTokenServices(),
restTemplate,
configuration,
securityConfiguration.oidcCache(),
securityConfiguration.oAuth2Cache(),
securityConfiguration.openIdConnectBearerTokenValidator());
}

Expand Down
Loading