use of org.springframework.security.Authentication in project gocd by gocd.
the class RemoveAdminPermissionFilterIntegrationTest method testShouldReAuthenticateOnlyOnceAfterAuthorizationPluginUnloaded.
@Test
public void testShouldReAuthenticateOnlyOnceAfterAuthorizationPluginUnloaded() throws IOException, ServletException {
Authentication authentication = setupAuthentication();
when(session.getAttribute(SECURITY_CONFIG_LAST_CHANGE)).thenReturn(0L).thenReturn(0L).thenReturn(100L);
RemoveAdminPermissionFilter filter = new RemoveAdminPermissionFilter(goConfigService, timeProvider, pluginRoleService);
filter.initialize();
// good initial state
assertThat(authentication.isAuthenticated(), is(true));
filter.doFilterHttp(request, response, chain);
turnOnSecurity("pavan");
assertThat(authentication.isAuthenticated(), is(true));
filter.doFilterHttp(request, response, chain);
assertThat(authentication.isAuthenticated(), is(false));
authentication.setAuthenticated(true);
filter.doFilterHttp(request, response, chain);
assertThat(authentication.isAuthenticated(), is(true));
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class OauthAuthenticationFilter method doFilterHttp.
protected void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
// Token token="ACCESS_TOKEN"
String header = request.getHeader(AUTHORIZATION);
if (header != null) {
logger.debug("Oauth authorization header: " + header);
Matcher matcher = OAUTH_TOKEN_PATTERN.matcher(header);
if (matcher.matches()) {
String token = matcher.group(1);
OauthAuthenticationToken authenticationToken = new OauthAuthenticationToken(token);
try {
Authentication authResult = authenticationManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authResult);
} catch (AuthenticationException e) {
logger.debug("Oauth authentication request for token: " + token, e);
SecurityContextHolder.getContext().setAuthentication(null);
}
}
}
chain.doFilter(request, response);
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class PreAuthenticatedRequestsProcessingFilter method attemptAuthentication.
@Override
public Authentication attemptAuthentication(HttpServletRequest request) throws AuthenticationException {
PreAuthenticatedAuthenticationToken authRequest = new PreAuthenticatedAuthenticationToken(null, fetchAuthorizationServerAccessToken(request), pluginId(request));
Authentication authResult = this.getAuthenticationManager().authenticate(authRequest);
return authResult;
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class ReAuthenticationFilter method doFilterHttp.
@Override
protected void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!systemEnvironment.isReAuthenticationEnabled() || authentication == null) {
chain.doFilter(request, response);
return;
}
synchronized (request.getSession().getId().intern()) {
Long lastAuthenticationTime = (Long) request.getSession().getAttribute(LAST_REAUTHENICATION_CHECK_TIME);
if (lastAuthenticationTime == null) {
request.getSession().setAttribute(LAST_REAUTHENICATION_CHECK_TIME, timeProvider.currentTimeMillis());
} else if (forceReAuthentication(lastAuthenticationTime)) {
request.getSession().setAttribute(LAST_REAUTHENICATION_CHECK_TIME, timeProvider.currentTimeMillis());
authentication.setAuthenticated(false);
}
}
chain.doFilter(request, response);
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class RemoveAdminPermissionFilter method doFilterHttp.
public void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
chain.doFilter(request, response);
return;
}
synchronized (request.getRequestedSessionId().intern()) {
// This is so that the volatile variable is accessed only once.
long localCopyOfLastChangedTime = lastChangedTime;
Long previousLastChangedTime = (Long) request.getSession().getAttribute(SECURITY_CONFIG_LAST_CHANGE);
if (previousLastChangedTime == null) {
request.getSession().setAttribute(SECURITY_CONFIG_LAST_CHANGE, localCopyOfLastChangedTime);
} else if (previousLastChangedTime < localCopyOfLastChangedTime) {
request.getSession().setAttribute(SECURITY_CONFIG_LAST_CHANGE, localCopyOfLastChangedTime);
authentication.setAuthenticated(false);
}
}
chain.doFilter(request, response);
}
Aggregations