Skip to content
GitLab
Menu
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
90a82380
Commit
90a82380
authored
Jan 14, 2021
by
Komal Makkar
Browse files
undoing stray changes
parent
17503fa0
Changes
1
Hide whitespace changes
Inline
Side-by-side
provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java
View file @
90a82380
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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