diff --git a/2-Authorization/0-call-api-vanillajs/SPA/public/fetch.js b/2-Authorization/0-call-api-vanillajs/SPA/public/fetch.js index 3339e31fa..83c79dca4 100644 --- a/2-Authorization/0-call-api-vanillajs/SPA/public/fetch.js +++ b/2-Authorization/0-call-api-vanillajs/SPA/public/fetch.js @@ -1,116 +1,116 @@ -/** - * Execute a fetch request with the given options - * @param {string} method: GET, POST, PUT, DELETE - * @param {String} endpoint: The endpoint to call - * @param {Object} data: The data to send to the endpoint, if any - * @returns response - */ -function callApi(method, endpoint, token, data = null) { - const headers = new Headers(); - const bearer = `Bearer ${token}`; - - headers.append('Authorization', bearer); - - if (data) { - headers.append('Content-Type', 'application/json'); - } - - const options = { - method: method, - headers: headers, - body: data ? JSON.stringify(data) : null, - }; - - return fetch(endpoint, options) - .then((response) => { - const contentType = response.headers.get("content-type"); - - if (contentType && contentType.indexOf("application/json") !== -1) { - return response.json(); - } else { - return response; - } - }); -} - - -/** - * Handles todolist actions - * @param {Object} task - * @param {string} method - * @param {string} endpoint - */ -async function handleToDoListActions(task, method, endpoint) { - let listData; - - try { - const accessToken = await getToken(); - const data = await callApi(method, endpoint, accessToken, task); - - switch (method) { - case 'POST': - listData = JSON.parse(localStorage.getItem('todolist')); - listData = [data, ...listData]; - localStorage.setItem('todolist', JSON.stringify(listData)); - AddTaskToToDoList(data); - break; - case 'DELETE': - listData = JSON.parse(localStorage.getItem('todolist')); - const index = listData.findIndex((todoItem) => todoItem.id === task.id); - localStorage.setItem('todolist', JSON.stringify([...listData.splice(index, 1)])); - showToDoListItems(listData); - break; - default: - console.log('Unrecognized method.') - break; - } - } catch (error) { - console.error(error); - } -} - -/** - * Handles todolist action GET action. - */ -async function getToDos() { - try { - const accessToken = await getToken(); - - const data = await callApi( - 'GET', - protectedResources.toDoListAPI.endpoint, - accessToken - ); - - if (data) { - localStorage.setItem('todolist', JSON.stringify(data)); - showToDoListItems(data); - } - } catch (error) { - console.error(error); - } -} - -/** - * Retrieves an access token. - */ -async function getToken() { - let tokenResponse; - - if (typeof getTokenPopup === 'function') { - tokenResponse = await getTokenPopup({ - scopes: [...protectedResources.toDoListAPI.scopes.read], - redirectUri: '/redirect' - }); - } else { - tokenResponse = await getTokenRedirect({ - scopes: [...protectedResources.toDoListAPI.scopes.read], - }); - } - - if (!tokenResponse) { - return null; - } - - return tokenResponse.accessToken; -} +/** + * Execute a fetch request with the given options + * @param {string} method: GET, POST, PUT, DELETE + * @param {String} endpoint: The endpoint to call + * @param {Object} data: The data to send to the endpoint, if any + * @returns response + */ +function callApi(method, endpoint, token, data = null) { + const headers = new Headers(); + const bearer = `Bearer ${token}`; + + headers.append('Authorization', bearer); + + if (data) { + headers.append('Content-Type', 'application/json'); + } + + const options = { + method: method, + headers: headers, + body: data ? JSON.stringify(data) : null, + }; + + return fetch(endpoint, options) + .then((response) => { + const contentType = response.headers.get("content-type"); + + if (contentType && contentType.indexOf("application/json") !== -1) { + return response.json(); + } else { + return response; + } + }); +} + + +/** + * Handles todolist actions + * @param {Object} task + * @param {string} method + * @param {string} endpoint + */ +async function handleToDoListActions(task, method, endpoint) { + let listData; + + try { + const accessToken = await getToken(); + const data = await callApi(method, endpoint, accessToken, task); + + switch (method) { + case 'POST': + listData = JSON.parse(localStorage.getItem('todolist')); + listData = [data, ...listData]; + localStorage.setItem('todolist', JSON.stringify(listData)); + AddTaskToToDoList(data); + break; + case 'DELETE': + listData = JSON.parse(localStorage.getItem('todolist')); + const index = listData.findIndex((todoItem) => todoItem.id === task.id); + localStorage.setItem('todolist', JSON.stringify([...listData.splice(index, 1)])); + showToDoListItems(listData); + break; + default: + console.log('Unrecognized method.') + break; + } + } catch (error) { + console.error(error); + } +} + +/** + * Handles todolist action GET action. + */ +async function getToDos() { + try { + const accessToken = await getToken(); + + const data = await callApi( + 'GET', + protectedResources.toDoListAPI.endpoint, + accessToken + ); + + if (data) { + localStorage.setItem('todolist', JSON.stringify(data)); + showToDoListItems(data); + } + } catch (error) { + console.error(error); + } +} + +/** + * Retrieves an access token. + */ +async function getToken() { + let tokenResponse; + + if (typeof getTokenPopup === 'function') { + tokenResponse = await getTokenPopup({ + scopes: [...protectedResources.toDoListAPI.scopes.read], + redirectUri: '/redirect' + }); + } else { + tokenResponse = await getTokenRedirect({ + scopes: [...protectedResources.toDoListAPI.scopes.read], + }); + } + + if (!tokenResponse) { + return null; + } + + return tokenResponse.accessToken; +} diff --git a/2-Authorization/3-call-api-node-daemon/API/ToDoListAPI/appsettings.json b/2-Authorization/3-call-api-node-daemon/API/ToDoListAPI/appsettings.json index 586df5bd4..b4e75f1f6 100644 --- a/2-Authorization/3-call-api-node-daemon/API/ToDoListAPI/appsettings.json +++ b/2-Authorization/3-call-api-node-daemon/API/ToDoListAPI/appsettings.json @@ -1,24 +1,24 @@ -{ - "AzureAd": { - "Instance": "https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/", - "TenantId": "Enter_the_Tenant_Id_Here", - "ClientId": "Enter_the_Application_Id_Here", - "Scopes": { - "Read": ["ToDoList.Read", "ToDoList.ReadWrite"], - "Write": ["ToDoList.ReadWrite"] - }, - "AppPermissions": { - "Read": ["ToDoList.Read.All", "ToDoList.ReadWrite.All"], - "Write": ["ToDoList.ReadWrite.All"] - } - }, - "https_port": 44351, - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, - "AllowedHosts": "*" -} +{ + "AzureAd": { + "Instance": "https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/", + "TenantId": "Enter_the_Tenant_Id_Here", + "ClientId": "Enter_the_Application_Id_Here", + "Scopes": { + "Read": ["ToDoList.Read", "ToDoList.ReadWrite"], + "Write": ["ToDoList.ReadWrite"] + }, + "AppPermissions": { + "Read": ["ToDoList.Read.All", "ToDoList.ReadWrite.All"], + "Write": ["ToDoList.ReadWrite.All"] + } + }, + "https_port": 44351, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/2-Authorization/3-call-api-node-daemon/App/.env b/2-Authorization/3-call-api-node-daemon/App/.env index b4269ed4d..9d845288e 100644 --- a/2-Authorization/3-call-api-node-daemon/App/.env +++ b/2-Authorization/3-call-api-node-daemon/App/.env @@ -1,3 +1,3 @@ -# This variable should only be used in the development environment. -# Please remove the variable when moving the app to the production environment. -NODE_TLS_REJECT_UNAUTHORIZED='0' +# This variable should only be used in the development environment. +# Please remove the variable when moving the app to the production environment. +NODE_TLS_REJECT_UNAUTHORIZED='0' diff --git a/2-Authorization/3-call-api-node-daemon/App/auth.js b/2-Authorization/3-call-api-node-daemon/App/auth.js index dd08d049c..8d0efbb7d 100644 --- a/2-Authorization/3-call-api-node-daemon/App/auth.js +++ b/2-Authorization/3-call-api-node-daemon/App/auth.js @@ -1,39 +1,36 @@ -const msal = require('@azure/msal-node'); - -const { msalConfig, protectedResources } = require('./authConfig'); - -/** - * With client credentials flows permissions need to be granted in the portal by a tenant administrator. - * The scope is always in the format '/.default'. For more, visit: - * https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow - */ -const tokenRequest = { - scopes: [`${protectedResources.apiToDoList.scopes}/.default`], -}; - -const apiConfig = { - uri: protectedResources.apiToDoList.endpoint, -}; - - - -/** - * Initialize a confidential client application. For more info, visit: - * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/initialize-confidential-client-application.md - */ - -const cca = new msal.ConfidentialClientApplication(msalConfig); - -/** - * Acquires token with client credentials. - * @param {object} tokenRequest - */ -async function getToken(tokenRequest) { - return await cca.acquireTokenByClientCredential(tokenRequest); -} - -module.exports = { - apiConfig: apiConfig, - tokenRequest: tokenRequest, - getToken: getToken, -}; \ No newline at end of file +import { ConfidentialClientApplication } from '@azure/msal-node'; + +import { msalConfig, protectedResources } from './authConfig.js'; + + +/** + * With client credentials flows permissions need to be granted in the portal by a tenant administrator. + * The scope is always in the format '/.default'. For more, visit: + * https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow + */ +export const tokenRequest = { + // scopes: [`${protectedResources.apiToDoList.scope}/.default`], // tried this in case the array format in authConfig was throwing things off. + scopes: [`${protectedResources.apiToDoList.scopes}/.default`], + authority: msalConfig.auth.authority, +// this doesn't help + // skipCache: true, +}; + +export const apiConfig = { + uri: protectedResources.apiToDoList.endpoint, +}; + +/** + * Initialize a confidential client application. For more info, visit: + * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/initialize-confidential-client-application.md + */ + +const cca = new ConfidentialClientApplication(msalConfig); + +/** + * Acquires token with client credentials. + * @param {object} tokenRequest + */ +export async function getToken(tokenRequest) { + return await cca.acquireTokenByClientCredential(tokenRequest); +} \ No newline at end of file diff --git a/2-Authorization/3-call-api-node-daemon/App/authConfig.js b/2-Authorization/3-call-api-node-daemon/App/authConfig.js index 1e963ff69..71c8414df 100644 --- a/2-Authorization/3-call-api-node-daemon/App/authConfig.js +++ b/2-Authorization/3-call-api-node-daemon/App/authConfig.js @@ -1,46 +1,41 @@ -/* - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. - */ - -require('dotenv').config(); -const fs = require('fs'); - -/** - * Configuration object to be passed to MSAL instance on creation. - * For a full list of MSAL Node configuration parameters, visit: - * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/configuration.md - */ - -const msalConfig = { - auth: { - clientId: process.env.CLIENT_ID || 'Enter_the_Application_Id_Here', // 'Application (client) ID' of app registration in Microsoft Entra admin center - this value is a GUID - authority: process.env.AUTHORITY || 'https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/', // Replace the placeholder with your tenant subdomain - clientSecret: process.env.CLIENT_SECRET || 'Enter_the_Client_Secret_Here', // Client secret generated from the app registration in Microsoft Entra admin center - // clientCertificate: { - // thumbprint: process.env.CERT_THUMBPRINT || 'YOUR_CERT_THUMBPRINT', // replace with thumbprint obtained during step 2 above - // privateKey: fs.readFileSync(process.env.CERT_PRIVATE_KEY_FILE || 'PATH_TO_YOUR_PRIVATE_KEY_FILE'), // e.g. c:/Users/diego/Desktop/example.key - // }, - }, - system: { - loggerOptions: { - loggerCallback(loglevel, message, containsPii) { - console.log(message); - }, - piiLoggingEnabled: false, - logLevel: 'Info', - }, - }, -}; - -const protectedResources = { - apiToDoList: { - endpoint: process.env.API_ENDPOINT || 'https://localhost:44351/api/todolist', - scopes: [process.env.SCOPES || 'api://Enter_the_Web_Api_Application_Id_Here'], - }, -}; - -module.exports = { - msalConfig, - protectedResources, -}; +/* + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import 'dotenv/config'; +import 'fs'; + +/** + * Configuration object to be passed to MSAL instance on creation. + * For a full list of MSAL Node configuration parameters, visit: + * https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/configuration.md + */ + +export const msalConfig = { + auth: { + clientId: process.env.CLIENT_ID || 'Enter_the_Application_Id_Here', // 'Application (client) ID' of app registration in Microsoft Entra admin center - this value is a GUID + authority: process.env.AUTHORITY || 'https://Enter_the_Tenant_Subdomain_Here.ciamlogin.com/', // Replace the placeholder with your tenant subdomain + clientSecret: process.env.CLIENT_SECRET || 'Enter_the_Client_Secret_Here', // Client secret generated from the app registration in Microsoft Entra admin center + // clientCertificate: { + // thumbprint: process.env.CERT_THUMBPRINT || 'YOUR_CERT_THUMBPRINT', // replace with thumbprint obtained during step 2 above + // privateKey: fs.readFileSync(process.env.CERT_PRIVATE_KEY_FILE || 'PATH_TO_YOUR_PRIVATE_KEY_FILE'), // e.g. c:/Users/diego/Desktop/example.key + // }, + }, + system: { + loggerOptions: { + loggerCallback(loglevel, message, containsPii) { + console.log(message); + }, + piiLoggingEnabled: false, + logLevel: 'Info', + }, + }, +}; + +export const protectedResources = { + apiToDoList: { + endpoint: process.env.API_ENDPOINT || 'https://localhost:44351/api/todolist', + scopes: [process.env.SCOPES || 'api://Enter_the_Web_Api_Application_Id_Here'], + }, +}; \ No newline at end of file diff --git a/2-Authorization/3-call-api-node-daemon/App/fetch.js b/2-Authorization/3-call-api-node-daemon/App/fetch.js index db98184e7..1b25800de 100644 --- a/2-Authorization/3-call-api-node-daemon/App/fetch.js +++ b/2-Authorization/3-call-api-node-daemon/App/fetch.js @@ -1,29 +1,25 @@ -const axios = require('axios'); - -/** - * Calls the endpoint with authorization bearer token. - * @param {string} endpoint - * @param {string} accessToken - */ -async function callApi(endpoint, accessToken) { - - const options = { - headers: { - Authorization: `Bearer ${accessToken}` - } - }; - - console.log('request made to web API at: ' + new Date().toString()); - - try { - const response = await axios.get(endpoint, options); - return response.data; - } catch (error) { - console.log(error) - return error; - } -}; - -module.exports = { - callApi: callApi +import axios from 'axios'; + +/** + * Calls the endpoint with authorization bearer token. + * @param {string} endpoint + * @param {string} accessToken + */ +export async function callApi(endpoint, accessToken) { + + const options = { + headers: { + Authorization: `Bearer ${accessToken}` + } + }; + + console.log('request made to web API at: ' + new Date().toString()); + + try { + const response = await axios.get(endpoint, options); + return response.data; + } catch (error) { + console.log(error) + return error; + } }; \ No newline at end of file diff --git a/2-Authorization/3-call-api-node-daemon/App/index.js b/2-Authorization/3-call-api-node-daemon/App/index.js index 5a34a592b..7bbcf32c1 100644 --- a/2-Authorization/3-call-api-node-daemon/App/index.js +++ b/2-Authorization/3-call-api-node-daemon/App/index.js @@ -1,38 +1,18 @@ -#!/usr/bin/env node - -// read in env settings - -require('dotenv').config(); - -const yargs = require('yargs'); -const fetch = require('./fetch'); -const auth = require('./auth'); - -const options = yargs - .usage('Usage: --op ') - .option('op', { alias: 'operation', describe: 'operation name', type: 'string', demandOption: true }) - .argv; - -async function main() { - console.log(`You have selected: ${options.op}`); - - switch (yargs.argv['op']) { - case 'getToDos': - - try { - const authResponse = await auth.getToken(auth.tokenRequest); - const todos = await fetch.callApi(auth.apiConfig.uri, authResponse.accessToken); - - console.log(todos); - } catch (error) { - console.log(error); - } - - break; - default: - console.log('Select an operation first'); - break; - } -}; - +#!/usr/bin/env node + +import 'dotenv/config'; +import { callApi } from './fetch.js'; +import { apiConfig, getToken, tokenRequest } from './auth.js'; + +async function main() { + try { + const authResponse = await getToken(tokenRequest); + const todos = await callApi(apiConfig.uri, authResponse.accessToken); + + console.log(todos); + } catch (error) { + console.log(error); + } +}; + main(); \ No newline at end of file diff --git a/2-Authorization/3-call-api-node-daemon/App/package-lock.json b/2-Authorization/3-call-api-node-daemon/App/package-lock.json new file mode 100644 index 000000000..8d4dd0247 --- /dev/null +++ b/2-Authorization/3-call-api-node-daemon/App/package-lock.json @@ -0,0 +1,302 @@ +{ + "name": "3-call-api-node-console", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "3-call-api-node-console", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "@azure/msal-node": "^2.9.2", + "axios": "^1.7.2", + "dotenv": "^16.4.5" + }, + "bin": { + "msal-node-cli": "index.js" + } + }, + "node_modules/@azure/msal-common": { + "version": "14.12.0", + "resolved": "https://registry.npmjs.org/@azure/msal-common/-/msal-common-14.12.0.tgz", + "integrity": "sha512-IDDXmzfdwmDkv4SSmMEyAniJf6fDu3FJ7ncOjlxkDuT85uSnLEhZi3fGZpoR7T4XZpOMx9teM9GXBgrfJgyeBw==", + "license": "MIT", + "engines": { + "node": ">=0.8.0" + } + }, + "node_modules/@azure/msal-node": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/@azure/msal-node/-/msal-node-2.9.2.tgz", + "integrity": "sha512-8tvi6Cos3m+0KmRbPjgkySXi+UQU/QiuVRFnrxIwt5xZlEEFa69O04RTaNESGgImyBBlYbo2mfE8/U8Bbdk1WQ==", + "license": "MIT", + "dependencies": { + "@azure/msal-common": "14.12.0", + "jsonwebtoken": "^9.0.0", + "uuid": "^8.3.0" + }, + "engines": { + "node": ">=16" + } + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" + }, + "node_modules/axios": { + "version": "1.7.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.2.tgz", + "integrity": "sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha512-zRpUiDwd/xk6ADqPMATG8vc9VPrkck7T07OIx0gnjmJAnHnTVXNQG3vfvWNuiZIkwu9KrKdA1iJKfsfTVxE6NA==", + "license": "BSD-3-Clause" + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, + "node_modules/ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/jsonwebtoken": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-9.0.2.tgz", + "integrity": "sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==", + "license": "MIT", + "dependencies": { + "jws": "^3.2.2", + "lodash.includes": "^4.3.0", + "lodash.isboolean": "^3.0.3", + "lodash.isinteger": "^4.0.4", + "lodash.isnumber": "^3.0.3", + "lodash.isplainobject": "^4.0.6", + "lodash.isstring": "^4.0.1", + "lodash.once": "^4.0.0", + "ms": "^2.1.1", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=12", + "npm": ">=6" + } + }, + "node_modules/jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "license": "MIT", + "dependencies": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "license": "MIT", + "dependencies": { + "jwa": "^1.4.1", + "safe-buffer": "^5.0.1" + } + }, + "node_modules/lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha512-W3Bx6mdkRTGtlJISOvVD/lbqjTlPPUDTMnlXZFnVwi9NKJ6tiAk6LVdlhZMm17VZisqhKcgzpO5Wz91PCt5b0w==", + "license": "MIT" + }, + "node_modules/lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha512-Bz5mupy2SVbPHURB98VAcw+aHh4vRV5IPNhILUCsOzRmsTmSQ17jIuqopAentWoehktxGd9e/hbIXq980/1QJg==", + "license": "MIT" + }, + "node_modules/lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha512-DBwtEWN2caHQ9/imiNeEA5ys1JoRtRfY3d7V9wkqtbycnAmTvRRmbHKDV4a0EYc678/dia0jrte4tjYwVBaZUA==", + "license": "MIT" + }, + "node_modules/lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha512-QYqzpfwO3/CWf3XP+Z+tkQsfaLL/EnUlXWVkIk5FUPc4sBdTehEqZONuyRt2P67PXAk+NXmTBcc97zw9t1FQrw==", + "license": "MIT" + }, + "node_modules/lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==", + "license": "MIT" + }, + "node_modules/lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha512-0wJxfxH1wgO3GrbuP+dTTk7op+6L41QCXbGINEmD+ny/G/eCqGzxyCsh7159S+mgDDcoarnBw6PC1PS5+wUGgw==", + "license": "MIT" + }, + "node_modules/lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha512-Sb487aTOCr9drQVL8pIxOzVhafOjZN9UU54hiN8PU3uAiSV7lx1yYNpbNmex2PK6dSJoNTSJUUswT651yww3Mg==", + "license": "MIT" + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/semver": { + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz", + "integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "license": "MIT", + "bin": { + "uuid": "dist/bin/uuid" + } + } + } +} diff --git a/2-Authorization/3-call-api-node-daemon/App/package.json b/2-Authorization/3-call-api-node-daemon/App/package.json index 70e8db473..251fdf8d7 100644 --- a/2-Authorization/3-call-api-node-daemon/App/package.json +++ b/2-Authorization/3-call-api-node-daemon/App/package.json @@ -1,19 +1,19 @@ -{ - "name": "3-call-api-node-console", - "version": "1.0.0", - "description": "", - "main": "index.js", - "bin": { - "msal-node-cli": "index.js" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "license": "ISC", - "dependencies": { - "@azure/msal-node": "^2.7.0", - "axios": "^1.6.8", - "dotenv": "^16.4.5", - "yargs": "^17.7.2" - } -} +{ + "name": "3-call-api-node-console", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "bin": { + "msal-node-cli": "index.js" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "license": "ISC", + "dependencies": { + "@azure/msal-node": "^2.9.2", + "axios": "^1.7.2", + "dotenv": "^16.4.5" + } +} diff --git a/2-Authorization/3-call-api-node-daemon/README.md b/2-Authorization/3-call-api-node-daemon/README.md index 899767550..77d38113f 100644 --- a/2-Authorization/3-call-api-node-daemon/README.md +++ b/2-Authorization/3-call-api-node-daemon/README.md @@ -1,411 +1,411 @@ ---- -page_type: sample -name: A Node.js daemon application secured by MSAL Node on Microsoft identity platform -description: This sample demonstrates how to use MSAL Node to acquire an access token for a protected resource in a daemon application using the application's own identity with the client credentials flow -languages: - - javascript - - csharp -products: - - entra-external-id - - aspnet-core - - msal-js - - microsoft-identity-web -urlFragment: ms-identity-ciam-javascript-tutorial-3-call-api-node-daemon -extensions: - services: - - ms-identity - platform: - - JavaScript - endpoint: - - AAD v2.0 - level: - - 200 - client: - - Node.js daemon app - service: - - ASP.NET Core web API ---- - -# A Node.js daemon application secured by MSAL Node on Microsoft identity platform - -* [Overview](#overview) -* [Scenario](#scenario) -* [Contents](#contents) -* [Prerequisites](#prerequisites) -* [Setup the sample](#setup-the-sample) -* [Explore the sample](#explore-the-sample) -* [Troubleshooting](#troubleshooting) -* [About the code](#about-the-code) -* [How to deploy this sample to Azure](#how-to-deploy-this-sample-to-azure) -* [Contributing](#contributing) -* [Learn More](#learn-more) - -## Overview - -This sample demonstrates how to use [MSAL Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node) to acquire an access token for a protected resource in a daemon application using the application's own identity with the ([client credentials flow](https://learn.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)). - -Here you'll learn about [access tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens), [token validation](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens), [application permissions](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), and more. - -## Scenario - -1. The Node.js daemon app obtains a JWT [Access Token](https://aka.ms/access-tokens) from **Microsoft Entra External ID**. -1. The **access token** is used as a *bearer* token to authorize the user to call the ASP.NET Core web API protected by **Microsoft Entra External ID**. -1. The service uses the [Microsoft.Identity.Web](https://aka.ms/microsoft-identity-web) to protect the Web api, check permissions and validate tokens. - -![Scenario Image](./ReadmeFiles/topology.png). - -## Contents - -| File/folder | Description | -|---------------------------------|-----------------------------------------------------------| -| `App/.env` | Authentication parameters for the daemon app reside here.| -| `App/auth.js` | MSAL Node is initialized here. | -| `App/fetch.js` | logic to call the API reside here. | -| `API/ToDoListAPI/appsettings.json` | Authentication parameters for the API reside here. | -| `API/ToDoListAPI/Startup.cs` | Microsoft.Identity.Web is initialized here. | - -## Prerequisites - -* Either [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Visual Studio Code](https://code.visualstudio.com/download) and [.NET Core SDK](https://www.microsoft.com/net/learn/get-started) -* An **Microsoft Entra External ID** tenant. For more information, see: [How to get an external tenant](https://github.com/microsoft/entra-previews/blob/PP2/docs/1-Create-a-CIAM-tenant.md) -* A user account in your **Microsoft Entra External ID** tenant. - -## Setup the sample - -### Step 1: Clone or download this repository - -From your shell or command line: - -```console -git clone https://github.com/Azure-Samples/ms-identity-ciam-javascript-tutorial.git -``` - -or download and extract the repository *.zip* file. - -> :warning: To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive. - -### Step 2: Install project dependencies - -```console - cd 2-Authorization\3-call-api-node-daemon\App - npm install -``` - -### Step 3: Register the sample application(s) in your tenant - -There are two projects in this sample. Each needs to be separately registered in your external tenant. To register these projects, you can: - -- follow the steps below for manually register your apps -- or use PowerShell scripts that: - - **automatically** creates the Microsoft Entra applications and related objects (passwords, permissions, dependencies) for you. - - modify the projects' configuration files. - -
- Expand this section if you want to use this automation: - -> :warning: If you have never used **Microsoft Graph PowerShell** before, we recommend you go through the [App Creation Scripts Guide](./AppCreationScripts/AppCreationScripts.md) once to ensure that your environment is prepared correctly for this step. - -1. Ensure that you have [PowerShell 7](https://learn.microsoft.com/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3) or later. -1. Run the script to create your Azure AD application and configure the code of the sample application accordingly. -1. For interactive process -in PowerShell, run: - - ```PowerShell - cd .\AppCreationScripts\ - .\Configure.ps1 -TenantId "[Optional] - your tenant id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'" - ``` - -> Other ways of running the scripts are described in [App Creation Scripts guide](./AppCreationScripts/AppCreationScripts.md). The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios. - -> :information_source: This sample can make use of client certificates. You can use **AppCreationScripts** to register an Azure AD application with certificates. See: [How to use certificates instead of client secrets](./README-use-certificate.md) - -
- -#### Choose the Microsoft Entra External ID tenant where you want to create your applications - -To manually register the apps, as a first step you'll need to: - -1. Sign in to the [Microsoft Entra admin center](https://entra.microsoft.com/). -1. If your account is present in more than one Microsoft Entra External ID tenant, select your profile at the top right corner in the menu on top of the page, and then **switch directory** to change your portal session to the desired Microsoft Entra External ID tenant. - -#### Register the service app (ciam-msal-dotnet-api) - -1. Navigate to the [Microsoft Entra admin center](https://entra.microsoft.com/) and select the **Microsoft Entra External ID** service. -1. Select the **App Registrations** blade on the left, then select **New registration**. -1. In the **Register an application page** that appears, enter your application's registration information: - 1. In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `ciam-msal-dotnet-api`. - 1. Under **Supported account types**, select **Accounts in this organizational directory only** - 1. Select **Register** to create the application. -1. In the **Overview** blade, find and note the **Application (client) ID** and **Directory (tenant) ID**. You use this value in your app's configuration file(s) later in your code. -1. In the app's registration screen, select the **Expose an API** blade to the left to open the page where you can publish the permission as an API for which client applications can obtain [access tokens](https://aka.ms/access-tokens) for. The first thing that we need to do is to declare the unique [resource](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow) URI that the clients will be using to obtain access tokens for this API. To declare an resource URI(Application ID URI), follow the following steps: - 1. Select **Set** next to the **Application ID URI** to generate a URI that is unique for this app. - 1. For this sample, accept the proposed Application ID URI (`api://{clientId}`) by selecting **Save**. - > :information_source: Read more about Application ID URI at [Validation differences by supported account types (signInAudience)](https://docs.microsoft.com/azure/active-directory/develop/supported-accounts-validation). - -##### Publish Delegated Permissions - -1. All APIs must publish a minimum of one [scope](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-authorization-code), also called [Delegated Permission](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), for the client apps to obtain an access token for a *user* successfully. To publish a scope, follow these steps: -1. Select **Add a scope** button open the **Add a scope** screen and Enter the values as indicated below: - 1. For **Scope name**, use `ToDoList.Read`. - 1. For **Admin consent display name** type in *ToDoList.Read*. - 1. For **Admin consent description** type in *e.g. Allows the app to read the signed-in user's files.*. - 1. Keep **State** as **Enabled**. - 1. Select the **Add scope** button on the bottom to save this scope. - 1. Repeat the steps above for another scope named **ToDoList.ReadWrite** -1. Select the **Manifest** blade on the left. - 1. Set `accessTokenAcceptedVersion` property to **2**. - 1. Select on **Save**. - -> :information_source: Follow [the principle of least privilege when publishing permissions](https://learn.microsoft.com/security/zero-trust/develop/protected-api-example) for a web API. - -##### Publish Application Permissions - -1. All APIs should publish a minimum of one [App role for applications](https://docs.microsoft.com/azure/active-directory/develop/howto-add-app-roles-in-azure-ad-apps#assign-app-roles-to-applications), also called [Application Permission](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), for the client apps to obtain an access token as *themselves*, i.e. when they are not signing-in a user. **Application permissions** are the type of permissions that APIs should publish when they want to enable client applications to successfully authenticate as themselves and not need to sign-in users. To publish an application permission, follow these steps: -1. Still on the same app registration, select the **App roles** blade to the left. -1. Select **Create app role**: - 1. For **Display name**, enter a suitable name for your application permission, for instance **ToDoList.Read.All**. - 1. For **Allowed member types**, choose **Application** to ensure other applications can be granted this permission. - 1. For **Value**, enter **ToDoList.Read.All**. - 1. For **Description**, enter *e.g. Allows the app to read the signed-in user's files.*. - 1. Select **Apply** to save your changes. - 1. Repeat the steps above for another app permission named **ToDoList.ReadWrite.All** - -##### Configure Optional Claims - -1. Still on the same app registration, select the **Token configuration** blade to the left. -1. Select **Add optional claim**: - 1. Select **optional claim type**, then choose **Access**. - 1. Select the optional claim **idtyp**. - > Indicates token type. This claim is the most accurate way for an API to determine if a token is an app token or an app+user token. This is not issued in tokens issued to users. - 1. Select **Add** to save your changes. - -##### Configure the service app (ciam-msal-dotnet-api) to use your app registration - -Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code. - -> In the steps below, "ClientID" is the same as "Application ID" or "AppId". - -1. Open the `API\ToDoListAPI\appsettings.json` file. -1. Find the key `Enter_the_Application_Id_Here` and replace the existing value with the application ID (clientId) of `ciam-msal-dotnet-api` app copied from the Microsoft Entra admin center. -1. Find the key `Enter_the_Tenant_Id_Here` and replace the existing value with your external tenant/directory ID. -1. Find the placeholder `Enter_the_Tenant_Subdomain_Here` and replace it with the Directory (tenant) subdomain. For instance, if your tenant primary domain is `contoso.onmicrosoft.com`, use `contoso`. If you don't have your tenant domain name, learn how to [read your tenant details](https://review.learn.microsoft.com/azure/active-directory/external-identities/customers/how-to-create-customer-tenant-portal#get-the-customer-tenant-details). - -#### Register the client app (ciam-msal-node-daemon) - -1. Navigate to the [Microsoft Entra admin center](https://entra.microsoft.com/) and select the **Microsoft Entra External ID** service. -1. Select the **App Registrations** blade on the left, then select **New registration**. -1. In the **Register an application page** that appears, enter your application's registration information: - 1. In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `ciam-msal-node-daemon`. - 1. Under **Supported account types**, select **Accounts in this organizational directory only** - 1. Select **Register** to create the application. -1. In the **Overview** blade, find and note the **Application (client) ID**. You use this value in your app's configuration file(s) later in your code. -1. In the app's registration screen, select the **Certificates & secrets** blade in the left to open the page where you can generate secrets and upload certificates. -1. In the **Client secrets** section, select **New client secret**: - 1. Type a key description (for instance `app secret`). - 1. Select one of the available key durations (**6 months**, **12 months** or **Custom**) as per your security posture. - 1. The generated key value will be displayed when you select the **Add** button. Copy and save the generated value for use in later steps. - 1. You'll need this key later in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Microsoft Entra admin center before navigating to any other screen or blade. - > :warning: For enhanced security, consider using **certificates** instead of client secrets. See: [How to use certificates instead of secrets](./README-use-certificate.md). -1. Since this app signs-in as itself using the [OAuth 2\.0 client credentials flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow), we will now proceed to select **application permissions**, which is required by apps authenticating as themselves. - 1. In the app's registration screen, select the **API permissions** blade in the left to open the page where we add access to the APIs that your application needs: - 1. Select the **Add a permission** button and then: - 1. Ensure that the **My APIs** tab is selected. - 1. In the list of APIs, select the API `ciam-msal-dotnet-api`. - 1. We will select “Application permissions”, which should be the type of permissions that apps should use when they are authenticating just as themselves and not signing-in users. - 1. In the **Application permissions** section, select the **ToDoList.Read.All**, **ToDoList.ReadWrite.All** in the list. Use the search box if necessary. - 1. Select the **Add permissions** button at the bottom. -1. At this stage, the permissions are assigned correctly but since the client app does not allow users to interact, the users' themselves cannot consent to these permissions. To get around this problem, we'd let the [tenant administrator consent on behalf of all users in the tenant](https://docs.microsoft.com/azure/active-directory/develop/v2-admin-consent). Select the **Grant admin consent for {tenant}** button, and then select **Yes** when you are asked if you want to grant consent for the requested permissions for all accounts in the tenant. You need to be a tenant admin to be able to carry out this operation. - -##### Configure the client app (ciam-msal-node-daemon) to use your app registration - -Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code. - -> In the steps below, "ClientID" is the same as "Application ID" or "AppId". - -1. Open the `APP\.env` file. -1. Find the key `Enter_the_Application_Id_Here` and replace the existing value with the application ID (clientId) of `ciam-msal-node-daemon` app copied from the Microsoft Entra admin center. -1. Find the placeholder `Enter_the_Tenant_Subdomain_Here` and replace it with the Directory (tenant) subdomain. For instance, if your tenant primary domain is `contoso.onmicrosoft.com`, use `contoso`. If you don't have your tenant domain name, learn how to [read your tenant details](https://review.learn.microsoft.com/azure/active-directory/external-identities/customers/how-to-create-customer-tenant-portal#get-the-customer-tenant-details). -1. Find the key `Enter_the_Client_Secret_Here` and replace the existing value with the generated secret that you saved during the creation of `ciam-msal-node-daemon` copied from the Microsoft Entra admin center. -1. Find the key `Enter_the_Web_Api_Application_Id_Here` and replace the existing value with the application ID (clientId) of `ciam-msal-dotnet-api` app copied from the Microsoft Entra admin center. - -### Step 4: Running the sample - -From your shell or command line, execute the following commands: - -```console - cd 2-Authorization\3-call-api-node-daemon\API\ToDoListAPI - dotnet run -``` - -Then, open a separate command terminal and run: - -```console - 2-Authorization\3-call-api-node-daemon\App - node . --op getToDos -``` - -## Explore the sample - -1. Navigate to the [app](./App/) root directory in the terminal. -1. Enter the following command `node . --op getToDos`. - -![Screenshot](./ReadmeFiles/Screenshot.png) - -> :information_source: Did the sample not work for you as expected? Then please reach out to us using the [GitHub Issues](../../../../issues) page. - -## We'd love your feedback! - -Were we successful in addressing your learning objective? Consider taking a moment to [share your experience with us](https://forms.office.com/Pages/ResponsePage.aspx?id=v4j5cvGGr0GRqy180BHbR_ivMYEeUKlEq8CxnMPgdNZUNDlUTTk2NVNYQkZSSjdaTk5KT1o4V1VVNS4u). - -## Troubleshooting - -Use [Stack Overflow](http://stackoverflow.com/questions/tagged/msal) to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [`azure-active-directory` `node` `ms-identity` `adal` `msal`]. - -If you find a bug in the sample, raise the issue on [GitHub Issues](../../../../issues). - -## About the code - -You need to instantiate **MSAL Node** as a [ConfidentialClientApplication](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/initialize-confidential-client-application.md) to support the ([client credentials flow](https://learn.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)). - -```javascript -const msal = require('@azure/msal-node'); -const cca = new msal.ConfidentialClientApplication(msalConfig); -``` - -### Acquire a token - -**Access Token** requests in **MSAL.js** are meant to be *per-resource-per-scope(s)*. This means that an **Access Token** requested for resource **A** with scope `scp1`: - -* cannot be used for accessing resource **A** with scope `scp2`, and, -* cannot be used for accessing resource **B** of any scope. - -The intended recipient of an **Access Token** is represented by the `aud` claim; in case the value for the `aud` claim does not mach the resource APP ID URI, the token should be considered invalid. Likewise, the permissions that an Access Token grants is represented by the `roles` claim. See [Access Token claims](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#payload-claims) for more information. - -To get the **access token** for a protected resource using the client credentials grant flow, **MSAL Node** exposes `acquireTokenByClientCredential` API to grant permission to the application to get an **access token**. - -```javascript -const cca = new msal.ConfidentialClientApplication(msalConfig); - -async function getToken(tokenRequest) { - return await cca.acquireTokenByClientCredential(tokenRequest); -} -``` - -### Access token validation - -On the web API side, the `AddMicrosoftIdentityWebApi` method in [Program.cs](./API/ToDoListAPI/Program.cs) protects the web API by [validating access tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens) sent tho this API. Check out [Protected web API: Code configuration](https://docs.microsoft.com/azure/active-directory/develop/scenario-protected-web-api-app-configuration) which explains the inner workings of this method in more detail. - -```csharp -builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) - .AddMicrosoftIdentityWebApi(options => - { - builder.Configuration.Bind("AzureAd", options); - options.Events = new JwtBearerEvents(); - }, options => { builder.Configuration.Bind("AzureAd", options); }); -``` - -For validation and debugging purposes, developers can decode **JWT**s (*JSON Web Tokens*) using [jwt.ms](https://jwt.ms). - -### Verifying permissions - -Access tokens that have neither the **scp** (for delegated permissions) nor **roles** (for application permissions) claim with the required scopes/permissions should not be accepted. In the sample, this is illustrated via the `RequiredScopeOrAppPermission` attribute in [ToDoListController.cs](./API/ToDoListAPI/Controllers/ToDoListController.cs): - -```csharp -[HttpGet] - [RequiredScopeOrAppPermission( - RequiredScopesConfigurationKey = "AzureAD:Scopes:Read", - RequiredAppPermissionsConfigurationKey = "AzureAD:AppPermissions:Read" - )] - public async Task GetAsync() - { - var toDos = await _toDoContext.ToDos! - .Where(td => RequestCanAccessToDo(td.Owner)) - .ToListAsync(); - return Ok(toDos); - } -``` - -### Access to data - -Web API endpoints should be prepared to accept calls from both users and applications, and should have control structures in place to respond to each accordingly. For instance, a call from a user via delegated permissions should be responded with user's data, while a call from an application via application permissions might be responded with the entire todolist. This is illustrated in the [ToDoListController](./API/ToDoListAPI/Controllers/ToDoListController.cs) controller: - -```csharp -private bool RequestCanAccessToDo(Guid userId) - { - return IsAppMakingRequest() || (userId == GetUserId()); - } -[HttpGet] - [RequiredScopeOrAppPermission( - RequiredScopesConfigurationKey = "AzureAD:Scopes:Read", - RequiredAppPermissionsConfigurationKey = "AzureAD:AppPermissions:Read" - )] - public async Task GetAsync() - { - var toDos = await _toDoContext.ToDos! - .Where(td => RequestCanAccessToDo(td.Owner)) - .ToListAsync(); - return Ok(toDos); - } -``` - -When granting access to data based on scopes, be sure to follow [the principle of least privilege](https://docs.microsoft.com/azure/active-directory/develop/secure-least-privileged-access). - -### Debugging the sample - -To debug the .NET Core web API that comes with this sample, install the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) for Visual Studio Code. - -Learn more about using [.NET Core with Visual Studio Code](https://docs.microsoft.com/dotnet/core/tutorials/with-visual-studio-code). - -## How to deploy this sample to Azure - -
-Expand the section - -### Deploying web API to Azure App Services - -There is one web API in this sample. To deploy it to **Azure App Services**, you'll need to: - -* create an **Azure App Service** -* publish the projects to the **App Services** - -> :warning: Please make sure that you have not switched on the *[Automatic authentication provided by App Service](https://docs.microsoft.com/azure/app-service/scenario-secure-app-authentication-app-service)*. It interferes the authentication code used in this code example. - -#### Publish your files (ciam-msal-dotnet-api) - -##### Publish using Visual Studio - -Follow the link to [Publish with Visual Studio](https://docs.microsoft.com/visualstudio/deployment/quickstart-deploy-to-azure). - -##### Publish using Visual Studio Code - -1. Install the Visual Studio Code extension [Azure App Service](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azureappservice). -1. Follow the link to [Publish with Visual Studio Code](https://docs.microsoft.com/aspnet/core/tutorials/publish-to-azure-webapp-using-vscode) - -> :information_source: When calling the web API, your app may receive an error similar to the following: -> -> *Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://some-url-here. (Reason: additional information here).* -> -> If that's the case, you'll need enable [cross-origin resource sharing (CORS)](https://developer.mozilla.org/docs/Web/HTTP/CORS) for you web API. Follow the steps below to do this: -> -> * Go to [Microsoft Entra admin center](https://entra.microsoft.com/), and locate the web API project that you've deployed to App Service. -> * On the API blade, select **CORS**. Check the box **Enable Access-Control-Allow-Credentials**. -> * Under **Allowed origins**, add the URL of your published web app **that will call this web API**. - -
- -## Contributing - -If you'd like to contribute to this sample, see [CONTRIBUTING.MD](/CONTRIBUTING.md). - -This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. - -## Learn More - -* [Customize the default branding](https://github.com/microsoft/entra-previews/blob/PP2/docs/5-Customize-default-branding.md) -* [OAuth 2.0 device authorization grant flow](https://github.com/microsoft/entra-previews/blob/PP2/docs/9-OAuth2-device-code.md) -* [Customize sign-in strings](https://github.com/microsoft/entra-previews/blob/PP2/docs/8-Customize-sign-in-strings.md) -* [Building Zero Trust ready apps](https://aka.ms/ztdevsession) -* [Microsoft.Identity.Web](https://aka.ms/microsoft-identity-web) -* [Validating Access Tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens) -* [User and application tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#user-and-application-tokens) -* [Validation differences by supported account types](https://docs.microsoft.com/azure/active-directory/develop/supported-accounts-validation) -* [How to manually validate a JWT access token using the Microsoft identity platform](https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/master/README.md) +--- +page_type: sample +name: A Node.js daemon application secured by MSAL Node on Microsoft identity platform +description: This sample demonstrates how to use MSAL Node to acquire an access token for a protected resource in a daemon application using the application's own identity with the client credentials flow +languages: + - javascript + - csharp +products: + - entra-external-id + - aspnet-core + - msal-js + - microsoft-identity-web +urlFragment: ms-identity-ciam-javascript-tutorial-3-call-api-node-daemon +extensions: + services: + - ms-identity + platform: + - JavaScript + endpoint: + - AAD v2.0 + level: + - 200 + client: + - Node.js daemon app + service: + - ASP.NET Core web API +--- + +# A Node.js daemon application secured by MSAL Node on Microsoft identity platform + +* [Overview](#overview) +* [Scenario](#scenario) +* [Contents](#contents) +* [Prerequisites](#prerequisites) +* [Setup the sample](#setup-the-sample) +* [Explore the sample](#explore-the-sample) +* [Troubleshooting](#troubleshooting) +* [About the code](#about-the-code) +* [How to deploy this sample to Azure](#how-to-deploy-this-sample-to-azure) +* [Contributing](#contributing) +* [Learn More](#learn-more) + +## Overview + +This sample demonstrates how to use [MSAL Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node) to acquire an access token for a protected resource in a daemon application using the application's own identity with the ([client credentials flow](https://learn.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)). + +Here you'll learn about [access tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens), [token validation](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens), [application permissions](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), and more. + +## Scenario + +1. The Node.js daemon app obtains a JWT [Access Token](https://aka.ms/access-tokens) from **Microsoft Entra External ID**. +1. The **access token** is used as a *bearer* token to authorize the user to call the ASP.NET Core web API protected by **Microsoft Entra External ID**. +1. The service uses the [Microsoft.Identity.Web](https://aka.ms/microsoft-identity-web) to protect the Web api, check permissions and validate tokens. + +![Scenario Image](./ReadmeFiles/topology.png). + +## Contents + +| File/folder | Description | +|---------------------------------|-----------------------------------------------------------| +| `App/.env` | Authentication parameters for the daemon app reside here.| +| `App/auth.js` | MSAL Node is initialized here. | +| `App/fetch.js` | logic to call the API reside here. | +| `API/ToDoListAPI/appsettings.json` | Authentication parameters for the API reside here. | +| `API/ToDoListAPI/Startup.cs` | Microsoft.Identity.Web is initialized here. | + +## Prerequisites + +* Either [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [Visual Studio Code](https://code.visualstudio.com/download) and [.NET Core SDK](https://www.microsoft.com/net/learn/get-started) +* An **Microsoft Entra External ID** tenant. For more information, see: [How to get an external tenant](https://github.com/microsoft/entra-previews/blob/PP2/docs/1-Create-a-CIAM-tenant.md) +* A user account in your **Microsoft Entra External ID** tenant. + +## Setup the sample + +### Step 1: Clone or download this repository + +From your shell or command line: + +```console +git clone https://github.com/Azure-Samples/ms-identity-ciam-javascript-tutorial.git +``` + +or download and extract the repository *.zip* file. + +> :warning: To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive. + +### Step 2: Install project dependencies + +```console + cd 2-Authorization\3-call-api-node-daemon\App + npm install +``` + +### Step 3: Register the sample application(s) in your tenant + +There are two projects in this sample. Each needs to be separately registered in your external tenant. To register these projects, you can: + +- follow the steps below for manually register your apps +- or use PowerShell scripts that: + - **automatically** creates the Microsoft Entra applications and related objects (passwords, permissions, dependencies) for you. + - modify the projects' configuration files. + +
+ Expand this section if you want to use this automation: + +> :warning: If you have never used **Microsoft Graph PowerShell** before, we recommend you go through the [App Creation Scripts Guide](./AppCreationScripts/AppCreationScripts.md) once to ensure that your environment is prepared correctly for this step. + +1. Ensure that you have [PowerShell 7](https://learn.microsoft.com/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.3) or later. +1. Run the script to create your Azure AD application and configure the code of the sample application accordingly. +1. For interactive process -in PowerShell, run: + + ```PowerShell + cd .\AppCreationScripts\ + .\Configure.ps1 -TenantId "[Optional] - your tenant id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'" + ``` + +> Other ways of running the scripts are described in [App Creation Scripts guide](./AppCreationScripts/AppCreationScripts.md). The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios. + +> :information_source: This sample can make use of client certificates. You can use **AppCreationScripts** to register an Azure AD application with certificates. See: [How to use certificates instead of client secrets](./README-use-certificate.md) + +
+ +#### Choose the Microsoft Entra External ID tenant where you want to create your applications + +To manually register the apps, as a first step you'll need to: + +1. Sign in to the [Microsoft Entra admin center](https://entra.microsoft.com/). +1. If your account is present in more than one Microsoft Entra External ID tenant, select your profile at the top right corner in the menu on top of the page, and then **switch directory** to change your portal session to the desired Microsoft Entra External ID tenant. + +#### Register the service app (ciam-msal-dotnet-api) + +1. Navigate to the [Microsoft Entra admin center](https://entra.microsoft.com/) and select the **Microsoft Entra External ID** service. +1. Select the **App Registrations** blade on the left, then select **New registration**. +1. In the **Register an application page** that appears, enter your application's registration information: + 1. In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `ciam-msal-dotnet-api`. + 1. Under **Supported account types**, select **Accounts in this organizational directory only** + 1. Select **Register** to create the application. +1. In the **Overview** blade, find and note the **Application (client) ID** and **Directory (tenant) ID**. You use this value in your app's configuration file(s) later in your code. +1. In the app's registration screen, select the **Expose an API** blade to the left to open the page where you can publish the permission as an API for which client applications can obtain [access tokens](https://aka.ms/access-tokens) for. The first thing that we need to do is to declare the unique [resource](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow) URI that the clients will be using to obtain access tokens for this API. To declare an resource URI(Application ID URI), follow the following steps: + 1. Select **Set** next to the **Application ID URI** to generate a URI that is unique for this app. + 1. For this sample, accept the proposed Application ID URI (`api://{clientId}`) by selecting **Save**. + > :information_source: Read more about Application ID URI at [Validation differences by supported account types (signInAudience)](https://docs.microsoft.com/azure/active-directory/develop/supported-accounts-validation). + +##### Publish Delegated Permissions + +1. All APIs must publish a minimum of one [scope](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-authorization-code), also called [Delegated Permission](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), for the client apps to obtain an access token for a *user* successfully. To publish a scope, follow these steps: +1. Select **Add a scope** button open the **Add a scope** screen and Enter the values as indicated below: + 1. For **Scope name**, use `ToDoList.Read`. + 1. For **Admin consent display name** type in *ToDoList.Read*. + 1. For **Admin consent description** type in *e.g. Allows the app to read the signed-in user's files.*. + 1. Keep **State** as **Enabled**. + 1. Select the **Add scope** button on the bottom to save this scope. + 1. Repeat the steps above for another scope named **ToDoList.ReadWrite** +1. Select the **Manifest** blade on the left. + 1. Set `accessTokenAcceptedVersion` property to **2**. + 1. Select on **Save**. + +> :information_source: Follow [the principle of least privilege when publishing permissions](https://learn.microsoft.com/security/zero-trust/develop/protected-api-example) for a web API. + +##### Publish Application Permissions + +1. All APIs should publish a minimum of one [App role for applications](https://docs.microsoft.com/azure/active-directory/develop/howto-add-app-roles-in-azure-ad-apps#assign-app-roles-to-applications), also called [Application Permission](https://docs.microsoft.com/azure/active-directory/develop/v2-permissions-and-consent#permission-types), for the client apps to obtain an access token as *themselves*, i.e. when they are not signing-in a user. **Application permissions** are the type of permissions that APIs should publish when they want to enable client applications to successfully authenticate as themselves and not need to sign-in users. To publish an application permission, follow these steps: +1. Still on the same app registration, select the **App roles** blade to the left. +1. Select **Create app role**: + 1. For **Display name**, enter a suitable name for your application permission, for instance **ToDoList.Read.All**. + 1. For **Allowed member types**, choose **Application** to ensure other applications can be granted this permission. + 1. For **Value**, enter **ToDoList.Read.All**. + 1. For **Description**, enter *e.g. Allows the app to read the signed-in user's files.*. + 1. Select **Apply** to save your changes. + 1. Repeat the steps above for another app permission named **ToDoList.ReadWrite.All** + +##### Configure Optional Claims + +1. Still on the same app registration, select the **Token configuration** blade to the left. +1. Select **Add optional claim**: + 1. Select **optional claim type**, then choose **Access**. + 1. Select the optional claim **idtyp**. + > Indicates token type. This claim is the most accurate way for an API to determine if a token is an app token or an app+user token. This is not issued in tokens issued to users. + 1. Select **Add** to save your changes. + +##### Configure the service app (ciam-msal-dotnet-api) to use your app registration + +Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code. + +> In the steps below, "ClientID" is the same as "Application ID" or "AppId". + +1. Open the `API\ToDoListAPI\appsettings.json` file. +1. Find the key `Enter_the_Application_Id_Here` and replace the existing value with the application ID (clientId) of `ciam-msal-dotnet-api` app copied from the Microsoft Entra admin center. +1. Find the key `Enter_the_Tenant_Id_Here` and replace the existing value with your external tenant/directory ID. +1. Find the placeholder `Enter_the_Tenant_Subdomain_Here` and replace it with the Directory (tenant) subdomain. For instance, if your tenant primary domain is `contoso.onmicrosoft.com`, use `contoso`. If you don't have your tenant domain name, learn how to [read your tenant details](https://review.learn.microsoft.com/azure/active-directory/external-identities/customers/how-to-create-customer-tenant-portal#get-the-customer-tenant-details). + +#### Register the client app (ciam-msal-node-daemon) + +1. Navigate to the [Microsoft Entra admin center](https://entra.microsoft.com/) and select the **Microsoft Entra External ID** service. +1. Select the **App Registrations** blade on the left, then select **New registration**. +1. In the **Register an application page** that appears, enter your application's registration information: + 1. In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `ciam-msal-node-daemon`. + 1. Under **Supported account types**, select **Accounts in this organizational directory only** + 1. Select **Register** to create the application. +1. In the **Overview** blade, find and note the **Application (client) ID**. You use this value in your app's configuration file(s) later in your code. +1. In the app's registration screen, select the **Certificates & secrets** blade in the left to open the page where you can generate secrets and upload certificates. +1. In the **Client secrets** section, select **New client secret**: + 1. Type a key description (for instance `app secret`). + 1. Select one of the available key durations (**6 months**, **12 months** or **Custom**) as per your security policy. + 1. The generated key value will be displayed when you select the **Add** button. Copy and save the generated value (not the key) for use in later steps. + 1. You'll need this key later in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Microsoft Entra admin center before navigating to any other screen or blade. + > :warning: For enhanced security, consider using **certificates** instead of client secrets. See: [How to use certificates instead of secrets](./README-use-certificate.md). +1. Since this app signs-in as itself using the [OAuth 2\.0 client credentials flow](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow), we will now proceed to select **application permissions**, which is required by apps authenticating as themselves. + 1. In the app's registration screen, select the **API permissions** blade in the left to open the page where we add access to the APIs that your application needs: + 1. Select the **Add a permission** button and then: + 1. Ensure that the **APIs my organization uses** tab is selected. + 1. In the list of APIs, select the API `ciam-msal-dotnet-api`. + 1. We will select “Application permissions”, which should be the type of permissions that apps should use when they are authenticating just as themselves and not signing-in users. + 1. In the **Application permissions** section, select the **ToDoList.Read.All**, **ToDoList.ReadWrite.All** in the list. Use the search box if necessary. + 1. Select the **Add permissions** button at the bottom. +1. At this stage, the permissions are assigned correctly but since the client app does not allow users to interact, the users' themselves cannot consent to these permissions. To get around this problem, we'd let the [tenant administrator consent on behalf of all users in the tenant](https://docs.microsoft.com/azure/active-directory/develop/v2-admin-consent). Select the **Grant admin consent for {tenant}** button, and then select **Yes** when you are asked if you want to grant consent for the requested permissions for all accounts in the tenant. You need to be a tenant admin to be able to carry out this operation. + +##### Configure the client app (ciam-msal-node-daemon) to use your app registration + +Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code. + +> In the steps below, "ClientID" is the same as "Application ID" or "AppId". + +1. Open the `APP\.env` file. +1. Find the key `Enter_the_Application_Id_Here` and replace the existing value with the application ID (clientId) of `ciam-msal-node-daemon` app copied from the Microsoft Entra admin center. +1. Find the placeholder `Enter_the_Tenant_Subdomain_Here` and replace it with the Directory (tenant) subdomain. For instance, if your tenant primary domain is `contoso.onmicrosoft.com`, use `contoso`. If you don't have your tenant domain name, learn how to [read your tenant details](https://review.learn.microsoft.com/azure/active-directory/external-identities/customers/how-to-create-customer-tenant-portal#get-the-customer-tenant-details). +1. Find the key `Enter_the_Client_Secret_Here` and replace the existing value with the generated secret that you saved during the creation of `ciam-msal-node-daemon` copied from the Microsoft Entra admin center. +1. Find the key `Enter_the_Web_Api_Application_Id_Here` and replace the existing value with the application ID (clientId) of `ciam-msal-dotnet-api` app copied from the Microsoft Entra admin center. + +### Step 4: Running the sample + +From your shell or command line, execute the following commands: + +```console + cd 2-Authorization\3-call-api-node-daemon\API\ToDoListAPI + dotnet run +``` + +Then, open a separate command terminal and run: + +```console + 2-Authorization\3-call-api-node-daemon\App + node . +``` + +## Explore the sample + +1. Navigate to the [app](./App/) root directory in the terminal. +1. Enter the following command `node .`. + +![Screenshot](./ReadmeFiles/Screenshot.png) + +> :information_source: Did the sample not work for you as expected? Then please reach out to us using the [GitHub Issues](../../../../issues) page. + +## We'd love your feedback! + +Were we successful in addressing your learning objective? Consider taking a moment to [share your experience with us](https://forms.office.com/Pages/ResponsePage.aspx?id=v4j5cvGGr0GRqy180BHbR_ivMYEeUKlEq8CxnMPgdNZUNDlUTTk2NVNYQkZSSjdaTk5KT1o4V1VVNS4u). + +## Troubleshooting + +Use [Stack Overflow](http://stackoverflow.com/questions/tagged/msal) to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [`azure-active-directory` `node` `ms-identity` `adal` `msal`]. + +If you find a bug in the sample, raise the issue on [GitHub Issues](../../../../issues). + +## About the code + +You need to instantiate **MSAL Node** as a [ConfidentialClientApplication](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/initialize-confidential-client-application.md) to support the ([client credentials flow](https://learn.microsoft.com/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)). + +```javascript +const msal = require('@azure/msal-node'); +const cca = new msal.ConfidentialClientApplication(msalConfig); +``` + +### Acquire a token + +**Access Token** requests in **MSAL.js** are meant to be *per-resource-per-scope(s)*. This means that an **Access Token** requested for resource **A** with scope `scp1`: + +* cannot be used for accessing resource **A** with scope `scp2`, and, +* cannot be used for accessing resource **B** of any scope. + +The intended recipient of an **Access Token** is represented by the `aud` claim; in case the value for the `aud` claim does not mach the resource APP ID URI, the token should be considered invalid. Likewise, the permissions that an Access Token grants is represented by the `roles` claim. See [Access Token claims](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#payload-claims) for more information. + +To get the **access token** for a protected resource using the client credentials grant flow, **MSAL Node** exposes `acquireTokenByClientCredential` API to grant permission to the application to get an **access token**. + +```javascript +const cca = new msal.ConfidentialClientApplication(msalConfig); + +async function getToken(tokenRequest) { + return await cca.acquireTokenByClientCredential(tokenRequest); +} +``` + +### Access token validation + +On the web API side, the `AddMicrosoftIdentityWebApi` method in [Program.cs](./API/ToDoListAPI/Program.cs) protects the web API by [validating access tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens) sent tho this API. Check out [Protected web API: Code configuration](https://docs.microsoft.com/azure/active-directory/develop/scenario-protected-web-api-app-configuration) which explains the inner workings of this method in more detail. + +```csharp +builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) + .AddMicrosoftIdentityWebApi(options => + { + builder.Configuration.Bind("AzureAd", options); + options.Events = new JwtBearerEvents(); + }, options => { builder.Configuration.Bind("AzureAd", options); }); +``` + +For validation and debugging purposes, developers can decode **JWT**s (*JSON Web Tokens*) using [jwt.ms](https://jwt.ms). + +### Verifying permissions + +Access tokens that have neither the **scp** (for delegated permissions) nor **roles** (for application permissions) claim with the required scopes/permissions should not be accepted. In the sample, this is illustrated via the `RequiredScopeOrAppPermission` attribute in [ToDoListController.cs](./API/ToDoListAPI/Controllers/ToDoListController.cs): + +```csharp +[HttpGet] + [RequiredScopeOrAppPermission( + RequiredScopesConfigurationKey = "AzureAD:Scopes:Read", + RequiredAppPermissionsConfigurationKey = "AzureAD:AppPermissions:Read" + )] + public async Task GetAsync() + { + var toDos = await _toDoContext.ToDos! + .Where(td => RequestCanAccessToDo(td.Owner)) + .ToListAsync(); + return Ok(toDos); + } +``` + +### Access to data + +Web API endpoints should be prepared to accept calls from both users and applications, and should have control structures in place to respond to each accordingly. For instance, a call from a user via delegated permissions should be responded with user's data, while a call from an application via application permissions might be responded with the entire todolist. This is illustrated in the [ToDoListController](./API/ToDoListAPI/Controllers/ToDoListController.cs) controller: + +```csharp +private bool RequestCanAccessToDo(Guid userId) + { + return IsAppMakingRequest() || (userId == GetUserId()); + } +[HttpGet] + [RequiredScopeOrAppPermission( + RequiredScopesConfigurationKey = "AzureAD:Scopes:Read", + RequiredAppPermissionsConfigurationKey = "AzureAD:AppPermissions:Read" + )] + public async Task GetAsync() + { + var toDos = await _toDoContext.ToDos! + .Where(td => RequestCanAccessToDo(td.Owner)) + .ToListAsync(); + return Ok(toDos); + } +``` + +When granting access to data based on scopes, be sure to follow [the principle of least privilege](https://docs.microsoft.com/azure/active-directory/develop/secure-least-privileged-access). + +### Debugging the sample + +To debug the .NET Core web API that comes with this sample, install the [C# extension](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) for Visual Studio Code. + +Learn more about using [.NET Core with Visual Studio Code](https://docs.microsoft.com/dotnet/core/tutorials/with-visual-studio-code). + +## How to deploy this sample to Azure + +
+Expand the section + +### Deploying web API to Azure App Services + +There is one web API in this sample. To deploy it to **Azure App Services**, you'll need to: + +* create an **Azure App Service** +* publish the projects to the **App Services** + +> :warning: Please make sure that you have not switched on the *[Automatic authentication provided by App Service](https://docs.microsoft.com/azure/app-service/scenario-secure-app-authentication-app-service)*. It interferes the authentication code used in this code example. + +#### Publish your files (ciam-msal-dotnet-api) + +##### Publish using Visual Studio + +Follow the link to [Publish with Visual Studio](https://docs.microsoft.com/visualstudio/deployment/quickstart-deploy-to-azure). + +##### Publish using Visual Studio Code + +1. Install the Visual Studio Code extension [Azure App Service](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azureappservice). +1. Follow the link to [Publish with Visual Studio Code](https://docs.microsoft.com/aspnet/core/tutorials/publish-to-azure-webapp-using-vscode) + +> :information_source: When calling the web API, your app may receive an error similar to the following: +> +> *Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://some-url-here. (Reason: additional information here).* +> +> If that's the case, you'll need enable [cross-origin resource sharing (CORS)](https://developer.mozilla.org/docs/Web/HTTP/CORS) for you web API. Follow the steps below to do this: +> +> * Go to [Microsoft Entra admin center](https://entra.microsoft.com/), and locate the web API project that you've deployed to App Service. +> * On the API blade, select **CORS**. Check the box **Enable Access-Control-Allow-Credentials**. +> * Under **Allowed origins**, add the URL of your published web app **that will call this web API**. + +
+ +## Contributing + +If you'd like to contribute to this sample, see [CONTRIBUTING.MD](/CONTRIBUTING.md). + +This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. + +## Learn More + +* [Customize the default branding](https://github.com/microsoft/entra-previews/blob/PP2/docs/5-Customize-default-branding.md) +* [OAuth 2.0 device authorization grant flow](https://github.com/microsoft/entra-previews/blob/PP2/docs/9-OAuth2-device-code.md) +* [Customize sign-in strings](https://github.com/microsoft/entra-previews/blob/PP2/docs/8-Customize-sign-in-strings.md) +* [Building Zero Trust ready apps](https://aka.ms/ztdevsession) +* [Microsoft.Identity.Web](https://aka.ms/microsoft-identity-web) +* [Validating Access Tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validating-tokens) +* [User and application tokens](https://docs.microsoft.com/azure/active-directory/develop/access-tokens#user-and-application-tokens) +* [Validation differences by supported account types](https://docs.microsoft.com/azure/active-directory/develop/supported-accounts-validation) +* [How to manually validate a JWT access token using the Microsoft identity platform](https://github.com/Azure-Samples/active-directory-dotnet-webapi-manual-jwt-validation/blob/master/README.md)