Refactoring library to be properly used as provider in Smartgears

This commit is contained in:
Luca Frosini 2022-02-25 12:58:24 +01:00
parent a66c1da525
commit 261e8733b2
4 changed files with 53 additions and 61 deletions

View File

@ -85,13 +85,22 @@ public class SecretHolder {
} }
public void reset() { public void reset() {
boolean first = true;
for(Secret secret : secrets) { for(Secret secret : secrets) {
try { try {
secret.reset(); if(first) {
secret.reset();
first = false;
}else {
secret.resetToken();
}
}catch (Exception e) { }catch (Exception e) {
// trying the next one // trying the next one
} }
} }
if(first) {
ScopeProvider.instance.reset();
}
} }
} }

View File

@ -1,7 +1,8 @@
package org.gcube.common.authorization.utils.manager; package org.gcube.common.authorization.utils.manager;
import java.util.SortedSet; import java.util.Collection;
import org.gcube.common.authorization.utils.provider.SecretProvider;
import org.gcube.common.authorization.utils.secret.Secret; import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.user.User; import org.gcube.common.authorization.utils.user.User;
@ -22,20 +23,28 @@ public class SecretManager {
private SecretHolder initialSecretHolder; private SecretHolder initialSecretHolder;
private SecretHolder currentSecretHolder; private SecretHolder currentSecretHolder;
private SecretManager(){ private SecretManager() {
initialSecretHolder = new SecretHolder(); initialSecretHolder = new SecretHolder();
currentSecretHolder = initialSecretHolder; currentSecretHolder = initialSecretHolder;
} }
public synchronized void addSecret(Secret secret) throws Exception { public synchronized void addSecretViaProvider(SecretProvider secretProvider) {
if(currentSecretHolder!=initialSecretHolder) { if (currentSecretHolder != initialSecretHolder) {
throw new Exception("You can't add a Secret in a session. You must terminate the session first."); throw new RuntimeException("You can't add a Secret in a session. You must terminate the session first.");
} }
initialSecretHolder.addSecret(secret); Secret secret = secretProvider.getSecret();
currentSecretHolder.addSecret(secret);
}
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);
} }
public synchronized void startSession(Secret secret) throws Exception { public synchronized void startSession(Secret secret) throws Exception {
if(currentSecretHolder!=initialSecretHolder) { if (currentSecretHolder != initialSecretHolder) {
throw new Exception("You are already in a session. You must terminate the session first."); throw new Exception("You are already in a session. You must terminate the session first.");
} }
initialSecretHolder.reset(); initialSecretHolder.reset();
@ -43,8 +52,8 @@ public class SecretManager {
currentSecretHolder.set(); currentSecretHolder.set();
} }
public synchronized void startSession(SortedSet<Secret> secrets) throws Exception { public synchronized void startSession(Collection<Secret> secrets) throws Exception {
if(currentSecretHolder!=initialSecretHolder) { if (currentSecretHolder != initialSecretHolder) {
throw new Exception("You are already in a session. You must terminate the session first."); throw new Exception("You are already in a session. You must terminate the session first.");
} }
initialSecretHolder.reset(); initialSecretHolder.reset();
@ -52,17 +61,33 @@ public class SecretManager {
currentSecretHolder.set(); currentSecretHolder.set();
} }
public synchronized void startSession(SecretHolder secretHolder) throws Exception {
if (currentSecretHolder != initialSecretHolder) {
throw new Exception("You are already in a session. You must terminate the session first.");
}
initialSecretHolder.reset();
currentSecretHolder = secretHolder;
currentSecretHolder.set();
}
public synchronized void endSession() throws Exception { public synchronized void endSession() throws Exception {
if(currentSecretHolder!=initialSecretHolder) { if (currentSecretHolder != initialSecretHolder) {
currentSecretHolder.reset(); currentSecretHolder.reset();
initialSecretHolder.set(); initialSecretHolder.set();
currentSecretHolder = initialSecretHolder; currentSecretHolder = initialSecretHolder;
} }
} }
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() { public synchronized void reset() {
initialSecretHolder.reset(); initialSecretHolder.reset();
if(initialSecretHolder!=currentSecretHolder) { if (initialSecretHolder != currentSecretHolder) {
currentSecretHolder.reset(); currentSecretHolder.reset();
} }
instance.remove(); instance.remove();
@ -72,7 +97,7 @@ public class SecretManager {
return currentSecretHolder.getContext(); return currentSecretHolder.getContext();
} }
public synchronized User getUser() { public synchronized User getIdentity() {
return currentSecretHolder.getUser(); return currentSecretHolder.getUser();
} }

View File

@ -1,21 +0,0 @@
package org.gcube.common.authorization.utils.provider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.GCubeSecret;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GCubeSecretProvider implements SecretProvider {
@Override
public Secret getSecret() {
String token = SecurityTokenProvider.instance.get();
if(token!=null) {
return new GCubeSecret(token);
}
return null;
}
}

View File

@ -1,21 +0,0 @@
package org.gcube.common.authorization.utils.provider;
import org.gcube.common.authorization.library.provider.AccessTokenProvider;
import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.authorization.utils.secret.JWTSecret;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class JWTSecretProvider implements SecretProvider {
@Override
public Secret getSecret() {
String token = AccessTokenProvider.instance.get();
if(token!=null) {
return new JWTSecret(token);
}
return null;
}
}