A Node.js/Express demonstration of the OAuth 2.0 Device Authorization Grant (Device Code Flow) using Microsoft Azure Active Directory. This project implements the complete flow for authenticating users on input-constrained devices (smart TVs, IoT devices, CLI tools) and calling the Microsoft Graph API.
The Device Code Flow is an OAuth 2.0 extension that enables devices with limited input capabilities or without a web browser to obtain user authorization. Instead of entering credentials on the device itself, users authenticate on a separate device (like a smartphone or computer) by visiting a URL and entering a code.
This demo walks through the complete 4-step flow:
- Get Device Code — Request a device code and user code from Azure AD
- User Authentication — User visits the verification URL and enters the code
- Retrieve Access Token — Poll for the access token once the user has authenticated
- Call Microsoft Graph API — Use the access token to call the beta
/meendpoint
Before running this project, ensure you have:
- Node.js (v14 or higher)
- An Azure AD tenant with administrative access
- A registered Azure AD application with:
- Application (client) ID
- Directory (tenant) ID
- API Permissions:
User.Read(Microsoft Graph) - Public client flows enabled (Authentication → Advanced settings → Allow public client flows: Yes)
- Go to Azure Portal → Azure Active Directory → App registrations
- Click New registration
- Enter a name for your application
- Under Supported account types, choose the appropriate option (single or multi-tenant)
- Click Register
- Note down the Application (client) ID and Directory (tenant) ID
- Navigate to API permissions → Add Microsoft Graph → Delegated permissions → Add
User.Read - Navigate to Authentication → Advanced settings → Enable Allow public client flows
- Clone the repository:
git clone https://github.com/0GiS0/oauth2-device-code-flow.git
cd oauth2-device-code-flow- Install dependencies:
npm install- Configure environment variables:
Copy the .env.sample file to .env:
cp .env.sample .envEdit the .env file and add your Azure AD credentials:
TENANT_ID="your-tenant-id"
CLIENT_ID="your-client-id"
Start the server:
npm startThe application will start on http://localhost:8000.
-
Navigate to
http://localhost:8000 -
Step 1: Get the Device Code
- Click "Get the device code" button
- The app requests a device code from Azure AD
- You'll see a user code and verification URL
-
Step 2: User Authentication
- Open the verification URL (e.g.,
https://microsoft.com/devicelogin) in a browser - Enter the user code displayed in the app
- Sign in with your Microsoft account
- Grant consent to the application
- Open the verification URL (e.g.,
-
Step 3: Retrieve Access Token
- The app polls Azure AD for the access token
- Once authentication is complete, the access token is retrieved
-
Step 4: Call Microsoft Graph API
- The app uses the access token to call the Microsoft Graph beta
/meendpoint - Your user profile information is displayed
- The app uses the access token to call the Microsoft Graph beta
oauth2-device-code-flow/
├── server.js # Main Express server and OAuth 2.0 flow implementation
├── package.json # Project dependencies and scripts
├── .env.sample # Template for environment variables
├── .gitignore # Git ignore rules
├── views/ # EJS templates
│ ├── index.ejs # Landing page with 4-step stepper
│ ├── device-code.ejs # Device code display and polling logic
│ ├── access-token.ejs # Access token display
│ ├── calling-ms-graph.ejs # Microsoft Graph API response
│ └── partials/ # Reusable EJS components
└── public/ # Static assets (CSS, JS)
-
server.js— Contains all Express routes and OAuth 2.0 Device Code Flow logic:GET /get/the/code— Initiates the device code requestPOST /checking— Polls Azure AD for tokenGET /access/token— Displays the retrieved access tokenPOST /call/ms/graph— Calls Microsoft Graph API
-
.env.sample— Template for required environment variables -
views/index.ejs— Landing page with visual stepper showing the 4-step flow
- Express — Web framework for Node.js
- EJS — Embedded JavaScript templating
- node-fetch — HTTP client for making API requests
- bunyan — JSON logging library
- dotenv — Environment variable management
- body-parser — Request body parsing middleware
- nodemon — Development tool for auto-restarting the server
- OAuth 2.0 Device Authorization Grant — RFC 8628
- Microsoft Identity Platform — Device Code Flow
- Microsoft Graph API — Get User
- Azure AD App Registration Guide
- Never commit your
.envfile — It contains sensitive credentials - Access tokens are displayed for demo purposes — In production, never expose tokens to the client-side
- Use HTTPS in production — Always serve your application over HTTPS
- Implement token refresh — Access tokens expire; implement refresh token logic for production use
- Limit token scope — Only request the minimum necessary permissions
This project is licensed under the ISC License.
Contributions are welcome! Please feel free to submit a Pull Request.
@0GiS0
⭐ If you found this demo helpful, please consider giving it a star!