Commit 48930036 authored by Abhishek Patil's avatar Abhishek Patil Committed by harshit aggarwal
Browse files

Using AzureServicePrincipleTokenService in implementation of UserCredential

parent a4574ae1
This diff is collapsed.
......@@ -12,7 +12,7 @@
<description>WKS service on Azure</description>
<packaging>jar</packaging>
<properties>
<osdu.corelibazure.version>0.11.0-rc4</osdu.corelibazure.version>
<osdu.corelibazure.version>0.11.0-rc5</osdu.corelibazure.version>
<azure.applicationinsights.version>2.6.1</azure.applicationinsights.version>
<osdu.os-wks-core.version>0.11.0-SNAPSHOT</osdu.os-wks-core.version>
<azure.version>2.1.7</azure.version>
......
package org.opengroup.osdu.wks.provider.azure.credentials;
import org.opengroup.osdu.azure.util.AzureServicePrincipal;
import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.wks.provider.interfaces.UserCredential;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.io.UnsupportedEncodingException;
@Configuration
public class JwtTokenGenerator implements UserCredential {
private final static Logger LOGGER = LoggerFactory.getLogger(JwtTokenGenerator.class);
private static final String BEARER = "Bearer ";
@Autowired
private AzureBootstrapConfig azureBootstrapConfig;
@Autowired
private AzureServicePrincipal azureServicePrincipal;
private AzureServicePrincipleTokenService tokenService;
@Override
public String getIdToken(TenantInfo tenant) throws ApplicationException {
// Azure service principal will have access to all data partitions
String token = null;
try {
token = BEARER + azureServicePrincipal.getIdToken(
azureBootstrapConfig.getClientId(),
azureBootstrapConfig.getClientSecret(),
azureBootstrapConfig.getTenantId(),
azureBootstrapConfig.getAppResourceId());
}
catch (UnsupportedEncodingException e) {
LOGGER.warn(e.getMessage(), e);
throw new ApplicationException(e.getMessage(), e.getCause());
}
return token;
public String getIdToken(TenantInfo tenantInfo){
return "Bearer " + this.tokenService.getAuthorizationToken();
}
}
package org.opengroup.osdu.wks.provider.azure.credentials;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.util.AzureServicePrincipal;
import org.opengroup.osdu.azure.util.AzureServicePrincipleTokenService;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.opengroup.osdu.wks.exceptions.ApplicationException;
import org.opengroup.osdu.wks.provider.azure.di.AzureBootstrapConfig;
import java.io.UnsupportedEncodingException;
......@@ -25,58 +24,34 @@ import static org.mockito.Mockito.when;
public class JwtTokenGeneratorTest {
private static final String token = "jwt-token";
private static final String clientId = "client-id";
private static final String clientSecret = "client-secret";
private static final String tenantID = "tenant-id";
private static final String appResourceId = "app-resource-id";
@InjectMocks
private JwtTokenGenerator jwtTokenGenerator;
@Mock
private AzureServicePrincipal azureServicePrincipal;
@Mock
private AzureBootstrapConfig azureBootstrapConfig;
@BeforeEach
public void init() {
when(azureBootstrapConfig.getClientId()).thenReturn(clientId);
when(azureBootstrapConfig.getClientSecret()).thenReturn(clientSecret);
when(azureBootstrapConfig.getTenantId()).thenReturn(tenantID);
when(azureBootstrapConfig.getAppResourceId()).thenReturn(appResourceId);
}
private AzureServicePrincipleTokenService azureServicePrincipleTokenService;
@Test
public void shouldSuccessfullyGenerateToken() throws UnsupportedEncodingException, ApplicationException {
when(azureServicePrincipal.getIdToken(clientId, clientSecret, tenantID, appResourceId)).thenReturn(token);
when(azureServicePrincipleTokenService.getAuthorizationToken()).thenReturn(token);
String result = jwtTokenGenerator.getIdToken(new TenantInfo());
assertEquals("Bearer " + token, result);
verify(azureServicePrincipal, times(1)).getIdToken(clientId, clientSecret, tenantID, appResourceId);
verify(azureBootstrapConfig, times(1)).getClientId();
verify(azureBootstrapConfig, times(1)).getClientSecret();
verify(azureBootstrapConfig, times(1)).getTenantId();
verify(azureBootstrapConfig, times(1)).getAppResourceId();
verify(azureServicePrincipleTokenService, times(1)).getAuthorizationToken();
}
@Test
public void shouldThrowApplicationException() throws UnsupportedEncodingException {
public void shouldThrowAppException() throws UnsupportedEncodingException {
doThrow(UnsupportedEncodingException.class).when(azureServicePrincipal).getIdToken(clientId, clientSecret, tenantID, appResourceId);
doThrow(AppException.class).when(azureServicePrincipleTokenService).getAuthorizationToken();
ApplicationException exception = assertThrows(ApplicationException.class, () -> {
AppException exception = assertThrows(AppException.class, () -> {
jwtTokenGenerator.getIdToken(new TenantInfo());
});
assertNotNull(exception);
verify(azureServicePrincipal, times(1)).getIdToken(clientId, clientSecret, tenantID, appResourceId);
verify(azureBootstrapConfig, times(1)).getClientId();
verify(azureBootstrapConfig, times(1)).getClientSecret();
verify(azureBootstrapConfig, times(1)).getTenantId();
verify(azureBootstrapConfig, times(1)).getAppResourceId();
verify(azureServicePrincipleTokenService, times(1)).getAuthorizationToken();
}
}
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