Commit a18e92fc authored by Komal Makkar's avatar Komal Makkar
Browse files

Revert "intermediate"

This reverts commit d10e6539
parent d10e6539
......@@ -34,24 +34,14 @@ import java.util.concurrent.*;
@Component
public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClient {
private final AppProperties config;
private final ITenantFactory tenantInfoServiceProvider;
private final IJwtCache<String, IdToken> tenantJwtCache;
@Autowired
private AppProperties config;
private final ExecutorService executorService;
@Autowired
private ITenantFactory tenantInfoServiceProvider;
@Autowired
ServiceAccountJwtAzureClientImpl(AppProperties appProperties,
ITenantFactory tenantInfoServiceProvider,
IJwtCache<String, IdToken> tenantJwtCache) {
this.config = appProperties;
this.tenantJwtCache = tenantJwtCache;
this.tenantInfoServiceProvider = tenantInfoServiceProvider;
this.executorService = Executors.newFixedThreadPool(1);;
}
private IJwtCache tenantJwtCache;
public String getIdToken(String tenantName) {
TenantInfo tenant = this.tenantInfoServiceProvider.getTenantInfo(tenantName);
......@@ -60,27 +50,38 @@ public class ServiceAccountJwtAzureClientImpl implements IServiceAccountJwtClien
}
String ACCESS_TOKEN = "";
// TODO : Refactor to move ID token form Common.Core.model.search to Common.core
IdToken cachedToken = this.tenantJwtCache.get(tenant.getName());
ExecutorService service = null;
if ((cachedToken != null) && !IdToken.refreshToken(cachedToken)) {
return "Bearer " + cachedToken.getTokenValue();
}
try {
// TODO : Refactor to move ID token form Common.Core.model.search to Common.core
IdToken cachedToken = (IdToken) this.tenantJwtCache.get(tenant.getName());
ACCESS_TOKEN = getAccessToken();
IdToken idToken = IdToken.builder().tokenValue(ACCESS_TOKEN).expirationTimeMillis(JWT.decode(ACCESS_TOKEN).getExpiresAt().getTime()).build();
if ((cachedToken != null) && !IdToken.refreshToken(cachedToken)) {
return "Bearer " + cachedToken.getTokenValue();
}
this.tenantJwtCache.put(tenant.getName(), idToken);
// 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;
}
private String getAccessToken(){
// 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, this.executorService);
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);
......
......@@ -32,10 +32,12 @@ import org.opengroup.osdu.notification.provider.azure.cache.JwtCache;
import org.opengroup.osdu.notification.provider.azure.util.AppProperties;
import org.opengroup.osdu.notification.provider.azure.util.ServiceAccountJwtAzureClientImpl;
import java.util.concurrent.ExecutorService;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ServiceAccountClientImplTest {
......@@ -53,6 +55,9 @@ public class ServiceAccountClientImplTest {
@Mock
private IdToken idToken;
@Mock
private ExecutorService executorService;
@Mock
private AppProperties appProperties;
......@@ -109,7 +114,7 @@ public class ServiceAccountClientImplTest {
public void should_updateCache_getIdTokenTest() {
// Set up
when(tenantJwtCacheMock.get(any())).thenReturn(idToken);
String expectedToken = "Bearer " + idToken.getTokenValue();
String expectedToken = "Bearer " +idToken.getTokenValue();
// Act
String returnedToken = this.sut.getIdToken(tenantName);
......@@ -118,7 +123,7 @@ public class ServiceAccountClientImplTest {
Assert.assertEquals(expectedToken, returnedToken);
}
/*@Test
@Test
public void should_return403GivenInvalidApplicationProperties_getAccessToken() {
when(appProperties.getAuthURL()).thenReturn("https://login.microsoftonline.com/s/oauth2/token/");
when(appProperties.getAuthClientID()).thenReturn("testAuthClientID");
......@@ -127,15 +132,15 @@ public class ServiceAccountClientImplTest {
try {
// Act
sut.getIdToken(tenantName);
sut.getAccessToken(executorService);
// Assert
//fail("Should throw exception");
fail("Should throw exception");
} catch (AppException appException) {
//Assert.assertEquals(HttpStatus.SC_FORBIDDEN, appException.getError().getCode());
Assert.assertEquals(HttpStatus.SC_FORBIDDEN, appException.getError().getCode());
} catch (Exception e) {
//fail("Should not throw this exception" + e.getMessage());
fail("Should not throw this exception" + e.getMessage());
}
}*/
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment