Created
July 26, 2015 08:55
-
-
Save koert/bd77993f6fb18acca5f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Service to authenticate user with functional style. | |
| * @author Koert Zeilstra | |
| */ | |
| @Path("/authentication") | |
| @ManagedBean | |
| public class LoginService { | |
| @Inject | |
| private AuthenticationRepository authenticationRepository; | |
| @Path("/login") | |
| @POST | |
| @Consumes("application/x-www-form-urlencoded") | |
| public Response login(@FormParam("username") String userName, @FormParam("password") String password) { | |
| Optional<AuthenticationToken> authentication = authenticationRepository.authenticateUser(userName, password); | |
| Response response = authentication.map(token -> Response.ok(createResource(token)).build()) | |
| .orElse(Response.status(Response.Status.NOT_ACCEPTABLE).build()); | |
| return response; | |
| } | |
| private AuthTokenResource createResource(AuthenticationToken token) { | |
| AuthTokenResource resource = new AuthTokenResource(); | |
| resource.token = token.getId(); | |
| resource.expiration = token.getExpiration(); | |
| return resource; | |
| } | |
| @XmlRootElement(name = "site") | |
| public static class AuthTokenResource { | |
| public LocalDateTime expiration; | |
| public String token; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment