use of org.springframework.security.Authentication in project gocd by gocd.
the class BasicAuthenticationFilterTest method shouldConvey_itsBasicProcessingFilter.
@Test
public void shouldConvey_itsBasicProcessingFilter() throws IOException, ServletException {
BasicAuthenticationFilter filter = new BasicAuthenticationFilter(localizer);
final Boolean[] hadBasicMarkOnInsideAuthenticationManager = new Boolean[] { false };
filter.setAuthenticationManager(new AuthenticationManager() {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
hadBasicMarkOnInsideAuthenticationManager[0] = BasicAuthenticationFilter.isProcessingBasicAuth();
return new UsernamePasswordAuthenticationToken("school-principal", "u can be principal if you know this!");
}
});
assertThat(BasicAuthenticationFilter.isProcessingBasicAuth(), is(false));
MockHttpServletRequest httpRequest = new MockHttpServletRequest();
httpRequest.addHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString("loser:boozer".getBytes()));
filter.doFilterHttp(httpRequest, new MockHttpServletResponse(), new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
}
});
assertThat(BasicAuthenticationFilter.isProcessingBasicAuth(), is(false));
assertThat(hadBasicMarkOnInsideAuthenticationManager[0], is(true));
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class ReAuthenticationFilterTest method shouldReAuthenticateIfReAuthTimeIntervalHasElapsed.
@Test
public void shouldReAuthenticateIfReAuthTimeIntervalHasElapsed() throws IOException, ServletException {
long currentTimeMillis = DateTimeUtils.currentTimeMillis();
long minuteBack = DateTimeUtils.currentTimeMillis() - 60000;
Authentication authentication = setupAuthentication(true);
when(timeProvider.currentTimeMillis()).thenReturn(currentTimeMillis);
when(systemEnvironment.isReAuthenticationEnabled()).thenReturn(true);
when(systemEnvironment.getReAuthenticationTimeInterval()).thenReturn(55000L);
when(session.getAttribute(LAST_REAUTHENICATION_CHECK_TIME)).thenReturn(minuteBack);
filter.doFilterHttp(request, response, filterChain);
verify(session).setAttribute(LAST_REAUTHENICATION_CHECK_TIME, currentTimeMillis);
verify(filterChain).doFilter(request, response);
verifyNoMoreInteractions(filterChain);
assertFalse(authentication.isAuthenticated());
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class ReAuthenticationFilterTest method shouldContinueWithChainAndReturnForAuthenticatedSessionWithoutLastAuthenticationTimeStamp.
@Test
public void shouldContinueWithChainAndReturnForAuthenticatedSessionWithoutLastAuthenticationTimeStamp() throws IOException, ServletException {
long currentTimeMillis = DateTimeUtils.currentTimeMillis();
Authentication authentication = setupAuthentication(true);
when(systemEnvironment.isReAuthenticationEnabled()).thenReturn(true);
when(timeProvider.currentTimeMillis()).thenReturn(currentTimeMillis);
filter.doFilterHttp(request, response, filterChain);
verify(session).setAttribute(LAST_REAUTHENICATION_CHECK_TIME, currentTimeMillis);
verify(filterChain).doFilter(request, response);
verifyNoMoreInteractions(filterChain);
assertTrue(authentication.isAuthenticated());
}
use of org.springframework.security.Authentication in project gocd by gocd.
the class RemoveAdminPermissionFilterIntegrationTest method testShouldReAuthenticateOnlyOnceAfterConfigChange.
@Test
public void testShouldReAuthenticateOnlyOnceAfterConfigChange() throws IOException, ServletException {
goConfigService.security().securityAuthConfigs().add(new SecurityAuthConfig("github", "cd.go.authorization.github"));
goConfigService.security().addRole(new PluginRoleConfig("spacetiger", "github"));
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);
pluginRoleService.invalidateRolesFor("cd.go.authorization.github");
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 RemoveAdminPermissionFilterIntegrationTest method testShouldReAuthenticateIffSecurityConfigChange.
@Test
public void testShouldReAuthenticateIffSecurityConfigChange() throws IOException, ServletException {
Authentication authentication = setupAuthentication();
when(session.getAttribute(RemoveAdminPermissionFilter.SECURITY_CONFIG_LAST_CHANGE)).thenReturn(0L).thenReturn(0L).thenReturn(100L);
RemoveAdminPermissionFilter filter = new RemoveAdminPermissionFilter(goConfigService, timeProvider, pluginRoleService);
filter.initialize();
filter.doFilterHttp(request, response, chain);
assertThat(authentication.isAuthenticated(), is(true));
// This changes the security config
turnOnSecurity("pavan");
filter.doFilterHttp(request, response, chain);
assertThat(authentication.isAuthenticated(), is(false));
authentication.setAuthenticated(true);
// This changes something else
modifyArtifactRoot();
filter.doFilterHttp(request, response, chain);
assertThat(authentication.isAuthenticated(), is(true));
}
Aggregations