Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
N
Notification
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSDU
OSDU Data Platform
System
Notification
Commits
90a82380
Commit
90a82380
authored
4 years ago
by
Komal Makkar
Browse files
Options
Downloads
Patches
Plain Diff
undoing stray changes
parent
17503fa0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!56
[Core] [Azure] Adding handshake filter
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java
+73
-7
73 additions, 7 deletions
...provider/azure/util/ServiceAccountJwtAzureClientImpl.java
with
73 additions
and
7 deletions
provider/notification-azure/src/main/java/org/opengroup/osdu/notification/provider/azure/util/ServiceAccountJwtAzureClientImpl.java
+
73
−
7
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment