removed removal provider that for now is not needed
This commit is contained in:
parent
7b0a5b96bf
commit
cec6726b90
|
@ -1,2 +1,3 @@
|
||||||
/target/
|
/target/
|
||||||
/.classpath
|
/.classpath
|
||||||
|
/bin/
|
||||||
|
|
|
@ -1,52 +0,0 @@
|
||||||
package org.gcube.common.security.renewal;
|
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
import org.gcube.common.keycloak.KeycloakClientFactory;
|
|
||||||
import org.gcube.common.keycloak.model.TokenResponse;
|
|
||||||
import org.gcube.common.security.secrets.JWTSecret;
|
|
||||||
import org.gcube.common.security.secrets.Secret;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
|
||||||
*/
|
|
||||||
public class ClientIDManager implements RenewalProvider {
|
|
||||||
|
|
||||||
protected final String clientID;
|
|
||||||
protected final String clientSecret;
|
|
||||||
protected final URL endpoint;
|
|
||||||
|
|
||||||
public ClientIDManager(String clientID, String clientSecret, URL endpoint) {
|
|
||||||
this.clientID = clientID;
|
|
||||||
this.clientSecret = clientSecret;
|
|
||||||
this.endpoint = endpoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Secret getSecret() throws Exception {
|
|
||||||
TokenResponse tokenResponse = KeycloakClientFactory.newInstance().queryUMAToken(endpoint, clientID, clientSecret, null);
|
|
||||||
|
|
||||||
JWTSecret jwtSecret = new JWTSecret(tokenResponse.getAccessToken());
|
|
||||||
jwtSecret.setRenewalProvider(this);
|
|
||||||
|
|
||||||
jwtSecret.setTokenResponse(tokenResponse);
|
|
||||||
|
|
||||||
return jwtSecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Secret getSecret(String context) throws Exception {
|
|
||||||
TokenResponse tokenResponse = KeycloakClientFactory.newInstance().queryUMAToken(endpoint, clientID, clientSecret, context, null);
|
|
||||||
|
|
||||||
JWTSecret jwtSecret = new JWTSecret(tokenResponse.getAccessToken());
|
|
||||||
jwtSecret.setRenewalProvider(this);
|
|
||||||
|
|
||||||
jwtSecret.setTokenResponse(tokenResponse);
|
|
||||||
|
|
||||||
return jwtSecret;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Secret renew() throws Exception {
|
|
||||||
return getSecret();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
package org.gcube.common.security.renewal;
|
|
||||||
|
|
||||||
import org.gcube.common.security.secrets.Secret;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
|
||||||
*/
|
|
||||||
public interface RenewalProvider {
|
|
||||||
|
|
||||||
public Secret renew() throws Exception;
|
|
||||||
}
|
|
|
@ -10,6 +10,9 @@ import org.gcube.common.security.Owner;
|
||||||
|
|
||||||
public class AccessTokenSecret extends Secret {
|
public class AccessTokenSecret extends Secret {
|
||||||
|
|
||||||
|
private static final String AUTH_HEADER = "Authorization";
|
||||||
|
private static final String USER_HEADER = "d4s-user";
|
||||||
|
|
||||||
private String encodedAccessToken;
|
private String encodedAccessToken;
|
||||||
|
|
||||||
private Owner owner;
|
private Owner owner;
|
||||||
|
@ -39,7 +42,9 @@ public class AccessTokenSecret extends Secret {
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> getHTTPAuthorizationHeaders() {
|
public Map<String, String> getHTTPAuthorizationHeaders() {
|
||||||
Map<String, String> authorizationHeaders = new HashMap<>();
|
Map<String, String> authorizationHeaders = new HashMap<>();
|
||||||
authorizationHeaders.put("Authorization", "Bearer " + this.encodedAccessToken);
|
authorizationHeaders.put(AUTH_HEADER, "Bearer " + this.encodedAccessToken);
|
||||||
|
String encodedUser = new String(Base64.getEncoder().encode(this.getOwner().getId().getBytes()));
|
||||||
|
authorizationHeaders.put(USER_HEADER, encodedUser);
|
||||||
return authorizationHeaders;
|
return authorizationHeaders;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -55,11 +60,6 @@ public class AccessTokenSecret extends Secret {
|
||||||
return accessToken.isExpired();
|
return accessToken.isExpired();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRefreshable() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private synchronized void init() {
|
private synchronized void init() {
|
||||||
if (!initialised)
|
if (!initialised)
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class CredentialSecret extends Secret {
|
||||||
private void refreshAccessToken() {
|
private void refreshAccessToken() {
|
||||||
try {
|
try {
|
||||||
KeycloakClient client = KeycloakClientFactory.newInstance();
|
KeycloakClient client = KeycloakClientFactory.newInstance();
|
||||||
TokenResponse response = client.queryUMAToken(username, password, context, null);
|
TokenResponse response = client.queryUMAToken(context, username, password, context, null);
|
||||||
this.accessTokenSecret = new AccessTokenSecret(response.getAccessToken());
|
this.accessTokenSecret = new AccessTokenSecret(response.getAccessToken());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
@ -61,11 +61,5 @@ public class CredentialSecret extends Secret {
|
||||||
public boolean isExpired() {
|
public boolean isExpired() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRefreshable() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,9 +72,4 @@ public class GCubeSecret extends Secret {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRefreshable() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +1,19 @@
|
||||||
package org.gcube.common.security.secrets;
|
package org.gcube.common.security.secrets;
|
||||||
|
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
import org.gcube.common.keycloak.KeycloakClient;
|
||||||
import org.gcube.common.keycloak.model.AccessToken;
|
import org.gcube.common.keycloak.KeycloakClientFactory;
|
||||||
import org.gcube.common.keycloak.model.ModelUtils;
|
|
||||||
import org.gcube.common.keycloak.model.RefreshToken;
|
|
||||||
import org.gcube.common.keycloak.model.TokenResponse;
|
import org.gcube.common.keycloak.model.TokenResponse;
|
||||||
import org.gcube.common.keycloak.model.util.Time;
|
|
||||||
import org.gcube.common.security.Owner;
|
import org.gcube.common.security.Owner;
|
||||||
import org.gcube.common.security.renewal.RenewalProvider;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*/
|
*/
|
||||||
public class JWTSecret extends Secret {
|
public class JWTSecret extends Secret {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(JWTSecret.class);
|
//private static final Logger logger = LoggerFactory.getLogger(JWTSecret.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The interval of time expressed in milliseconds used as guard to refresh the token before that it expires .
|
* The interval of time expressed in milliseconds used as guard to refresh the token before that it expires .
|
||||||
|
@ -30,132 +22,60 @@ public class JWTSecret extends Secret {
|
||||||
*/
|
*/
|
||||||
public static final long TOLERANCE = TimeUnit.MILLISECONDS.toMillis(200);
|
public static final long TOLERANCE = TimeUnit.MILLISECONDS.toMillis(200);
|
||||||
|
|
||||||
protected String jwtToken;
|
private String jwtToken;
|
||||||
|
|
||||||
protected AccessToken accessToken;
|
|
||||||
protected TokenResponse tokenResponse;
|
private String context;
|
||||||
protected RenewalProvider renewalProvider;
|
private AccessTokenSecret accessTokenSecret;
|
||||||
protected Owner owner;
|
|
||||||
protected String context;
|
|
||||||
|
|
||||||
protected boolean initialised = false;
|
protected boolean initialised = false;
|
||||||
|
|
||||||
public JWTSecret(String jwtToken) {
|
public JWTSecret(String jwtToken) {
|
||||||
this.jwtToken = jwtToken;
|
this.jwtToken = jwtToken;
|
||||||
}
|
init();
|
||||||
|
|
||||||
private String getTokenString() {
|
|
||||||
try {
|
|
||||||
boolean expired = false;
|
|
||||||
getAccessToken();
|
|
||||||
|
|
||||||
if(Time.currentTimeMillis()>=(accessToken.getExp()-TOLERANCE)) {
|
|
||||||
expired = true;
|
|
||||||
if(tokenResponse!=null) {
|
|
||||||
try {
|
|
||||||
//KeycloakClientFactory.newInstance().refreshToken(URL, this.getOwner().getId(), tokenResponse);
|
|
||||||
expired = false;
|
|
||||||
}catch (Exception e) {
|
|
||||||
logger.warn("Unable to refresh the token with RefreshToken. Going to try to renew it if possible.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(expired && renewalProvider!=null) {
|
|
||||||
try {
|
|
||||||
JWTSecret renewed = (JWTSecret) renewalProvider.renew();
|
|
||||||
this.jwtToken = renewed.jwtToken;
|
|
||||||
this.accessToken = getAccessToken();
|
|
||||||
}catch (Exception e) {
|
|
||||||
logger.warn("Unable to renew the token with the RenewalProvider. I'll continue using the old token.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}catch (Exception e) {
|
|
||||||
logger.error("Unexpected error in the procedure to evaluate/refresh the current token. I'll continue using the old token.", e);
|
|
||||||
}
|
|
||||||
return jwtToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected AccessToken getAccessToken() {
|
|
||||||
if(accessToken==null) {
|
|
||||||
String realUmaTokenEncoded = jwtToken.split("\\.")[1];
|
|
||||||
String realUmaToken = new String(Base64.getDecoder().decode(realUmaTokenEncoded.getBytes()));
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
|
||||||
try {
|
|
||||||
accessToken = mapper.readValue(realUmaToken, AccessToken.class);
|
|
||||||
}catch(Exception e){
|
|
||||||
logger.error("Error parsing JWT token",e);
|
|
||||||
throw new RuntimeException("Error parsing JWT token", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return accessToken;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void init() {
|
private synchronized void init() {
|
||||||
if (!initialised)
|
try {
|
||||||
try {
|
KeycloakClient client = KeycloakClientFactory.newInstance();
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
TokenResponse tokenResponse = client.queryUMAToken(context, "Bearer "+jwtToken, context, null);
|
||||||
String accessTokenString = objectMapper.writeValueAsString(getAccessToken());
|
this.accessTokenSecret = new AccessTokenSecret(tokenResponse.getAccessToken());
|
||||||
GCubeJWTObject obj = objectMapper.readValue(accessTokenString, GCubeJWTObject.class);
|
} catch (Exception e) {
|
||||||
Owner owner = new Owner(obj.getUsername(), obj.getRoles(), obj.getEmail(), obj.getFirstName(), obj.getLastName(), obj.isExternalService());
|
throw new RuntimeException(e);
|
||||||
owner.setClientName(obj.getClientName());
|
}
|
||||||
owner.setContactOrganisation(obj.getContactOrganisation());
|
}
|
||||||
owner.setClientName(obj.getClientName());
|
|
||||||
context = obj.getContext();
|
|
||||||
initialised = true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private synchronized void refreshAccessToken() {
|
||||||
|
try {
|
||||||
|
KeycloakClient client = KeycloakClientFactory.newInstance();
|
||||||
|
TokenResponse tokenResponse = client.queryUMAToken(context, "Bearer "+jwtToken, context, null);
|
||||||
|
this.accessTokenSecret = new AccessTokenSecret(tokenResponse.getAccessToken());
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Owner getOwner() {
|
public Owner getOwner() {
|
||||||
init();
|
return this.accessTokenSecret.getOwner();
|
||||||
return this.owner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getContext() {
|
public String getContext() {
|
||||||
init();
|
if (this.accessTokenSecret.isExpired())
|
||||||
return this.context;
|
refreshAccessToken();
|
||||||
|
return this.accessTokenSecret.getContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> getHTTPAuthorizationHeaders() {
|
public Map<String, String> getHTTPAuthorizationHeaders() {
|
||||||
Map<String, String> authorizationHeaders = new HashMap<>();
|
if (this.accessTokenSecret.isExpired())
|
||||||
authorizationHeaders.put("Authorization", "Bearer " + getTokenString());
|
refreshAccessToken();
|
||||||
return authorizationHeaders;
|
return this.accessTokenSecret.getHTTPAuthorizationHeaders();
|
||||||
}
|
|
||||||
|
|
||||||
public void setRenewalProvider(RenewalProvider renewalProvider) {
|
|
||||||
this.renewalProvider = renewalProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTokenResponse(TokenResponse tokenResponse) {
|
|
||||||
this.tokenResponse = tokenResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected boolean isExpired(AccessToken accessToken) {
|
|
||||||
return Time.currentTimeMillis()>accessToken.getExp();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isExpired() {
|
public boolean isExpired() {
|
||||||
return isExpired(getAccessToken());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRefreshable() {
|
|
||||||
if(tokenResponse!=null) {
|
|
||||||
try {
|
|
||||||
RefreshToken refreshToken = ModelUtils.getRefreshTokenFrom(tokenResponse);
|
|
||||||
return isExpired(refreshToken);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue