Created
December 13, 2018 15:07
-
-
Save eldermoraes/c6e1108660226e76353196bca5e6fb1d to your computer and use it in GitHub Desktop.
This file contains 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
@POST | |
@Consumes("application/x-www-form-urlencoded") | |
public Response login(@FormParam("login") String login, @FormParam("password") String password) { | |
//If user already logged, then get it token | |
Optional<String> key = authSession.getToken(login, password); | |
if (key.isPresent()) { | |
return Response.ok(key.get()).build(); | |
} | |
//Validate the login and password on data source | |
if (!authSession.getDataSource().containsKey(login) | |
|| !authSession.getDataSource() | |
.get(login) | |
.getPassword() | |
.equals(password)) { | |
return Response.status(Response.Status.UNAUTHORIZED).build(); | |
} | |
String token = TokenUtils.generateToken(); | |
//Persiste the information of authentication on the AuthSession. | |
authSession.putAuthenticated(token, new Auth(login, password, new Date())); | |
return Response.ok(token).status(Response.Status.CREATED).build(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment