Skip to content

Commit 6ae5cde

Browse files
committed
Fix #25862: allow using SpEL in openid.logout-url
This is useful to provide the `id_token_hint` parameter.
1 parent 7087ab2 commit 6ae5cde

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/main/java/eu/openanalytics/containerproxy/auth/IAuthenticationBackend.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.AuthorizedUrl;
2828

2929
import eu.openanalytics.containerproxy.model.spec.ContainerSpec;
30+
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
31+
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
3032

3133
public interface IAuthenticationBackend {
3234

@@ -39,7 +41,7 @@ public interface IAuthenticationBackend {
3941
* Return true if this authentication backend supports authorization.
4042
* In this context, authorization means the separation of permission levels
4143
* via groups.
42-
*
44+
*
4345
* If there is no authorization, all users have the same (administrator) permissions.
4446
*/
4547
public boolean hasAuthorization();
@@ -70,5 +72,10 @@ public default void customizeContainer(ContainerSpec spec) {
7072
public default void customizeContainerEnv(Map<String, String> env) {
7173
// Default: do nothing.
7274
}
73-
75+
76+
public default LogoutSuccessHandler getLogoutSuccessHandler() {
77+
SimpleUrlLogoutSuccessHandler urlLogoutHandler = new SimpleUrlLogoutSuccessHandler();
78+
urlLogoutHandler.setDefaultTargetUrl(getLogoutSuccessURL());
79+
return urlLogoutHandler;
80+
}
7481
}

src/main/java/eu/openanalytics/containerproxy/auth/impl/OpenIDAuthenticationBackend.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import eu.openanalytics.containerproxy.auth.IAuthenticationBackend;
2424
import eu.openanalytics.containerproxy.security.FixedDefaultOAuth2AuthorizationRequestResolver;
25+
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionContext;
26+
import eu.openanalytics.containerproxy.spec.expression.SpecExpressionResolver;
2527
import eu.openanalytics.containerproxy.util.SessionHelper;
2628
import net.minidev.json.JSONArray;
2729
import net.minidev.json.parser.JSONParser;
@@ -54,6 +56,7 @@
5456
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
5557
import org.springframework.security.oauth2.core.oidc.user.OidcUserAuthority;
5658
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
59+
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
5760
import org.springframework.web.context.request.RequestContextHolder;
5861
import org.springframework.web.context.request.ServletRequestAttributes;
5962
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@@ -150,6 +153,17 @@ public void customizeContainerEnv(Map<String, String> env) {
150153

151154
env.put(ENV_TOKEN_NAME, client.getAccessToken().getTokenValue());
152155
}
156+
157+
@Inject
158+
private SpecExpressionResolver specExpressionResolver;
159+
160+
@Override
161+
public LogoutSuccessHandler getLogoutSuccessHandler() {
162+
return (request, response, authentication) -> {
163+
SpecExpressionContext context = SpecExpressionContext.create(authentication.getPrincipal(), authentication.getCredentials());
164+
response.sendRedirect(specExpressionResolver.evaluateToString(getLogoutSuccessURL(), context));
165+
};
166+
}
153167

154168
protected ClientRegistrationRepository createClientRepo() {
155169
Set<String> scopes = new HashSet<>();

src/main/java/eu/openanalytics/containerproxy/security/WebSecurityConfig.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
3939
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
4040
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
41+
import org.springframework.security.core.Authentication;
4142
import org.springframework.security.web.access.AccessDeniedHandler;
4243
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
44+
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
45+
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
4346
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
4447
import org.springframework.security.web.csrf.MissingCsrfTokenException;
4548
import org.springframework.security.web.header.writers.StaticHeadersWriter;
@@ -166,8 +169,8 @@ public void handle(HttpServletRequest request, HttpServletResponse response, Acc
166169
// important: set the next option after logoutUrl because it would otherwise get overwritten
167170
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
168171
.addLogoutHandler(logoutHandler)
169-
.logoutSuccessUrl(auth.getLogoutSuccessURL());
170-
172+
.logoutSuccessHandler(auth.getLogoutSuccessHandler());
173+
171174
// Enable basic auth for RESTful calls when APISecurityConfig is not enabled.
172175
http.addFilter(new BasicAuthenticationFilter(super.authenticationManagerBean()));
173176
}

0 commit comments

Comments
 (0)