authorization-utils/src/main/java/org/gcube/common/authorization/utils/secret/Secret.java

79 lines
1.7 KiB
Java

package org.gcube.common.authorization.utils.secret;
import java.util.Map;
import java.util.Objects;
import org.gcube.common.authorization.library.exception.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public abstract class Secret implements Comparable<Secret> {
protected static final Logger logger = LoggerFactory.getLogger(Secret.class);
protected int priority;
protected String token;
protected void check(String token) throws AuthorizationException {
if(token == null) {
throw new AuthorizationException("token cannot be null");
}
if(token.compareTo("")==0) {
throw new AuthorizationException("token cannot be an empty string");
}
}
protected Secret(int priority, String token) {
this.priority = priority;
check(token);
this.token = token;
}
public String getToken() {
return token;
}
public abstract void set() throws Exception;
public abstract String getContext() throws Exception;
public abstract String getUsername() throws Exception;
public abstract Map<String,String> getHTTPAuthorizationHeaders();
@Override
public int hashCode() {
return Objects.hash(priority, token);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Secret other = (Secret) obj;
return priority == other.priority && Objects.equals(token, other.token);
}
@Override
public int compareTo(Secret obj) {
if (this == obj) {
return 0;
}
if (obj == null) {
return priority;
}
if (getClass() != obj.getClass()) {
return priority;
}
return token.compareTo(obj.token);
}
}