Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2018 15:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
public class AuthenticationResource {
private AuthSession authSession;
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();
//Validade login and password on data source
if (!authSession.getDataSource().containsKey(login)
|| !authSession.getDataSource()
.equals(password)) {
return Response.status(Response.Status.UNAUTHORIZED).build();
String token = TokenUtils.generateToken();
//Persist the information of authentication on AuthSession
authSession.putAuthenticated(token, new Auth(login, password, new Date()));
return Response.ok(token).build();
public Response checkAuthentication(@PathParam("token") String token) {
if (authSession.getAuthenticated().containsKey(token)) {
return Response.ok().build();
return Response.status(Response.Status.UNAUTHORIZED).build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment