@Test public void testLoginInvalid() { Response response = accountResource.login(""); assertThat(response.getStatus()).isEqualTo(401); accountResource.login("testing 124"); assertThat(response.getStatus()).isEqualTo(401); }
@POST @Path("/login") public Response login(@HeaderParam("Authorization") String authHeader) { if (StringUtils.isBlank(authHeader)) { return Response.status(Response.Status.UNAUTHORIZED).build(); } Optional<Pair<String, String>> auth = processAuthHeader(authHeader); if (!auth.isPresent()) { return Response.status(Response.Status.UNAUTHORIZED).build(); } String username = auth.get().getKey(); String password = auth.get().getValue(); return accountService.authenticate(username, password).map(account -> { authenticatedAccount.setAuthenticatedAccount(account); authenticatedAccount.setAuthenticatedUsername(username); NewCookie cookie = generateCookie(COOKIE_MAX_AGE, authHeader); return Response.ok().cookie(cookie).build(); }).orElse(Response.status(Response.Status.UNAUTHORIZED).build()); }
AccountResourceImpl { @POST @Path("/login") public Response login(@HeaderParam("Authorization") String authHeader) { if (StringUtils.isBlank(authHeader)) { return Response.status(Response.Status.UNAUTHORIZED).build(); } Optional<Pair<String, String>> auth = processAuthHeader(authHeader); if (!auth.isPresent()) { return Response.status(Response.Status.UNAUTHORIZED).build(); } String username = auth.get().getKey(); String password = auth.get().getValue(); return accountService.authenticate(username, password).map(account -> { authenticatedAccount.setAuthenticatedAccount(account); authenticatedAccount.setAuthenticatedUsername(username); NewCookie cookie = generateCookie(COOKIE_MAX_AGE, authHeader); return Response.ok().cookie(cookie).build(); }).orElse(Response.status(Response.Status.UNAUTHORIZED).build()); } }
AccountResourceImpl { @POST @Path("/login") public Response login(@HeaderParam("Authorization") String authHeader) { if (StringUtils.isBlank(authHeader)) { return Response.status(Response.Status.UNAUTHORIZED).build(); } Optional<Pair<String, String>> auth = processAuthHeader(authHeader); if (!auth.isPresent()) { return Response.status(Response.Status.UNAUTHORIZED).build(); } String username = auth.get().getKey(); String password = auth.get().getValue(); return accountService.authenticate(username, password).map(account -> { authenticatedAccount.setAuthenticatedAccount(account); authenticatedAccount.setAuthenticatedUsername(username); NewCookie cookie = generateCookie(COOKIE_MAX_AGE, authHeader); return Response.ok().cookie(cookie).build(); }).orElse(Response.status(Response.Status.UNAUTHORIZED).build()); } @Inject  AccountResourceImpl(AccountService accountService,
        AuthenticatedAccount authenticatedAccount,
        Event<AccountCreated> accountCreatedEvent); @SuppressWarnings("unused")  AccountResourceImpl(); }
AccountResourceImpl { @POST @Path("/login") public Response login(@HeaderParam("Authorization") String authHeader) { if (StringUtils.isBlank(authHeader)) { return Response.status(Response.Status.UNAUTHORIZED).build(); } Optional<Pair<String, String>> auth = processAuthHeader(authHeader); if (!auth.isPresent()) { return Response.status(Response.Status.UNAUTHORIZED).build(); } String username = auth.get().getKey(); String password = auth.get().getValue(); return accountService.authenticate(username, password).map(account -> { authenticatedAccount.setAuthenticatedAccount(account); authenticatedAccount.setAuthenticatedUsername(username); NewCookie cookie = generateCookie(COOKIE_MAX_AGE, authHeader); return Response.ok().cookie(cookie).build(); }).orElse(Response.status(Response.Status.UNAUTHORIZED).build()); } @Inject  AccountResourceImpl(AccountService accountService,
        AuthenticatedAccount authenticatedAccount,
        Event<AccountCreated> accountCreatedEvent); @SuppressWarnings("unused")  AccountResourceImpl(); @GET @CheckRole("admin") List<AccountDto> getAllAccounts(@QueryParam("enabledOnly") boolean enabledOnly); @ValidatePayload(AccountDto.class) @POST @CheckRole("admin") Response registerNewAccount(AccountDto accountDto); @ValidatePayload(AccountDto.class) @PUT @CheckRole("admin") Response updateAccount(AccountDto accountDto); @POST @Path("/login") Response login(@HeaderParam("Authorization") String authHeader); @POST @Path("/logout") Response logout(); }
AccountResourceImpl { @POST @Path("/login") public Response login(@HeaderParam("Authorization") String authHeader) { if (StringUtils.isBlank(authHeader)) { return Response.status(Response.Status.UNAUTHORIZED).build(); } Optional<Pair<String, String>> auth = processAuthHeader(authHeader); if (!auth.isPresent()) { return Response.status(Response.Status.UNAUTHORIZED).build(); } String username = auth.get().getKey(); String password = auth.get().getValue(); return accountService.authenticate(username, password).map(account -> { authenticatedAccount.setAuthenticatedAccount(account); authenticatedAccount.setAuthenticatedUsername(username); NewCookie cookie = generateCookie(COOKIE_MAX_AGE, authHeader); return Response.ok().cookie(cookie).build(); }).orElse(Response.status(Response.Status.UNAUTHORIZED).build()); } @Inject  AccountResourceImpl(AccountService accountService,
        AuthenticatedAccount authenticatedAccount,
        Event<AccountCreated> accountCreatedEvent); @SuppressWarnings("unused")  AccountResourceImpl(); @GET @CheckRole("admin") List<AccountDto> getAllAccounts(@QueryParam("enabledOnly") boolean enabledOnly); @ValidatePayload(AccountDto.class) @POST @CheckRole("admin") Response registerNewAccount(AccountDto accountDto); @ValidatePayload(AccountDto.class) @PUT @CheckRole("admin") Response updateAccount(AccountDto accountDto); @POST @Path("/login") Response login(@HeaderParam("Authorization") String authHeader); @POST @Path("/logout") Response logout(); final static String AUTH_HEADER; }