Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] TokenCredentialRefresher and authenticationOptions #16924

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/core/core-auth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

- New interface added: `TokenCredentialRefresher`. Represents a credential that can refresh a token over time.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read the description of this PR ✨


### Breaking Changes

### Key Bugs Fixed
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/core-auth/review/core-auth.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export interface TokenCredential {
getToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null>;
}

// @public
export interface TokenCredentialRefresher extends TokenCredential {
refreshToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null>;
}


// (No @packageDocumentation comment for this package)

Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { AzureSASCredential, SASCredential, isSASCredential } from "./azureSASCr

export {
TokenCredential,
TokenCredentialRefresher,
GetTokenOptions,
AccessToken,
isTokenCredential
Expand Down
16 changes: 16 additions & 0 deletions sdk/core/core-auth/src/tokenCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ export interface TokenCredential {
getToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null>;
}

/**
* Represents a credential capable of refreshing an authentication token over time.
*/
export interface TokenCredentialRefresher extends TokenCredential {
/**
* Retrieves previously stored token, or retrieves a new token.
*
* This method is called automatically by Azure SDK client libraries.
*
* @param scopes - The list of scopes for which the token will have access.
* @param options - The options used to configure any requests this
* TokenCredential implementation might make.
*/
refreshToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null>;
}

/**
* Defines options for TokenCredential.getToken.
*/
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/core-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Features Added

- Added a new optional property `authenticationOptions` to the `OperationOptions` interface. Through this property, users will be able to specify values that will modify the client authentication through the request pipelines.

### Breaking Changes

### Bugs Fixed
Expand Down
7 changes: 7 additions & 0 deletions sdk/core/core-client/review/core-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ import { PipelinePolicy } from '@azure/core-rest-pipeline';
import { PipelineRequest } from '@azure/core-rest-pipeline';
import { PipelineResponse } from '@azure/core-rest-pipeline';
import { TokenCredential } from '@azure/core-auth';
import { TokenCredentialRefresher } from '@azure/core-auth';
import { TransferProgressEvent } from '@azure/core-rest-pipeline';

// @public
export interface AuthenticationOptions {
credential?: TokenCredential | TokenCredentialRefresher;
}

// @public (undocumented)
export interface BaseMapper {
constraints?: MapperConstraints;
Expand Down Expand Up @@ -202,6 +208,7 @@ export interface OperationArguments {
// @public
export interface OperationOptions {
abortSignal?: AbortSignalLike;
authenticationOptions?: AuthenticationOptions;
onResponse?: RawResponseCallback;
requestOptions?: OperationRequestOptions;
serializerOptions?: SerializerOptions;
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
OperationSpec,
OperationArguments,
OperationOptions,
AuthenticationOptions,
OperationResponseMap,
OperationParameter,
OperationQueryParameter,
Expand Down
16 changes: 16 additions & 0 deletions sdk/core/core-client/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { TokenCredential, TokenCredentialRefresher } from "@azure/core-auth";
import { AbortSignalLike } from "@azure/abort-controller";
import { OperationTracingOptions } from "@azure/core-tracing";
import {
Expand Down Expand Up @@ -37,6 +38,7 @@ export interface XmlOptions {
*/
xmlCharKey?: string;
}

/**
* Options to configure serialization/de-serialization behavior.
*/
Expand All @@ -47,6 +49,16 @@ export interface SerializerOptions {
xml: XmlOptions;
}

/**
* Options to configure authentication.
*/
export interface AuthenticationOptions {
/**
* Optionally swap the operation credential.
*/
credential?: TokenCredential | TokenCredentialRefresher;
}

export type RequiredSerializerOptions = {
[K in keyof SerializerOptions]: Required<SerializerOptions[K]>;
};
Expand Down Expand Up @@ -106,6 +118,10 @@ export interface OperationOptions {
* Options to override serialization/de-serialization behavior.
*/
serializerOptions?: SerializerOptions;
/**
* Options to configure authentication.
*/
authenticationOptions?: AuthenticationOptions;

/**
* A function to be called each time a response is received from the server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Debugger } from '@azure/logger';
import { GetTokenOptions } from '@azure/core-auth';
import { OperationTracingOptions } from '@azure/core-tracing';
import { TokenCredential } from '@azure/core-auth';
import { TokenCredentialRefresher } from '@azure/core-auth';

// @public
export interface AddPipelineOptions {
Expand Down Expand Up @@ -52,7 +53,7 @@ export const bearerTokenAuthenticationPolicyName = "bearerTokenAuthenticationPol
// @public
export interface BearerTokenAuthenticationPolicyOptions {
challengeCallbacks?: ChallengeCallbacks;
credential?: TokenCredential;
credential?: TokenCredential | TokenCredentialRefresher;
scopes: string | string[];
}

Expand Down Expand Up @@ -188,6 +189,9 @@ export interface PipelineRequest {
abortSignal?: AbortSignalLike;
agent?: Agent;
allowInsecureConnection?: boolean;
authenticationOptions?: {
credential?: TokenCredential | TokenCredentialRefresher;
};
body?: RequestBodyType;
disableKeepAlive?: boolean;
formData?: FormDataMap;
Expand Down
11 changes: 11 additions & 0 deletions sdk/core/core-rest-pipeline/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { TokenCredential, TokenCredentialRefresher } from "@azure/core-auth";
import { AbortSignalLike } from "@azure/abort-controller";
import { OperationTracingOptions } from "@azure/core-tracing";

Expand Down Expand Up @@ -156,6 +157,16 @@ export interface PipelineRequest {
*/
tracingOptions?: OperationTracingOptions;

/**
* Options to configure authentication.
*/
authenticationOptions?: {
/**
* Optionally swap the operation credential.
*/
credential?: TokenCredential | TokenCredentialRefresher;
};

/**
* Callback which fires upon upload progress.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { TokenCredential, GetTokenOptions, AccessToken } from "@azure/core-auth";
import {
TokenCredential,
GetTokenOptions,
AccessToken,
TokenCredentialRefresher
} from "@azure/core-auth";
import { PipelineResponse, PipelineRequest, SendRequest } from "../interfaces";
import { PipelinePolicy } from "../pipeline";
import { createTokenCycler } from "../util/tokenCycler";

/**
* The programmatic identifier of the bearerTokenAuthenticationPolicy.
Expand Down Expand Up @@ -75,7 +79,7 @@ export interface BearerTokenAuthenticationPolicyOptions {
/**
* The TokenCredential implementation that can supply the bearer token.
*/
credential?: TokenCredential;
credential?: TokenCredential | TokenCredentialRefresher;
/**
* The scopes for which the bearer token applies.
*/
Expand Down Expand Up @@ -131,14 +135,6 @@ export function bearerTokenAuthenticationPolicy(
...challengeCallbacks
};

// This function encapsulates the entire process of reliably retrieving the token
// The options are left out of the public API until there's demand to configure this.
// Remember to extend `BearerTokenAuthenticationPolicyOptions` with `TokenCyclerOptions`
// in order to pass through the `options` object.
const getAccessToken = credential
? createTokenCycler(credential /* , options */)
: () => Promise.resolve(null);

return {
name: bearerTokenAuthenticationPolicyName,
/**
Expand All @@ -161,6 +157,15 @@ export function bearerTokenAuthenticationPolicy(
);
}

// TODO:
// If we changed the AuthorizeRequestOptions to incldue the credential instead of the getAccessToken method,
// then we could skip this part and just pass any of these credentials directly.
const refreshCredential = request.authenticationOptions?.credential ?? credential;
const getAccessToken =
(refreshCredential as TokenCredentialRefresher)?.refreshToken?.bind(refreshCredential) ??
refreshCredential?.getToken.bind(refreshCredential) ??
(() => Promise.resolve(null));

await callbacks.authorizeRequest({
scopes: Array.isArray(scopes) ? scopes : [scopes],
request,
Expand Down
Loading