Created
July 26, 2015 08:54
-
-
Save koert/e3918b9b6b72065d4bf4 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 classical if-null checking. | |
| * @author Koert Zeilstra | |
| */ | |
| @Path("/authentication") | |
| @ManagedBean | |
| public class LoginService2 { | |
| @Inject | |
| private AuthenticationRepository authenticationRepository; | |
| @Path("/login") | |
| @POST | |
| @Consumes("application/x-www-form-urlencoded") | |
| public Response login(@FormParam("username") String userName, @FormParam("password") String password) { | |
| Response response = null; | |
| AuthenticationToken authentication = authenticationRepository.authenticateUser2(userName, password); | |
| if (authentication == null) { | |
| response = Response.status(Response.Status.NOT_ACCEPTABLE).build(); | |
| } else { | |
| AuthenticationToken authenticationToken = authenticationRepository.createAuthenticationToken(userName); | |
| response = Response.ok(createResource(authenticationToken)).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