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

[Identity] Add the ability to read AZURE_AUTHORITY_HOST from environment #8226

Merged
merged 4 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion sdk/identity/identity/src/client/identityClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
WebResource,
RequestPrepareOptions,
GetTokenOptions,
createPipelineFromOptions
createPipelineFromOptions,
isNode
} from "@azure/core-http";
import { CanonicalCode } from "@opentelemetry/types";
import { AuthenticationError, AuthenticationErrorName } from "./errors";
Expand Down Expand Up @@ -38,6 +39,9 @@ export class IdentityClient extends ServiceClient {
public authorityHost: string;

constructor(options?: TokenCredentialOptions) {
if (isNode) {
options = options || IdentityClient.getEnvironmentOptions();
}
options = options || IdentityClient.getDefaultOptions();
super(
undefined,
Expand Down Expand Up @@ -179,6 +183,12 @@ export class IdentityClient extends ServiceClient {
}
}

static getEnvironmentOptions(): TokenCredentialOptions {
return {
authorityHost: process.env.AZURE_AUTHORITY_HOST
};
}

static getDefaultOptions(): TokenCredentialOptions {
return {
authorityHost: DefaultAuthorityHost
Expand Down
23 changes: 23 additions & 0 deletions sdk/identity/identity/test/identityClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AuthenticationError } from "../src/";
import { IdentityClient } from "../src/client/identityClient";
import { ClientSecretCredential } from "../src";
import { setLogLevel, AzureLogger, getLogLevel, AzureLogLevel } from "@azure/logger";
import { isNode } from "@azure/core-http";

function isExpectedError(expectedErrorName: string): (error: any) => boolean {
return (error: any) => {
Expand Down Expand Up @@ -78,6 +79,28 @@ describe("IdentityClient", function() {
);
});

it("throws an exception when an Env AZURE_AUTHORITY_HOST using 'http' is provided", async () => {
if (isNode) {
process.env.AZURE_AUTHORITY_HOST ="http://totallyinsecure.lol";
assert.throws(
() => {
new IdentityClient();
},
Error,
"The authorityHost address must use the 'https' protocol."
);
process.env.AZURE_AUTHORITY_HOST ="httpsomg.com";
assert.throws(
() => {
new IdentityClient();
},
Error,
"The authorityHost address must use the 'https' protocol."
);
delete process.env.AZURE_AUTHORITY_HOST;
}
});

it("returns a usable error when the authentication response doesn't contain a body", async () => {
const mockHttp = new MockAuthHttpClient({
authResponse: {
Expand Down