forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add getBearerTokenSupplier (Azure#42152)
* Add getBearerTokenSupplier This supports our Open AI work. Fixes Azure#41611 * PR feedback * linter fixes * pr feedback * fix ordering of modifiers
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
sdk/identity/azure-identity/src/main/java/com/azure/identity/AuthenticationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.identity; | ||
|
||
import com.azure.core.credential.TokenCredential; | ||
import com.azure.core.http.HttpHeaderName; | ||
import com.azure.core.http.HttpMethod; | ||
import com.azure.core.http.HttpPipeline; | ||
import com.azure.core.http.HttpPipelineBuilder; | ||
import com.azure.core.http.HttpRequest; | ||
import com.azure.core.http.HttpResponse; | ||
import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; | ||
import com.azure.core.util.Context; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Utility methods for working with authentication. | ||
*/ | ||
public final class AuthenticationUtil { | ||
|
||
private AuthenticationUtil() { | ||
} | ||
|
||
/** | ||
* Creates a {@link Supplier} that provides a Bearer token from the specified credential. | ||
* The token is cached and will refresh when it expires. | ||
* <p><strong>Using the supplier:</strong></p> | ||
* <!-- src_embed com.azure.identity.util.getBearerTokenSupplier --> | ||
* <pre> | ||
* DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build(); | ||
* String scope = "https://cognitiveservices.azure.com/.default"; | ||
* Supplier<String> supplier = AuthenticationUtil.getBearerTokenSupplier(credential, scope); | ||
* | ||
* // This example simply uses the Azure SDK HTTP library to demonstrate setting the header. | ||
* // Use the token as is appropriate for your circumstances. | ||
* HttpRequest request = new HttpRequest(HttpMethod.GET, "https://www.example.com"); | ||
* request.setHeader(HttpHeaderName.AUTHORIZATION, "Bearer " + supplier.get()); | ||
* HttpClient client = HttpClient.createDefault(); | ||
* client.sendSync(request, Context.NONE); | ||
* </pre> | ||
* <!-- end com.azure.identity.util.getBearerTokenSupplier --> | ||
* | ||
* @param credential The {@link TokenCredential} from which to retrieve a token. | ||
* @param scopes The scopes as appropriate for the token you are retrieving. | ||
* @return A {@link Supplier} which returns the bearer token as a {@link String}. | ||
*/ | ||
public static Supplier<String> getBearerTokenSupplier(TokenCredential credential, String... scopes) { | ||
HttpPipeline pipeline = new HttpPipelineBuilder().policies(new BearerTokenAuthenticationPolicy(credential, scopes)).build(); | ||
return () -> { | ||
// This request will never need to go anywhere; it is simply to cause the policy to interact with | ||
// the user's credential | ||
HttpRequest req = new HttpRequest(HttpMethod.GET, "https://www.example.com"); | ||
try (HttpResponse res = pipeline.sendSync(req, Context.NONE)) { | ||
return res.getRequest().getHeaders().get(HttpHeaderName.AUTHORIZATION).getValue().split(" ")[1]; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
sdk/identity/azure-identity/src/test/java/com/azure/identity/AuthenticationUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.identity; | ||
|
||
import com.azure.core.test.utils.MockTokenCredential; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static com.azure.identity.AuthenticationUtil.getBearerTokenSupplier; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class AuthenticationUtilTest { | ||
|
||
@Test | ||
public void testGetBearerTokenSupplier() { | ||
MockTokenCredential credential = new MockTokenCredential(); | ||
Supplier<String> supplier = getBearerTokenSupplier(credential, "scope"); | ||
assertEquals("mockToken", supplier.get()); | ||
} | ||
} |