Skip to content

Instantly share code, notes, and snippets.

@eldermoraes
Created December 13, 2018 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldermoraes/c6e1108660226e76353196bca5e6fb1d to your computer and use it in GitHub Desktop.
Save eldermoraes/c6e1108660226e76353196bca5e6fb1d to your computer and use it in GitHub Desktop.
@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