Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Open Subsurface Data Universe Software
Platform
System
Notification
Commits
a18e92fc
Commit
a18e92fc
authored
Sep 28, 2020
by
Komal Makkar
Browse files
Revert "intermediate"
This reverts commit
d10e6539
parent
d10e6539
Changes
2
Hide whitespace changes
Inline
Side-by-side
provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java
View file @
a18e92fc
...
...
@@ -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
.
executorS
ervice
);
context
=
new
AuthenticationContext
(
this
.
config
.
getAuthURL
(),
false
,
s
ervice
);
credential
=
new
ClientCredential
(
this
.
config
.
getAuthClientID
(),
this
.
config
.
getAuthClientSecret
());
Future
<
AuthenticationResult
>
future
=
context
.
acquireToken
(
this
.
config
.
getAadClientID
(),
credential
,
null
);
...
...
provider/notification-azure/src/test/java/org/opengroup/osdu/notification/util/ServiceAccountClientImplTest.java
View file @
a18e92fc
...
...
@@ -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.get
IdToken(tenantNam
e);
sut
.
get
AccessToken
(
executorServic
e
);
// 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
());
}
}
*/
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment