Using AppAuth in Node.js
AppAuth-JS provides specific support for Node.js CLI applications where a standard browser redirect isn't possible.
Key Components
- NodeRequestor: A Node.js HTTP client that follows redirects.
- NodeCrypto: Uses the Node.js
cryptomodule for secure random string and challenge generation. - NodeBasedHandler: Automatically starts a temporary local HTTP server to capture the redirect from the system browser.
Implementation Example
import {
NodeRequestor,
NodeCrypto,
NodeBasedHandler,
BaseTokenRequestHandler
} from '@openid/appauth/built/node_support';
const PORT = 3000;
const requestor = new NodeRequestor();
const crypto = new NodeCrypto();
const authHandler = new NodeBasedHandler(PORT, undefined, crypto);
const tokenHandler = new BaseTokenRequestHandler(requestor);
// When performAuthorizationRequest is called, it will:
// 1. Setup a local server at http://127.0.0.1:3000
// 2. Open the system browser to the provider's auth page
// 3. Wait for the redirect, capture the code, and close the server.
Redirect URI in Node
When using NodeBasedHandler, your redirect_uri should typically be http://127.0.0.1:PORT or http://localhost:PORT.