Jonathan Bernard 83a0f7275c Implemented token-based API authentication.
Replaced the ApiKey concept with ephemeral tokens. Users and apps obtain a
token by authenticating the user credentials (to be implemented). The service
then generates a temporary token that is stored by the client and sent with
every request using the `Authorization-Token` header. The server verifies this
token to recognize and authenticate the request. With an authenticated user,
the server can use the user's role to authorize requests.

This implementation uses JSR 250 SecurityContext and security annotations.
2015-03-02 21:20:25 -06:00

36 lines
977 B
Java

package com.jdbernard.nlsongs.rest.security;
import java.security.Principal;
import com.jdbernard.nlsongs.model.Token;
public class TokenPrincipal implements Principal {
public final Token token;
public TokenPrincipal(Token token) { this.token = token; }
@Override
public boolean equals(Object thatObj) {
if (thatObj == null) return false;
if (!(thatObj instanceof TokenPrincipal)) return false;
TokenPrincipal that = (TokenPrincipal) thatObj;
if (this.token == null) { return that.token == null; }
else { return this.token.equals(that.token); } }
@Override
public String getName() {
if (this.token == null) return null;
else return this.token.getUser().getUsername(); }
@Override
public int hashCode() {
if (this.token == null) return 0;
return this.token.getUser().getUsername().hashCode(); }
@Override
public String toString() { return getName(); }
}