Skip to content

Commit

Permalink
Verify MSI creds return AccessToken when used like TokenCredential
Browse files Browse the repository at this point in the history
  • Loading branch information
daviwil committed Jul 22, 2019
1 parent 972c638 commit cbdb4a6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
25 changes: 24 additions & 1 deletion test/credentials/msiAppServiceTokenCredentialTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ describe("MSI App Service Authentication", function () {
expect(response!.tokenType).to.exist;
});

it("should return an AccessToken when invoked as a TokenCredential", async () => {
const mockResponse = {
access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1d",
expires_in: "3599",
expires_on: "1502930996",
resource: "https://management.azure.com/",
token_type: "Bearer"
};

const httpClient = getMockHttpClient(mockResponse);
const msiCredsObj = new MSIAppServiceTokenCredentials({
msiEndpoint: "http://127.0.0.1:41741/MSI/token/",
msiSecret: "69418689F1E342DD946CB82994CDA3CB",
httpClient: httpClient
});

const response = await msiCredsObj.getToken("scope");
expect(response).to.exist;
expect(response!.token).to.exist;
expect(response!.expiresOnTimestamp).to.exist;
});


it('should throw if the response contains "ExceptionMessage"', async function () {
const errorResponse = {
"error": "unknown",
Expand Down Expand Up @@ -155,4 +178,4 @@ describe("MSI App Service Authentication", function () {
}
});
});
});
});
25 changes: 25 additions & 0 deletions test/credentials/msiVmTokenCredentialTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ describe("MSI Vm Authentication", () => {
expect(response!.tokenType).to.exist;
});

it("should return an AccessToken when invoked as a TokenCredential", async () => {
const mockResponse = {
access_token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1d",
refresh_token: "",
expires_in: "3599",
expires_on: "1502930996",
not_before: "1502927096",
resource: "https://management.azure.com/",
token_type: "Bearer"
};

const expectedQuery = {
"apiVersion": "2018-02-01",
"resource": "https://management.azure.com/"
};

const httpClient = setupNockResponse(undefined, expectedQuery, mockResponse);

const msiCredsObj = new MSIVmTokenCredentials({ httpClient: httpClient });
const response = await msiCredsObj.getToken("scope");
expect(response).to.exist;
expect(response!.token).to.exist;
expect(response!.expiresOnTimestamp).to.exist;
});

it("should throw on requests with bad resource", async () => {
const errorMessage = "unknown";
const errorDescription = "Failed to retrieve token from the Active directory. For details see logs in C:\\User1\\Logs\\Plugins\\Microsoft.Identity.MSI\\1.0\\service_identity_0.log";
Expand Down

0 comments on commit cbdb4a6

Please sign in to comment.