@Test public void goodCredentialsSetsCookie() throws Exception { User user = User.named("goodUser"); when(ldapAuthenticator.authenticate(goodCredentials)).thenReturn(Optional.of(user)); Response response = sessionLoginResource.login(LoginRequest.from("good", "credentials".toCharArray())); assertThat(response.getStatus()).isEqualTo(200); Map<String, NewCookie> responseCookies = response.getCookies(); assertThat(responseCookies).hasSize(1).containsOnlyKeys("session"); User authUser = cookieAuthenticator.authenticate(responseCookies.get("session")) .orElseThrow(RuntimeException::new); assertThat(authUser).isEqualTo(user); }
@Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response login(@Valid LoginRequest request) { String username = request.username(); String password = String.copyValueOf(request.password()); Optional<User> optionalUser = Optional.empty(); try { optionalUser = userAuthenticator.authenticate(new BasicCredentials(username, password)); } catch (AuthenticationException e) { logger.warn("User authenticator threw something weird.", e); } if (!optionalUser.isPresent()) { logger.info("User authentication failed at login for {}", username); throw new NotAuthorizedException(""); } logger.info("User logged in: {}", username); Response.ResponseBuilder response = Response.ok(); cookiesForUser(optionalUser.get()) .forEach(response::cookie); return response.build(); }
SessionLoginResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response login(@Valid LoginRequest request) { String username = request.username(); String password = String.copyValueOf(request.password()); Optional<User> optionalUser = Optional.empty(); try { optionalUser = userAuthenticator.authenticate(new BasicCredentials(username, password)); } catch (AuthenticationException e) { logger.warn("User authenticator threw something weird.", e); } if (!optionalUser.isPresent()) { logger.info("User authentication failed at login for {}", username); throw new NotAuthorizedException(""); } logger.info("User logged in: {}", username); Response.ResponseBuilder response = Response.ok(); cookiesForUser(optionalUser.get()) .forEach(response::cookie); return response.build(); } }
SessionLoginResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response login(@Valid LoginRequest request) { String username = request.username(); String password = String.copyValueOf(request.password()); Optional<User> optionalUser = Optional.empty(); try { optionalUser = userAuthenticator.authenticate(new BasicCredentials(username, password)); } catch (AuthenticationException e) { logger.warn("User authenticator threw something weird.", e); } if (!optionalUser.isPresent()) { logger.info("User authentication failed at login for {}", username); throw new NotAuthorizedException(""); } logger.info("User logged in: {}", username); Response.ResponseBuilder response = Response.ok(); cookiesForUser(optionalUser.get()) .forEach(response::cookie); return response.build(); } @Inject  SessionLoginResource(@Readonly Authenticator<BasicCredentials, User> userAuthenticator,
      AuthenticatedEncryptedCookieFactory cookieFactory); }
SessionLoginResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response login(@Valid LoginRequest request) { String username = request.username(); String password = String.copyValueOf(request.password()); Optional<User> optionalUser = Optional.empty(); try { optionalUser = userAuthenticator.authenticate(new BasicCredentials(username, password)); } catch (AuthenticationException e) { logger.warn("User authenticator threw something weird.", e); } if (!optionalUser.isPresent()) { logger.info("User authentication failed at login for {}", username); throw new NotAuthorizedException(""); } logger.info("User logged in: {}", username); Response.ResponseBuilder response = Response.ok(); cookiesForUser(optionalUser.get()) .forEach(response::cookie); return response.build(); } @Inject  SessionLoginResource(@Readonly Authenticator<BasicCredentials, User> userAuthenticator,
      AuthenticatedEncryptedCookieFactory cookieFactory); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) Response login(@Valid LoginRequest request); ImmutableList<NewCookie> cookiesForUser(User user); }
SessionLoginResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) public Response login(@Valid LoginRequest request) { String username = request.username(); String password = String.copyValueOf(request.password()); Optional<User> optionalUser = Optional.empty(); try { optionalUser = userAuthenticator.authenticate(new BasicCredentials(username, password)); } catch (AuthenticationException e) { logger.warn("User authenticator threw something weird.", e); } if (!optionalUser.isPresent()) { logger.info("User authentication failed at login for {}", username); throw new NotAuthorizedException(""); } logger.info("User logged in: {}", username); Response.ResponseBuilder response = Response.ok(); cookiesForUser(optionalUser.get()) .forEach(response::cookie); return response.build(); } @Inject  SessionLoginResource(@Readonly Authenticator<BasicCredentials, User> userAuthenticator,
      AuthenticatedEncryptedCookieFactory cookieFactory); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) Response login(@Valid LoginRequest request); ImmutableList<NewCookie> cookiesForUser(User user);  }