Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
Indexer
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
Indexer
Commits
7389e655
Commit
7389e655
authored
3 years ago
by
Abhishek Patil
Browse files
Options
Downloads
Patches
Plain Diff
Using AzureServicePrincipleTokenService in implementation for IServiceAccountJwtClient
parent
802adcd6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!190
Using AzureServicePrincipleTokenService in implementation for IServiceAccountJwtClient
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
provider/indexer-azure/src/main/java/org/opengroup/osdu/indexer/azure/util/ServiceAccountJwtClientImpl.java
+7
-96
7 additions, 96 deletions
.../osdu/indexer/azure/util/ServiceAccountJwtClientImpl.java
with
7 additions
and
96 deletions
provider/indexer-azure/src/main/java/org/opengroup/osdu/indexer/azure/util/ServiceAccountJwtClientImpl.java
+
7
−
96
View file @
7389e655
...
...
@@ -14,110 +14,21 @@
package
org.opengroup.osdu.indexer.azure.util
;
import
com.auth0.jwt.JWT
;
import
com.auth0.jwt.exceptions.JWTDecodeException
;
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.logging.JaxRsDpsLog
;
import
org.opengroup.osdu.core.common.model.http.AppException
;
import
org.opengroup.osdu.core.common.model.http.DpsHeaders
;
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.azure.util.AzureServicePrincipleTokenService
;
import
org.opengroup.osdu.core.common.util.IServiceAccountJwtClient
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.annotation.RequestScope
;
import
javax.inject.Inject
;
import
javax.inject.Named
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
@Component
@RequestScope
public
class
ServiceAccountJwtClientImpl
implements
IServiceAccountJwtClient
{
@Inject
private
ITenantFactory
tenantInfoServiceProvider
;
@Inject
private
DpsHeaders
dpsHeaders
;
@Inject
private
IJwtCache
cacheService
;
@Inject
private
JaxRsDpsLog
log
;
@Inject
@Named
(
"AAD_OBO_API"
)
private
String
authAPI
;
@Inject
@Named
(
"AUTH_CLIENT_ID"
)
private
String
authClientID
;
@Inject
@Named
(
"AUTH_CLIENT_SECRET"
)
private
String
authClientSecret
;
@Inject
@Named
(
"AUTH_URL"
)
private
String
authURL
;
public
String
getIdToken
(
String
tenantName
)
{
this
.
log
.
info
(
"Tenant name received for auth token is: "
+
tenantName
);
TenantInfo
tenant
=
this
.
tenantInfoServiceProvider
.
getTenantInfo
(
tenantName
);
if
(
tenant
==
null
)
{
this
.
log
.
error
(
"Invalid tenant name receiving from azure"
);
throw
new
AppException
(
HttpStatus
.
SC_BAD_REQUEST
,
"Invalid tenant Name"
,
"Invalid tenant Name from azure"
);
}
String
ACCESS_TOKEN
=
""
;
try
{
IdToken
cachedToken
=
(
IdToken
)
this
.
cacheService
.
get
(
tenant
.
getServiceAccount
());
this
.
dpsHeaders
.
put
(
DpsHeaders
.
USER_EMAIL
,
tenant
.
getServiceAccount
());
if
(!
IdToken
.
refreshToken
(
cachedToken
))
{
return
cachedToken
.
getTokenValue
();
}
ExecutorService
service
=
Executors
.
newFixedThreadPool
(
1
);
AuthenticationContext
context
=
null
;
try
{
context
=
new
AuthenticationContext
(
authURL
,
false
,
service
);
ClientCredential
credential
=
new
ClientCredential
(
authClientID
,
authClientSecret
);
Future
<
AuthenticationResult
>
future
=
context
.
acquireToken
(
authAPI
,
credential
,
null
);
ACCESS_TOKEN
=
future
.
get
().
getAccessToken
();
if
(
future
==
null
)
{
log
.
error
(
String
.
format
(
"Azure Authentication: %s"
,
future
.
get
().
getAccessToken
()));
throw
new
AppException
(
HttpStatus
.
SC_FORBIDDEN
,
"Access denied"
,
"The user is not authorized to perform this action"
);
}
IdToken
idToken
=
IdToken
.
builder
().
tokenValue
(
ACCESS_TOKEN
).
expirationTimeMillis
(
JWT
.
decode
(
ACCESS_TOKEN
).
getExpiresAt
().
getTime
()).
build
();
this
.
cacheService
.
put
(
tenant
.
getServiceAccount
(),
idToken
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
finally
{
service
.
shutdown
();
}
}
catch
(
JWTDecodeException
e
)
{
throw
new
AppException
(
HttpStatus
.
SC_INTERNAL_SERVER_ERROR
,
"Persistence error"
,
"Invalid token, error decoding"
,
e
);
}
catch
(
AppException
e
)
{
throw
e
;
}
catch
(
Exception
e
)
{
throw
new
AppException
(
HttpStatus
.
SC_INTERNAL_SERVER_ERROR
,
"Persistence error"
,
"Error generating token"
,
e
);
}
@Autowired
private
AzureServicePrincipleTokenService
tokenService
;
return
ACCESS_TOKEN
;
@Override
public
String
getIdToken
(
String
partitionId
){
return
"Bearer "
+
this
.
tokenService
.
getAuthorizationToken
();
}
}
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