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

OP-14834: invalidate session and throw SAMLAuthenticationException #209

Merged
merged 19 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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 @@ -24,6 +24,7 @@ import com.netflix.spinnaker.gate.security.SpinnakerAuthConfig
import com.netflix.spinnaker.gate.services.PermissionService
import com.netflix.spinnaker.kork.core.RetrySupport
import com.netflix.spinnaker.security.User
import com.opsmx.spinnaker.gate.security.saml.SamlAuthTokenUpdateFilter
import groovy.util.logging.Slf4j
import org.opensaml.saml2.core.Assertion
import org.opensaml.saml2.core.Attribute
Expand All @@ -43,11 +44,12 @@ import org.springframework.security.config.annotation.web.configuration.WebSecur
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.security.extensions.saml2.config.SAMLConfigurer
import org.springframework.security.saml.websso.WebSSOProfileConsumerImpl
import org.springframework.security.saml.SAMLCredential
import org.springframework.security.saml.userdetails.SAMLUserDetailsService
import org.springframework.security.saml.websso.WebSSOProfileConsumerImpl
import org.springframework.security.web.authentication.RememberMeServices
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter
import org.springframework.session.web.http.DefaultCookieSerializer
import org.springframework.stereotype.Component

Expand Down Expand Up @@ -168,7 +170,9 @@ class SamlSsoConfig extends WebSecurityConfigurerAdapter {
.keyPassword(samlSecurityConfigProperties.keyStorePassword)

saml.init(http)

SamlAuthTokenUpdateFilter authTokenUpdateFilter = new SamlAuthTokenUpdateFilter()
http.addFilterAfter(authTokenUpdateFilter,
BasicAuthenticationFilter.class)
// @formatter:on

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.opsmx.spinnaker.gate.security.saml;

import org.springframework.security.core.AuthenticationException;

public class SAMLAuthenticationException extends AuthenticationException {
public SAMLAuthenticationException(String msg) {
super(msg);
}

public SAMLAuthenticationException(String msg, Throwable t) {
super(msg, t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.opsmx.spinnaker.gate.security.saml;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.providers.ExpiringUsernameAuthenticationToken;
import org.springframework.web.filter.GenericFilterBean;

public class SamlAuthTokenUpdateFilter extends GenericFilterBean {

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
logger.debug("SamlAuthTokenUpdateFilter doFilter started");
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

logger.debug("Previously Authenticated is : " + authentication);
if (authentication instanceof ExpiringUsernameAuthenticationToken
&& !authentication.isAuthenticated()) {
if (logger.isDebugEnabled()) {
logger.debug(
"Previously Authenticated token Expired; redirecting to authentication entry point.");
}

HttpSession session = request.getSession();
if (session != null) {
session.invalidate();
}
throw new SAMLAuthenticationException("Previously Authenticated token Expired");
}
logger.debug("SamlAuthTokenUpdateFilter doFilter ended");

chain.doFilter(request, response);
}
}