1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00
Files
Bobby I. 6d21de3f31 feat(auth): add Entra ID identity provider integration for Redis client authentication (#2877)
* feat(auth): refactor authentication mechanism to use CredentialsProvider

- Introduce new credential providers: AsyncCredentialsProvider, StreamingCredentialsProvider
- Update client handshake process to use the new CredentialsProviders and to support async credentials fetch / credentials refresh
- Internal conversion of username/password to a CredentialsProvider
- Modify URL parsing to accommodate the new authentication structure
- Tests

* feat(auth): auth extensions

Introduces TokenManager and supporting classes to handle token acquisition, automatic
refresh, and updates via identity providers. This foundation enables consistent
authentication token management across different identity provider implementations.

Key additions:
- Add TokenManager to obtain and maintain auth tokens from identity providers
  with automated refresh scheduling based on TTL and configurable thresholds
- Add IdentityProvider interface for token acquisition from auth providers
- Implement Token class for managing token state and TTL tracking
- Include configurable retry mechanism with exponential backoff and jitter
- Add comprehensive test suite covering refresh cycles and error handling

This change establishes the core infrastructure needed for reliable token
lifecycle management across different authentication providers.

* feat(auth): add Entra ID identity provider integration

Introduces Entra ID (former Azure AD) authentication support with multiple authentication flows
and automated token lifecycle management.

Key additions:
- Add EntraIdCredentialsProvider for handling Entra ID authentication flows
- Implement MSALIdentityProvider to integrate with MSAL/EntraID authentication library
- Add support for multiple authentication methods:
  - Managed identities (system and user-assigned)
  - Client credentials with certificate
  - Client credentials with secret
  - Authorization Code flow with PKCE
- Add factory class with builder methods for each authentication flow
- Include sample Express server implementation for Authorization Code flow
- Add comprehensive configuration options for authority and token management

* feat(test-utils): improve cluster testing

- Add support for configuring replica authentication with 'masterauth'
- Allow default client configuration during test cluster creation

This improves the testing framework's flexibility by automatically
configuring replica authentication when '--requirepass' is used and
enabling custom client configurations across cluster nodes.

* feat(auth): add EntraId integration tests

- Add integration tests for token renewal and re-authentication flows
- Update credentials provider to use uniqueId as username instead of account username
- Add test utilities for loading Redis endpoint configurations
- Split TypeScript configs into separate files for samples and integration tests
- Remove `@redis/authx` package and nest it under `@`
2025-01-30 10:29:19 +02:00

153 lines
4.3 KiB
TypeScript

import express, { Request, Response } from 'express';
import session from 'express-session';
import dotenv from 'dotenv';
import { DEFAULT_TOKEN_MANAGER_CONFIG, EntraIdCredentialsProviderFactory } from '../../lib/entra-id-credentials-provider-factory';
dotenv.config();
if (!process.env.SESSION_SECRET) {
throw new Error('SESSION_SECRET environment variable must be set');
}
interface PKCESession extends session.Session {
pkceCodes?: {
verifier: string;
challenge: string;
challengeMethod: string;
};
}
interface AuthRequest extends Request {
session: PKCESession;
}
const app = express();
const sessionConfig = {
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
secure: process.env.NODE_ENV === 'production', // Only use secure in production
httpOnly: true,
sameSite: 'lax',
maxAge: 3600000 // 1 hour
}
} as const;
app.use(session(sessionConfig));
if (!process.env.MSAL_CLIENT_ID || !process.env.MSAL_TENANT_ID) {
throw new Error('MSAL_CLIENT_ID and MSAL_TENANT_ID environment variables must be set');
}
// Initialize MSAL provider with authorization code PKCE flow
const {
getPKCECodes,
createCredentialsProvider,
getAuthCodeUrl
} = EntraIdCredentialsProviderFactory.createForAuthorizationCodeWithPKCE({
clientId: process.env.MSAL_CLIENT_ID,
redirectUri: process.env.REDIRECT_URI || 'http://localhost:3000/redirect',
authorityConfig: { type: 'multi-tenant', tenantId: process.env.MSAL_TENANT_ID },
tokenManagerConfig: DEFAULT_TOKEN_MANAGER_CONFIG
});
app.get('/login', async (req: AuthRequest, res: Response) => {
try {
// Generate PKCE Codes before starting the authorization flow
const pkceCodes = await getPKCECodes();
// Store PKCE codes in session
req.session.pkceCodes = pkceCodes
await new Promise<void>((resolve, reject) => {
req.session.save((err) => {
if (err) reject(err);
else resolve();
});
});
const authUrl = await getAuthCodeUrl({
challenge: pkceCodes.challenge,
challengeMethod: pkceCodes.challengeMethod
});
res.redirect(authUrl);
} catch (error) {
console.error('Login flow failed:', error);
res.status(500).send('Authentication failed');
}
});
app.get('/redirect', async (req: AuthRequest, res: Response) => {
try {
// The authorization code is in req.query.code
const { code, client_info } = req.query;
const { pkceCodes } = req.session;
if (!pkceCodes) {
console.error('Session state:', {
hasSession: !!req.session,
sessionID: req.sessionID,
pkceCodes: req.session.pkceCodes
});
return res.status(400).send('PKCE codes not found in session');
}
// Check both possible error scenarios
if (req.query.error) {
console.error('OAuth error:', req.query.error, req.query.error_description);
return res.status(400).send(`OAuth error: ${req.query.error_description || req.query.error}`);
}
if (!code) {
console.error('Missing authorization code. Query parameters received:', req.query);
return res.status(400).send('Authorization code not found in request. Query params: ' + JSON.stringify(req.query));
}
// Configure with the received code
const entraidCredentialsProvider = createCredentialsProvider(
{
code: code as string,
verifier: pkceCodes.verifier,
clientInfo: client_info as string | undefined
},
);
const initialCredentials = entraidCredentialsProvider.subscribe({
onNext: (token) => {
console.log('Token acquired:', token);
},
onError: (error) => {
console.error('Token acquisition failed:', error);
}
});
const [credentials] = await initialCredentials;
console.log('Credentials acquired:', credentials)
// Clear sensitive data
delete req.session.pkceCodes;
await new Promise<void>((resolve, reject) => {
req.session.save((err) => {
if (err) reject(err);
else resolve();
});
});
res.json({ message: 'Authentication successful' });
} catch (error) {
console.error('Token acquisition failed:', error);
res.status(500).send('Failed to acquire token');
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`Login URL: http://localhost:${PORT}/login`);
});