Giter VIP home page Giter VIP logo

azure-node-api-to-api-msal-auth's Introduction

Introduction

Example of creating a micro service application with a frontend calling an API which in its turn authenticate using the with Azure AD and then uses the token to call another protected API.

This solution uses the supported case "Web APIs that call web APIs" from the msal-node library and uses the "Client Credentials" flow to get a token with the appropriate access rights.

Prerequisites

Creating the two Azure Applications used by the API applications

  1. Create API1 from Azure AD → App registrations → New registration
  2. Give it a name, like for example "msapi1"
  3. Select the version for "Accounts in this organizational directory only (YourTenantName - Single tenant)"
  4. Do the same procedure for API2 and call it "msapi2"

Creating a secret for API1

When an application need to ask for tokens without any human intervention it needs a secret that can be used in the "Client Credential"-flow. API1 will be asking for tokens to access API2, so we need to create a secret for API1.

  1. Azure AD → Find application for API1 → "Certificates and secrets" → "New client secret"

Setting up roles and permissions

API1 must be authorized to request a token that can call the protected route on API2.

  1. Make sure that API2 has an established route, otherwise create one

  2. Create a new app role on API2

  3. Give API1 access to request tokens containing the new role

  4. Fulfill the admin consent for the API permission. You need to do this as the access lies under "Application permissions" which means that the application can request tokens and refresh tokens by itself and therefore without any user input

  5. Remove the excessive access given by the command

Update the config for the services

Update the config which is present in all micro services

Deploy solution to Azure

  1. Deploy every service as an App Service for Linux to Azure using the extension to Visual Studio Code

Overview of the micro service code

Frontend

Contains two routes "/" and "/get" where the "/get" route sends a get request to API1 using axios package and then waits for API1 to return data.

API1

API1 receives a request from the frontend axios get call and then uses the "@azure/msal-node" package to get a token for the "api://msapi2/.default" scope. When it has a token it in its turn does a get request using axios to API2 when the access token in its "authorization" header.

API2

API2 gets the request from API1 and immediately decode the token looking at the "roles" in the claims. Is the request contains the "can_call_api"-role it will accept the request and return the data to API1 which in its turn return it to the frontend that presents it on the screen.

azure-node-api-to-api-msal-auth's People

Contributors

githubjens avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.