From 90a82380c6205f3b664effba7aed680e38e666cb Mon Sep 17 00:00:00 2001 From: komakkar <komakkar@microsoft.com> Date: Thu, 14 Jan 2021 17:09:14 +0530 Subject: [PATCH] undoing stray changes --- .../ServiceAccountJwtAzureClientImpl.java | 80 +++++++++++++++++-- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java index 9c229753b..3ae77ff2d 100644 --- a/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java +++ b/provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java @@ -14,23 +14,89 @@ package org.opengroup.osdu.notification.provider.azure.util; -import org.opengroup.osdu.core.common.model.http.DpsHeaders; +import com.auth0.jwt.JWT; +import com.microsoft.aad.adal4j.AuthenticationContext; +import com.microsoft.aad.adal4j.AuthenticationResult; +import com.microsoft.aad.adal4j.ClientCredential; +import org.apache.http.HttpStatus; +import org.opengroup.osdu.core.common.model.http.AppException; +import org.opengroup.osdu.core.common.model.search.IdToken; +import org.opengroup.osdu.core.common.model.tenant.TenantInfo; +import org.opengroup.osdu.core.common.provider.interfaces.IJwtCache; +import org.opengroup.osdu.core.common.provider.interfaces.ITenantFactory; import org.opengroup.osdu.core.common.util.IServiceAccountJwtClient; -import org.opengroup.osdu.notification.provider.interfaces.IPubsubRequestBodyExtractor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.Map; +import java.net.MalformedURLException; +import java.util.concurrent.*; @Component public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClient { @Autowired - private IPubsubRequestBodyExtractor pubsubRequestBodyExtractor; + private AppProperties config; + + @Autowired + private ITenantFactory tenantInfoServiceProvider; + + @Autowired + private IJwtCache tenantJwtCache; public String getIdToken(String tenantName) { - Map<String, String> attributes = this.pubsubRequestBodyExtractor.extractAttributesFromRequestBody(); - return attributes.get(DpsHeaders.AUTHORIZATION); + TenantInfo tenant = this.tenantInfoServiceProvider.getTenantInfo(tenantName); + if (tenant == null) { + throw new AppException(HttpStatus.SC_BAD_REQUEST, "Invalid tenant Name", "Invalid tenant Name from azure"); + } + + String ACCESS_TOKEN = ""; + ExecutorService service = null; + + try { + // TODO : Refactor to move ID token form Common.Core.model.search to Common.core + IdToken cachedToken = (IdToken) this.tenantJwtCache.get(tenant.getName()); + + if ((cachedToken != null) && !IdToken.refreshToken(cachedToken)) { + return "Bearer " + cachedToken.getTokenValue(); + } + + // TODO : Control the thread count via config and pool should be created once. + service = Executors.newFixedThreadPool(1); + + ACCESS_TOKEN = getAccessToken(service); + IdToken idToken = IdToken.builder().tokenValue(ACCESS_TOKEN).expirationTimeMillis(JWT.decode(ACCESS_TOKEN).getExpiresAt().getTime()).build(); + this.tenantJwtCache.put(tenant.getName(), idToken); + } finally { + if(service != null) { + service.shutdown(); + } + } + return "Bearer " + ACCESS_TOKEN; } -} + // TODO : Refactor for making it test-able. + // THIS METHOD IS PUBLIC ONLY TO ENABLE UNIT TESTING + public String getAccessToken(ExecutorService service) { + AuthenticationContext context = null; + ClientCredential credential = null; + String ACCESS_TOKEN = null; + try { + context = new AuthenticationContext(this.config.getAuthURL(), false, service); + credential = new ClientCredential(this.config.getAuthClientID(), this.config.getAuthClientSecret()); + + Future<AuthenticationResult> future = context.acquireToken(this.config.getAadClientID(), credential, null); + + if (future == null) { + throw new AppException(HttpStatus.SC_FORBIDDEN, "Token not generated", "The user is not authorized to obtain Token From AAD"); + } + ACCESS_TOKEN = future.get().getAccessToken(); + } catch (MalformedURLException malformedURLException) { + malformedURLException.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + return ACCESS_TOKEN; + } +} \ No newline at end of file -- GitLab