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

117 lines
3.6 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;
2022-03-08 10:16:57 +01:00
import java.util.SortedSet;
import java.util.TreeSet;
2021-11-30 17:39:26 +01:00
import org.gcube.common.authorization.utils.provider.SecretProvider;
2021-12-01 11:48:22 +01:00
import org.gcube.common.authorization.utils.secret.Secret;
2022-03-08 10:16:57 +01:00
import org.gcube.common.authorization.utils.secret.SecretUtility;
2021-12-07 13:50:39 +01:00
import org.gcube.common.authorization.utils.user.User;
2021-11-30 17:39:26 +01:00
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class SecretManager {
2021-12-01 17:12:52 +01:00
private SecretHolder initialSecretHolder;
private SecretHolder currentSecretHolder;
2022-02-25 18:08:51 +01:00
public SecretManager() {
2021-12-01 17:12:52 +01:00
initialSecretHolder = new SecretHolder();
currentSecretHolder = initialSecretHolder;
2021-11-30 17:39:26 +01:00
}
public synchronized void addSecretViaProvider(SecretProvider secretProvider) {
if (currentSecretHolder != initialSecretHolder) {
throw new RuntimeException("You can't add a Secret in a session. You must terminate the session first.");
2021-11-30 17:39:26 +01:00
}
Secret secret = secretProvider.getSecret();
currentSecretHolder.addSecret(secret);
2021-11-30 17:39:26 +01:00
}
public synchronized void addSecret(Secret secret) {
if (currentSecretHolder != initialSecretHolder) {
throw new RuntimeException("You can't add a Secret in a session. You must terminate the session first.");
}
currentSecretHolder.addSecret(secret);
}
2021-12-01 17:12:52 +01:00
public synchronized void startSession(Secret secret) throws Exception {
if (currentSecretHolder != initialSecretHolder) {
2022-03-30 15:04:09 +02:00
throw new RuntimeException("You are already in a session. You must terminate the session first.");
2021-12-01 17:12:52 +01:00
}
initialSecretHolder.reset();
2021-12-01 17:12:52 +01:00
currentSecretHolder = new SecretHolder(secret);
2021-12-02 15:43:58 +01:00
currentSecretHolder.set();
2021-11-30 17:39:26 +01:00
}
public synchronized void startSession(Collection<Secret> secrets) throws Exception {
if (currentSecretHolder != initialSecretHolder) {
2022-03-30 15:04:09 +02:00
throw new RuntimeException("You are already in a session. You must terminate the session first.");
}
initialSecretHolder.reset();
2021-12-01 17:12:52 +01:00
currentSecretHolder = new SecretHolder(secrets);
2021-12-02 15:43:58 +01:00
currentSecretHolder.set();
2021-12-01 17:12:52 +01:00
}
public synchronized void startSession(SecretHolder secretHolder) throws Exception {
if (currentSecretHolder != initialSecretHolder) {
2022-03-30 15:04:09 +02:00
throw new RuntimeException("You are already in a session. You must terminate the session first.");
}
initialSecretHolder.reset();
currentSecretHolder = secretHolder;
currentSecretHolder.set();
}
2022-03-08 11:25:21 +01:00
public synchronized void endSession() {
if (currentSecretHolder != initialSecretHolder) {
currentSecretHolder.reset();
2022-03-08 11:25:21 +01:00
try {
initialSecretHolder.set();
}catch (Exception e) {
throw new RuntimeException(e);
}
2021-12-01 17:12:52 +01:00
currentSecretHolder = initialSecretHolder;
}
2021-11-30 17:39:26 +01:00
}
public synchronized void set() throws Exception {
if (currentSecretHolder != initialSecretHolder) {
throw new Exception("You are in a session. You must terminate the session first.");
}
currentSecretHolder.set();
}
public synchronized void reset() {
2022-02-25 14:34:37 +01:00
currentSecretHolder.reset();
if (initialSecretHolder != currentSecretHolder) {
2022-02-25 14:34:37 +01:00
initialSecretHolder.reset();
2021-12-02 13:16:03 +01:00
}
}
2021-12-06 17:43:18 +01:00
public synchronized String getContext() {
2021-12-07 13:50:39 +01:00
return currentSecretHolder.getContext();
2021-12-06 17:43:18 +01:00
}
2022-02-25 16:10:10 +01:00
public synchronized User getUser() {
2021-12-07 13:50:39 +01:00
return currentSecretHolder.getUser();
2021-12-06 17:43:18 +01:00
}
2022-03-08 10:16:57 +01:00
/**
* @return a copy of the current secret holder
* to avoid modification to the original
*/
public synchronized SecretHolder getCurrentSecretHolder() {
SecretHolder secretHolder = new SecretHolder();
SortedSet<Secret> secrets = new TreeSet<>();
SortedSet<Secret> originalSecrets = currentSecretHolder.getSecrets();
for(Secret s : originalSecrets) {
Secret secret = SecretUtility.getSecretByTokenString(s.getToken());
secrets.add(secret);
}
secretHolder.addSecrets(secrets);
return secretHolder;
}
2021-11-30 17:39:26 +01:00
}