-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted Certificate Sample to TypeScript (#7368)
This is the first time we'll have an E2E test running on a TypeScript sample. They all currently run on JS samples.
- Loading branch information
1 parent
2b750fb
commit 1a82bfb
Showing
12 changed files
with
189 additions
and
153 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import yargs from "yargs"; | ||
|
||
interface Arguments { | ||
c: string; | ||
p: number; | ||
r: string | undefined; | ||
s: string; | ||
$0: string; | ||
} | ||
|
||
const argv: Arguments = yargs(process.argv.slice(2)) | ||
.usage("Usage: $0 -p [PORT]") | ||
.options({ | ||
c: { | ||
type: "string", | ||
alias: "cache location", | ||
default: "data/cache.json", | ||
description: | ||
"(Optional) Cache location - default is data/cache.json", | ||
}, | ||
p: { | ||
type: "number", | ||
alias: "port", | ||
default: 3000, | ||
description: "(Optional) Port Number - default is 3000", | ||
}, | ||
r: { | ||
alias: "region", | ||
default: undefined, | ||
description: "(Optional) Region - default is undefined", | ||
}, | ||
s: { | ||
type: "string", | ||
alias: "scenario", | ||
default: "AAD", | ||
description: "(Optional) Scenario name - default is AAD", | ||
}, | ||
}) | ||
.parseSync(); | ||
|
||
export default argv; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
samples/msal-node-samples/client-credentials-with-cert-from-key-vault/app.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { | ||
AuthenticationResult, | ||
ConfidentialClientApplication, | ||
Configuration, | ||
LogLevel, | ||
} from "@azure/msal-node"; | ||
import argv from "../cliArgs"; // command line arguments - see samples/msal-node-samples/cliArgs.ts | ||
|
||
const getClientCredentialsToken = async ( | ||
cca: ConfidentialClientApplication, | ||
clientCredentialRequestScopes: Array<string>, | ||
region?: string | ||
): Promise<AuthenticationResult | null> => { | ||
// With client credentials flows permissions need to be granted in the portal by a tenant administrator. | ||
// The scope is always in the format "<resource>/.default" | ||
const clientCredentialRequest = { | ||
scopes: clientCredentialRequestScopes, | ||
azureRegion: region, // (optional) specify the region you will deploy your application to here (e.g. "westus2") | ||
skipCache: true, // (optional) this skips the cache and forces MSAL to get a new token from Azure AD | ||
}; | ||
|
||
try { | ||
const response: AuthenticationResult | null = | ||
await cca.acquireTokenByClientCredential(clientCredentialRequest); | ||
console.log("Response: ", response); | ||
return response; | ||
} catch (error) { | ||
console.log(JSON.stringify(error)); | ||
throw error; | ||
} | ||
}; | ||
|
||
/** | ||
* The code below checks if the script is being executed manually or in automation. | ||
* If the script was executed manually, it will initialize a ConfidentialClientApplication object | ||
* and execute the sample client credentials application. | ||
*/ | ||
if (argv.$0 === "dist/client-credentials-with-cert-from-key-vault/app.js") { | ||
(async () => { | ||
const clientConfig: Configuration = { | ||
auth: { | ||
clientId: "<ENTER_CLIENT_ID>", | ||
authority: | ||
"https://login.microsoftonline.com/<ENTER_TENANT_ID>", | ||
clientCertificate: { | ||
thumbprintSha256: | ||
"<ENTER_CLIENT_CERTIFICATE_THUMBPRINT_SHA_256>", | ||
privateKey: "ENTER_CLIENT_CERTIFICATE_PRIVATE_KEY", | ||
x5c: "ENTER_CLIENT_CERTIFICATE_X5C", | ||
}, | ||
}, | ||
system: { | ||
loggerOptions: { | ||
loggerCallback(loglevel, message, containsPii) { | ||
console.log(message); | ||
}, | ||
piiLoggingEnabled: false, | ||
logLevel: LogLevel.Verbose, | ||
}, | ||
}, | ||
}; | ||
|
||
const confidentialClientApplication: ConfidentialClientApplication = | ||
new ConfidentialClientApplication(clientConfig); | ||
|
||
await getClientCredentialsToken( | ||
confidentialClientApplication, | ||
["https://graph.microsoft.com/.default"], | ||
argv.r | ||
); | ||
})(); | ||
} | ||
|
||
export default getClientCredentialsToken; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
"AccessToken": {}, | ||
"RefreshToken": {}, | ||
"AppMetadata": {} | ||
} | ||
} |
83 changes: 0 additions & 83 deletions
83
samples/msal-node-samples/client-credentials-with-cert-from-key-vault/index.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.