Skip to content

Instantly share code, notes, and snippets.

@koert
Created July 26, 2015 08:55
Show Gist options
  • Select an option

  • Save koert/bd77993f6fb18acca5f5 to your computer and use it in GitHub Desktop.

Select an option

Save koert/bd77993f6fb18acca5f5 to your computer and use it in GitHub Desktop.
/**
* 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