Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

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