Skip to content

Commit 5066a02

Browse files
authored
Clientcore oauth updates (Azure#44846)
* add OAuthTokenCredentialTrait * Make the constructor take a OAuthTokenRequest so it has a proper set of metdata. Make the constructor take a OAuthTokenRequest so it has a proper set of metdata. * linter fixes
1 parent 5441dac commit 5066a02

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

sdk/clientcore/core/src/main/java/io/clientcore/core/http/pipeline/OAuthBearerTokenAuthenticationPolicy.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,33 @@ public class OAuthBearerTokenAuthenticationPolicy extends HttpCredentialPolicy {
3636
private static final ClientLogger LOGGER = new ClientLogger(OAuthBearerTokenAuthenticationPolicy.class);
3737
private static final String BEARER = "Bearer";
3838

39-
private final String[] scopes;
39+
// The default context contains all OAuth metadata specified in the tsp.
40+
private final OAuthTokenRequestContext context;
4041
private final OAuthTokenCredential credential;
4142

4243
/**
4344
* Creates BearerTokenAuthenticationPolicy.
4445
*
45-
* @param credential the token credential to authenticate the request
46-
* @param scopes the scopes of authentication the credential should get token for
46+
* @param credential the token credential to authenticate the request.
47+
* @param context the default OAuth metadata to use for the token request.
4748
*/
48-
public OAuthBearerTokenAuthenticationPolicy(OAuthTokenCredential credential, String... scopes) {
49+
public OAuthBearerTokenAuthenticationPolicy(OAuthTokenCredential credential, OAuthTokenRequestContext context) {
4950
Objects.requireNonNull(credential);
51+
Objects.requireNonNull(context);
5052
this.credential = credential;
51-
this.scopes = scopes;
53+
this.context = context;
5254
}
5355

5456
/**
5557
* Executed before sending the initial request and authenticates the request.
5658
*
5759
* @param httpRequest The request context.
60+
* @param context the OAuth metadata to use for the token request.
5861
*/
59-
public void authorizeRequest(HttpRequest httpRequest) {
60-
setAuthorizationHeader(httpRequest, new OAuthTokenRequestContext().addScopes(scopes));
61-
}
62-
63-
/**
64-
* Authorizes the request with the bearer token acquired using the specified {@code tokenRequestContext}
65-
*
66-
* @param request the HTTP request.
67-
* @param tokenRequestContext the token request context to be used for token acquisition.
68-
*/
69-
protected void setAuthorizationHeader(HttpRequest request, OAuthTokenRequestContext tokenRequestContext) {
70-
AccessToken token = credential.getToken(tokenRequestContext);
71-
request.getHeaders().set(HttpHeaderName.AUTHORIZATION, BEARER + " " + token.getToken());
62+
public void authorizeRequest(HttpRequest httpRequest, OAuthTokenRequestContext context) {
63+
// Credential implementations are responsible for knowing what to do with the OAuth metadata.
64+
AccessToken token = credential.getToken(context);
65+
httpRequest.getHeaders().set(HttpHeaderName.AUTHORIZATION, BEARER + " " + token.getToken());
7266
}
7367

7468
@Override
@@ -80,7 +74,9 @@ public Response<BinaryData> process(HttpRequest httpRequest, HttpPipelineNextPol
8074

8175
HttpPipelineNextPolicy nextPolicy = next.copy();
8276

83-
authorizeRequest(httpRequest);
77+
// For now we don't support per-operation scopes. In the future when we do, we will need to retrieve the
78+
// scope from the incoming httpRequest and merge it with the default context.
79+
authorizeRequest(httpRequest, context);
8480
Response<BinaryData> httpResponse = next.process();
8581
String authHeader = httpResponse.getHeaders().getValue(HttpHeaderName.WWW_AUTHENTICATE);
8682
if (httpResponse.getStatusCode() == 401 && authHeader != null) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package io.clientcore.core.traits;
5+
6+
import io.clientcore.core.credentials.oauth.OAuthTokenCredential;
7+
8+
/**
9+
* A {@link io.clientcore.core.traits trait} providing a consistent interface for setting
10+
* {@link OAuthTokenCredential}.
11+
12+
* @param <T> The concrete type that implements the trait. This is required so that fluent operations can continue
13+
* to return the concrete type, rather than the trait type.
14+
*
15+
* @see io.clientcore.core.traits
16+
* @see OAuthTokenCredential
17+
*/
18+
public interface OAuthTokenCredentialTrait<T extends OAuthTokenCredentialTrait<T>> {
19+
/**
20+
* Sets the {@link OAuthTokenCredential} used to authorize requests sent to the service.
21+
*
22+
* @param credential {@link OAuthTokenCredential} used to authorize requests sent to the service.
23+
* @return Returns the same concrete type with the appropriate properties updated, to allow for fluent chaining of
24+
* operations.
25+
*/
26+
T credential(OAuthTokenCredential credential);
27+
}

0 commit comments

Comments
 (0)