Renamed class to self-explain it and added UMA token use with audience

This commit is contained in:
Mauro Mugnaini 2020-07-09 14:33:54 +02:00
parent 52cf5393e4
commit c15cd4b718
1 changed files with 17 additions and 9 deletions

View File

@ -12,20 +12,24 @@ import org.gcube.oidc.rest.OpenIdConnectRESTHelper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class HTTPEventSender implements EventSender { public class HTTPWithUMAAuthEventSender implements EventSender {
protected static final Logger log = LoggerFactory.getLogger(HTTPEventSender.class); protected static final Logger log = LoggerFactory.getLogger(HTTPWithUMAAuthEventSender.class);
private URL baseEnndpointURL; private URL baseEnndpointURL;
private String clientId; private String clientId;
private String clientSecret; private String clientSecret;
private URL tokenURL; private URL tokenURL;
private String umaAudience;
public HTTPWithUMAAuthEventSender(URL baseEnndpointURL, String clientId, String clientSecret, URL tokenURL,
String umaAudience) {
public HTTPEventSender(URL baseEnndpointURL, String clientId, String clientSecret, URL tokenURL) {
this.baseEnndpointURL = baseEnndpointURL; this.baseEnndpointURL = baseEnndpointURL;
this.clientId = clientId; this.clientId = clientId;
this.clientSecret = clientSecret; this.clientSecret = clientSecret;
this.tokenURL = tokenURL; this.tokenURL = tokenURL;
this.umaAudience = umaAudience;
} }
@Override @Override
@ -57,10 +61,14 @@ public class HTTPEventSender implements EventSender {
public void run() { public void run() {
try { try {
log.debug("Getting OIDC token for client: {}", clientId); log.debug("Getting OIDC token for client: {}", clientId);
JWTToken token = null; JWTToken umaToken = null;
if (clientId != null && clientSecret != null && tokenURL != null) { if (clientId != null && clientSecret != null && tokenURL != null && umaAudience != null) {
log.debug("Getting OIDC token for clientId '{}' from: {}", clientId, tokenURL); log.debug("Getting OIDC token for clientId '{}' from: {}", clientId, tokenURL);
token = OpenIdConnectRESTHelper.queryClientToken(clientId, clientSecret, tokenURL); JWTToken oidcToken = OpenIdConnectRESTHelper.queryClientToken(clientId, clientSecret, tokenURL);
log.debug("Getting UMA token for audience '{}' from: {}", umaAudience, tokenURL);
umaToken = OpenIdConnectRESTHelper.queryUMAToken(tokenURL, oidcToken.getAsBearer(), umaAudience, null);
} else {
log.debug("Can't get UMA token since not all the required params was provied");
} }
log.debug("Performing HTTP POST to: {}", endpoint); log.debug("Performing HTTP POST to: {}", endpoint);
HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection(); HttpURLConnection connection = (HttpURLConnection) endpoint.openConnection();
@ -73,9 +81,9 @@ public class HTTPEventSender implements EventSender {
// Commented out as per the Conductor issue: https://github.com/Netflix/conductor/issues/376 // Commented out as per the Conductor issue: https://github.com/Netflix/conductor/issues/376
// connection.setRequestProperty("Accept", "application/json"); // connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true); connection.setDoOutput(true);
if (token != null) { if (umaToken != null) {
log.debug("Setting authorization header as: {}", token.getAsBearer()); log.debug("Setting authorization header as: {}", umaToken.getAsBearer());
connection.setRequestProperty("Authorization", token.getAsBearer()); connection.setRequestProperty("Authorization", umaToken.getAsBearer());
} else { } else {
log.debug("Sending request without authorization header"); log.debug("Sending request without authorization header");
} }