authorization-utils/src/main/java/org/gcube/common/authorization/utils/manager/SecretHolder.java

107 lines
2.0 KiB
Java
Raw Permalink Normal View History

2021-11-30 17:39:26 +01:00
package org.gcube.common.authorization.utils.manager;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;
import org.gcube.common.authorization.utils.secret.Secret;
2021-12-07 13:50:39 +01:00
import org.gcube.common.authorization.utils.user.User;
2021-12-01 17:12:52 +01:00
import org.gcube.common.scope.api.ScopeProvider;
2021-11-30 17:39:26 +01:00
2021-12-01 17:12:52 +01:00
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-11-30 17:39:26 +01:00
public class SecretHolder {
2021-12-01 17:12:52 +01:00
private SortedSet<Secret> secrets;
2021-11-30 17:39:26 +01:00
public SecretHolder() {
2021-12-01 17:12:52 +01:00
this.secrets = new TreeSet<Secret>();
2021-11-30 17:39:26 +01:00
}
2021-12-01 17:12:52 +01:00
public SecretHolder(Secret secret) {
this.secrets = new TreeSet<Secret>();
addSecret(secret);
2021-11-30 17:39:26 +01:00
}
2021-12-01 17:12:52 +01:00
public SecretHolder(Collection<Secret> secrets) {
this.secrets = new TreeSet<Secret>(secrets);
}
public void addSecret(Secret secret) {
if(secret!=null) {
secrets.add(secret);
}
}
2022-03-08 10:16:57 +01:00
public void addSecrets(Collection<Secret> secrets) {
2021-12-01 17:12:52 +01:00
for(Secret secret : secrets){
addSecret(secret);
2021-11-30 17:39:26 +01:00
}
}
2021-12-02 15:43:58 +01:00
public void set() throws Exception {
boolean first = true;
for(Secret secret : secrets) {
/*
* Only the most important Secret must set
* AuthorizationProvider and ScopeProvider
* the others just need to set their token
* in the correspondent provider
*/
if(first) {
secret.set();
first = false;
}else {
secret.setToken();
}
}
}
2021-12-01 17:12:52 +01:00
public SortedSet<Secret> getSecrets() {
return secrets;
2021-11-30 17:39:26 +01:00
}
2021-12-07 13:50:39 +01:00
public User getUser() {
2021-12-01 17:12:52 +01:00
for(Secret secret : secrets) {
2021-11-30 17:39:26 +01:00
try {
2021-12-07 13:50:39 +01:00
return secret.getUser();
2021-11-30 17:39:26 +01:00
}catch (Exception e) {
// trying the next one
}
}
return null;
}
public String getContext() {
2021-12-01 17:12:52 +01:00
for(Secret secret : secrets) {
2021-11-30 17:39:26 +01:00
try {
2021-12-01 17:12:52 +01:00
return secret.getContext();
2021-11-30 17:39:26 +01:00
}catch (Exception e) {
// trying the next one
}
}
2021-12-01 17:12:52 +01:00
return ScopeProvider.instance.get();
2021-11-30 17:39:26 +01:00
}
2021-12-06 17:43:18 +01:00
2021-12-02 13:16:03 +01:00
public void reset() {
boolean first = true;
2021-12-02 13:16:03 +01:00
for(Secret secret : secrets) {
try {
if(first) {
secret.reset();
first = false;
}else {
secret.resetToken();
}
2021-12-02 13:16:03 +01:00
}catch (Exception e) {
// trying the next one
}
}
if(first) {
ScopeProvider.instance.reset();
}
2021-12-02 13:16:03 +01:00
}
2021-12-06 17:43:18 +01:00
2021-11-30 17:39:26 +01:00
}