use of com.linkedin.kafka.cruisecontrol.servlet.security.UserStoreAuthorizationService in project cruise-control by linkedin.
the class JwtLoginServiceTest method testRevalidateTokenFails.
@Test
public void testRevalidateTokenFails() throws Exception {
UserStore testUserStore = new UserStore();
testUserStore.addUser(TEST_USER, SecurityUtils.NO_CREDENTIAL, new String[] { "USER" });
Instant now = Instant.now();
TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER, now.plusSeconds(10).toEpochMilli());
Clock fixedClock = Clock.fixed(now, ZoneOffset.UTC);
JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), null, fixedClock);
SignedJWT jwtToken = SignedJWT.parse(tokenAndKeys.token());
HttpServletRequest request = mock(HttpServletRequest.class);
expect(request.getAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE)).andReturn(tokenAndKeys.token());
replay(request);
UserIdentity identity = loginService.login(TEST_USER, jwtToken, request);
verify(request);
assertNotNull(identity);
assertEquals(TEST_USER, identity.getUserPrincipal().getName());
loginService.setClock(Clock.offset(fixedClock, Duration.ofSeconds(20)));
assertFalse(loginService.validate(identity));
}
use of com.linkedin.kafka.cruisecontrol.servlet.security.UserStoreAuthorizationService in project cruise-control by linkedin.
the class JwtLoginServiceTest method testFailExpirationValidation.
@Test
public void testFailExpirationValidation() throws Exception {
UserStore testUserStore = new UserStore();
testUserStore.addUser(TEST_USER, SecurityUtils.NO_CREDENTIAL, new String[] { "USER" });
TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER, 1L);
JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), null);
SignedJWT jwtToken = SignedJWT.parse(tokenAndKeys.token());
HttpServletRequest request = mock(HttpServletRequest.class);
UserIdentity identity = loginService.login(TEST_USER, jwtToken, request);
assertNull(identity);
}
use of com.linkedin.kafka.cruisecontrol.servlet.security.UserStoreAuthorizationService in project cruise-control by linkedin.
the class JwtLoginServiceTest method testFailAudienceValidation.
@Test
public void testFailAudienceValidation() throws Exception {
UserStore testUserStore = new UserStore();
testUserStore.addUser(TEST_USER, SecurityUtils.NO_CREDENTIAL, new String[] { "USER" });
TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER, Arrays.asList("A", "B"));
JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), Arrays.asList("C", "D"));
SignedJWT jwtToken = SignedJWT.parse(tokenAndKeys.token());
HttpServletRequest request = mock(HttpServletRequest.class);
UserIdentity identity = loginService.login(TEST_USER, jwtToken, request);
assertNull(identity);
}
use of com.linkedin.kafka.cruisecontrol.servlet.security.UserStoreAuthorizationService in project cruise-control by linkedin.
the class JwtLoginServiceTest method testRevalidateTokenPasses.
@Test
public void testRevalidateTokenPasses() throws Exception {
UserStore testUserStore = new UserStore();
testUserStore.addUser(TEST_USER, SecurityUtils.NO_CREDENTIAL, new String[] { "USER" });
TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER);
JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), null);
SignedJWT jwtToken = SignedJWT.parse(tokenAndKeys.token());
HttpServletRequest request = mock(HttpServletRequest.class);
expect(request.getAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE)).andReturn(tokenAndKeys.token());
replay(request);
UserIdentity identity = loginService.login(TEST_USER, jwtToken, request);
verify(request);
assertNotNull(identity);
assertEquals(TEST_USER, identity.getUserPrincipal().getName());
assertTrue(loginService.validate(identity));
}
use of com.linkedin.kafka.cruisecontrol.servlet.security.UserStoreAuthorizationService in project cruise-control by linkedin.
the class JwtAuthenticatorTest method testFailedLoginWithUserNotFound.
@Test
public void testFailedLoginWithUserNotFound() throws Exception {
UserStore testUserStore = new UserStore();
testUserStore.addUser(TEST_USER_2, SecurityUtils.NO_CREDENTIAL, new String[] { USER_ROLE });
TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER);
JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), null);
Authenticator.AuthConfiguration configuration = mock(Authenticator.AuthConfiguration.class);
expect(configuration.getLoginService()).andReturn(loginService);
expect(configuration.getIdentityService()).andReturn(new DefaultIdentityService());
expect(configuration.isSessionRenewedOnAuthentication()).andReturn(true);
Request request = niceMock(Request.class);
expect(request.getMethod()).andReturn(HttpMethod.GET.asString());
expect(request.getHeader(HttpHeader.AUTHORIZATION.asString())).andReturn(null);
request.setAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE, tokenAndKeys.token());
expectLastCall().andVoid();
expect(request.getCookies()).andReturn(new Cookie[] { new Cookie(JWT_TOKEN, tokenAndKeys.token()) });
expect(request.getAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE)).andReturn(tokenAndKeys.token());
HttpServletResponse response = mock(HttpServletResponse.class);
response.setStatus(HttpStatus.UNAUTHORIZED_401);
expectLastCall().andVoid();
replay(configuration, request, response);
JwtAuthenticator authenticator = new JwtAuthenticator(TOKEN_PROVIDER, JWT_TOKEN);
authenticator.setConfiguration(configuration);
Authentication authentication = authenticator.validateRequest(request, response, true);
verify(configuration, request, response);
assertNotNull(authentication);
assertEquals(Authentication.SEND_FAILURE, authentication);
}
Aggregations