class 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 public  SessionLoginResource(@Readonly Authenticator<BasicCredentials, User> userAuthenticator,
      AuthenticatedEncryptedCookieFactory cookieFactory);

}