Azure / azure-sdk-for-java

This repository is for active development of the Azure SDK for Java. For consumers of the SDK we recommend visiting our public developer docs at https://docs.microsoft.com/java/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-java.
MIT License
2.32k stars 1.97k forks source link

Use an access token directly instead of token credential #40802

Closed HSV24 closed 3 months ago

HSV24 commented 3 months ago

Query/Question Cx is trying to establish connection with their cosmos DB using cosmosclientbuilder class. This is takes the tokencredential to get an access token from AAD and establish the connection. https://learn.microsoft.com/en-us/java/api/com.azure.cosmos.cosmosclientbuilder?view=azure-java-stable So, we define all the necessary values such as tenant id. client id and secret etc for tokencredential and pass it on the builder to get access token itself and use it.

However, due to cx network requirements, they restricted the access to login.microsoftonline.com URL from their environment. So the above builder could not establish connection with cosmosDB as it is unable to get token and tokencredetnial requires login.microsoftonline.com to get token successfully.

As a workaround, cx is able to get a token from their other environment. However, we are unable to pass this token to the builder. Seems the cosmos builder only accepts as tokencredential and not as an access token. So they are getting format error.

Now we would like to know is there any way to convert this retrieved access token to token credential format that can be utilized in cosmosclientbuilder class.

FabianMeiswinkel commented 3 months ago

No there isn't - an access token would have an expiration time. What you can do is build your own TokenCredential class that uses whatever magic behind the scenes to be able to deliver new access tokens when requested. But none of that has anything to do with Cosmos DB - it is purely azure-core/azure-identity usage.

The TokenCredential interface is here: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/core/azure-core/src/main/java/com/azure/core/credential/TokenCredential.java