Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion sdk/core/core-auth/src/tokenCredential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,11 @@ export interface AccessToken {
* @param credential The assumed TokenCredential to be tested.
*/
export function isTokenCredential(credential: any): credential is TokenCredential {
return credential && typeof credential.getToken === "function";
// Check for an object with a 'getToken' function but without
// a 'signRequest' function. We do this check to make sure that
// a ServiceClientCredentials implementor (like TokenClientCredentials
// in ms-rest-nodeauth) doesn't get mistaken for a TokenCredential.
return credential
&& typeof credential.getToken === "function"
&& credential.signRequest === undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps add a comment here as to why the explicit check on signRequest? Will help others who have no idea of ms-rest-nodeauth

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added:

  // Check for an object with a 'getToken' function but without
  // a 'signRequest' function.  We do this check to make sure that
  // a ServiceClientCredentials implementor (like TokenClientCredentials
  // in ms-rest-nodeauth) doesn't get mistaken for a TokenCredential.

}
7 changes: 7 additions & 0 deletions sdk/core/core-auth/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ describe("isTokenCredential", function () {
getToken: true
}), false);
});

it("should return false for an object that has a 'signRequest' field", () => {
assert.strictEqual(isTokenCredential({
getToken: function () { },
signRequest: function() { },
}), false);
})
});
33 changes: 29 additions & 4 deletions sdk/core/core-http/lib/serviceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,34 @@ export class ServiceClient {
if (Array.isArray(options.requestPolicyFactories)) {
requestPolicyFactories = options.requestPolicyFactories;
} else {
requestPolicyFactories = createDefaultRequestPolicyFactories(credentials, options);
let credentialsOrFactory: ServiceClientCredentials | RequestPolicyFactory | undefined = undefined;
if (isTokenCredential(credentials)) {
// Create a wrapped RequestPolicyFactory here so that we can provide the
// correct scope to the BearerTokenAuthenticationPolicy at the first time
// one is requested. This is needed because generated ServiceClient
// implementations do not set baseUri until after ServiceClient's constructor
// is finished, leaving baseUri empty at the time when it is needed to
// build the correct scope name.
const wrappedPolicyFactory: () => RequestPolicyFactory = () => {
let bearerTokenPolicyFactory: RequestPolicyFactory | undefined = undefined;
let serviceClient = this;
return {
create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): RequestPolicy {
if (bearerTokenPolicyFactory === undefined) {
bearerTokenPolicyFactory = bearerTokenAuthenticationPolicy(credentials, `${serviceClient.baseUri || ""}/.default`)
}

return bearerTokenPolicyFactory.create(nextPolicy, options);
}
}
};

credentialsOrFactory = wrappedPolicyFactory();
} else {
credentialsOrFactory = credentials;
}

requestPolicyFactories = createDefaultRequestPolicyFactories(credentialsOrFactory, options);
if (options.requestPolicyFactories) {
const newRequestPolicyFactories: void | RequestPolicyFactory[] = options.requestPolicyFactories(requestPolicyFactories);
if (newRequestPolicyFactories) {
Expand Down Expand Up @@ -395,7 +422,7 @@ function getValueOrFunctionResult(value: undefined | string | ((defaultValue: st
return result;
}

function createDefaultRequestPolicyFactories(credentials: ServiceClientCredentials | TokenCredential | RequestPolicyFactory | undefined, options: ServiceClientOptions): RequestPolicyFactory[] {
function createDefaultRequestPolicyFactories(credentials: ServiceClientCredentials | RequestPolicyFactory | undefined, options: ServiceClientOptions): RequestPolicyFactory[] {
const factories: RequestPolicyFactory[] = [];

if (options.generateClientRequestIdHeader) {
Expand All @@ -405,8 +432,6 @@ function createDefaultRequestPolicyFactories(credentials: ServiceClientCredentia
if (credentials) {
if (isRequestPolicyFactory(credentials)) {
factories.push(credentials);
} else if (isTokenCredential(credentials)) {
factories.push(bearerTokenAuthenticationPolicy(credentials, "/.default"));
} else {
factories.push(signingPolicy(credentials));
}
Expand Down