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

121 lines
2.9 KiB
Java
Raw Permalink Normal View History

2021-11-30 17:39:26 +01:00
package org.gcube.common.authorization.utils.secret;
import java.util.Map;
import java.util.Objects;
2021-12-01 16:22:36 +01:00
import org.gcube.common.authorization.library.ClientType;
2021-11-30 17:39:26 +01:00
import org.gcube.common.authorization.library.exception.AuthorizationException;
2021-12-01 11:48:22 +01:00
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.ClientInfo;
import org.gcube.common.authorization.library.utils.Caller;
2021-12-07 13:50:39 +01:00
import org.gcube.common.authorization.utils.user.User;
2021-12-01 11:48:22 +01:00
import org.gcube.common.scope.api.ScopeProvider;
2021-11-30 17:39:26 +01:00
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;
2021-12-07 13:50:39 +01:00
protected User user;
2021-11-30 17:39:26 +01:00
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;
}
2021-12-01 11:48:22 +01:00
public void set() throws Exception {
setToken();
2021-12-01 17:31:16 +01:00
setAuthorizationProvider();
2021-12-01 11:48:22 +01:00
ScopeProvider.instance.set(getContext());
}
public abstract void setToken() throws Exception;
2021-12-02 13:16:03 +01:00
public abstract void resetToken() throws Exception;
2021-12-01 11:48:22 +01:00
public abstract ClientInfo getClientInfo() throws Exception;
public abstract Caller getCaller() throws Exception;
2021-12-01 17:12:52 +01:00
public void setAuthorizationProvider() throws Exception {
Caller caller = getCaller();
AuthorizationProvider.instance.set(caller);
}
2021-11-30 17:39:26 +01:00
public abstract String getContext() throws Exception;
2021-12-01 16:22:36 +01:00
public String getUsername() throws Exception {
return getClientInfo().getId();
}
public boolean isApplication() throws Exception {
return getClientInfo().getType() == ClientType.EXTERNALSERVICE;
}
2021-11-30 17:39:26 +01:00
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);
}
2021-12-01 11:48:22 +01:00
2021-12-02 13:16:03 +01:00
public void reset() throws Exception {
resetToken();
AuthorizationProvider.instance.reset();
ScopeProvider.instance.reset();
}
2021-12-06 17:43:18 +01:00
public abstract boolean isExpired();
2021-12-01 11:48:22 +01:00
2021-12-06 17:43:18 +01:00
public abstract boolean isRefreshable();
2021-12-07 13:50:39 +01:00
public abstract User getUser();
2021-11-30 17:39:26 +01:00
}