Nodejs - Azure AD application access token
Microsoft Identity is a cloud-based identity and access management solution that provides secure sign-in and authorization to applications and services. Azure AD is a cloud-based directory and identity management service that enables users to sign in and access applications and services.
Authenticate against the SharePoint Online APIs using Nodejs, Azure AD app and a certificate.
Here is how to do it. Note: I use Node v19, where fetch is an available API.
Create a self-signed certificate using OpenSSL
- Install OpenSSL on your system if it's not already installed.
- Open a command prompt or terminal window and navigate to the directory where you want to store the certificate.
- Generate a private key using the following command:
or with a private key with a password
- Generate a self-signed certificate using the private key:
- During the certificate generation process, OpenSSL will ask for some information, such as the country name, state or province, city, and so on. Fill in the required information. The common name field should be the fully qualified domain name (FQDN) of the server where the certificate will be used.
- The
certificate.pem
file will now contain your self-signed certificate. You can use this certificate to secure your website or other applications. - If you need to convert the certificate to other formats, such as PFX or DER, you can use the following commands:
Note: Self-signed certificates are not trusted by default and can cause security warnings in web browsers. In production environments, it's recommended to use a trusted certificate authority (CA) to issue certificates.
Upload the self-signed certificate to the Azure AD Application.
To upload a PEM certificate to an Azure Active Directory (AD) application, you can use the Azure portal, Azure CLI, or Azure PowerShell. Here's a brief overview of the steps for each method.
Using the Azure portal:
- Log in to the Azure portal and navigate to the Azure AD application for which you want to upload the certificate.
- Select the "Certificates and secrets" option from the navigation menu.
- Click the "Upload certificate" button to select the PEM certificate file.
- Enter a description for the certificate, and then click the "Add" button to upload the certificate.
Registering an Application in Azure AD
To authenticate an application with Microsoft Identity and Azure AD, you need to register the application in the Azure AD portal. This process creates a unique identifier for the application and generates the necessary credentials that the application will use to authenticate with Microsoft Identity.
To register an application in Azure AD:
- Log in to the Azure portal and select Azure Active Directory.
- Select App registrations and then New registration.
- Provide a name for the application and select the appropriate, supported account types.
- Provide a redirect URI for the application, which is the URL that the user will be redirected to after they sign in.
- Click Register to create the application.
Granting Delegated Permissions
In order to access resources on behalf of the signed-in user, the application needs to be granted delegated permissions. Delegated permissions allow an application to access resources as the signed-in user, with the user's consent.
To grant delegated permissions to an application:
- Select the application you just registered from the list of app registrations.
- Select API permissions and then Add permission.
- Select the API that you want to grant permissions to, such as Microsoft Graph.
- Select the permissions that you want to grant the application.
- Click Add permissions to grant the selected permissions to the application.
Conclusion
Authentication in Microsoft Identity with Azure AD and application delegated permissions enables you to secure access to resources for your applications and services. By registering the application in Azure AD, granting delegated permissions, and authenticating the application, you can ensure that your applications are secure and your data is protected.