@oa2/core
Version:
A comprehensive, RFC-compliant OAuth 2.0 authorization server implementation in TypeScript
212 lines (206 loc) • 6.61 kB
TypeScript
import { Request, Response, NextFunction } from 'express';
import { OAuth2Server, OAuth2Request, OAuth2Response } from '../types.js';
import { APIGatewayEvent, APIGatewayProxyResult, APIGatewayProxyHandler } from 'aws-lambda';
import { OAuth2Error } from '../errors.js';
/**
* Express.js Adapter for OAuth 2.0 Server
*
* This module provides clean, functional middleware for integrating the OAuth 2.0 server
* with Express.js applications. All functions are focused on Express-specific concerns.
*/
/**
* Express OAuth 2.0 Configuration
* ===============================
*/
interface ExpressOAuth2Options {
/** The OAuth2 server instance */
server: OAuth2Server;
/** Whether to handle CORS automatically */
cors?: boolean;
/** Custom CORS origins (if cors is true) */
corsOrigins?: string | string[];
/** Whether to handle preflight OPTIONS requests */
handlePreflight?: boolean;
}
/**
* OAuth 2.0 Endpoint Handlers
* ===========================
*/
/**
* Express middleware for OAuth2 authorization endpoint.
*/
declare function expressAuthorizeHandler(options: ExpressOAuth2Options): (req: Request, res: Response, next: NextFunction) => Promise<void>;
/**
* Express middleware for OAuth2 token endpoint.
*/
declare function expressTokenHandler(options: ExpressOAuth2Options): (req: Request, res: Response, next: NextFunction) => Promise<void>;
/**
* Express middleware for OAuth2 token revocation endpoint.
*/
declare function expressRevokeHandler(options: ExpressOAuth2Options): (req: Request, res: Response, next: NextFunction) => Promise<void>;
/**
* Express middleware for OAuth2 token introspection endpoint.
*/
declare function expressIntrospectHandler(options: ExpressOAuth2Options): (req: Request, res: Response, next: NextFunction) => Promise<void>;
/**
* Express Router Factory
* =====================
*/
/**
* Express router factory that sets up all OAuth2 endpoints.
* Provides a complete OAuth 2.0 server in a single router.
*
* @example
* ```typescript
* const oauth2Router = createOAuth2Router({
* server: myOAuth2Server,
* cors: true,
* corsOrigins: ['https://myapp.com']
* });
*
* app.use('/oauth', oauth2Router);
* ```
*/
declare function createOAuth2Router(options: ExpressOAuth2Options): any;
/**
* Token Validation Middleware
* ===========================
*/
/**
* Express middleware to validate OAuth2 access tokens.
* Protects routes by requiring valid OAuth 2.0 access tokens.
*
* @example
* ```typescript
* // Protect a route with required scopes
* app.get('/api/protected',
* validateOAuth2Token({
* server: myServer,
* scopes: ['read', 'write']
* }),
* (req, res) => {
* // Access req.oauth2Token for token info
* res.json({ message: 'Protected resource', user: req.oauth2Token.username });
* }
* );
* ```
*/
declare function validateOAuth2Token(options: {
server: OAuth2Server;
scopes?: string[];
optional?: boolean;
}): (req: Request, res: Response, next: NextFunction) => Promise<void>;
/**
* Type Augmentation
* =================
*/
/**
* Type augmentation for Express Request to include OAuth2 token information.
*/
declare global {
namespace Express {
interface Request {
oauth2Token?: {
active: boolean;
scope?: string;
client_id?: string;
username?: string;
exp?: number;
};
}
}
}
/**
* AWS Lambda Adapter for OAuth 2.0 Server
*
* This module provides clean, functional handlers for integrating the OAuth 2.0 server
* with AWS Lambda and API Gateway. All functions are focused on AWS-specific concerns.
*/
/**
* Extracts OAuth2Request from API Gateway event.
* Handles both JSON and form-urlencoded request bodies.
*
* @example
* ```typescript
* export const handler: APIGatewayProxyHandler = async (event) => {
* const oauth2Request = extractOAuth2Request(event);
* const response = await server.token(oauth2Request);
* return transformOAuth2Response(response);
* };
* ```
*/
declare function extractOAuth2Request(event: APIGatewayEvent): OAuth2Request;
/**
* Response Processing
* ===================
*/
/**
* Transforms OAuth2Response into API Gateway result.
* Handles redirects, headers, cookies, and body formatting.
*
* @example
* ```typescript
* const oauth2Response = await server.authorize(request);
* return transformOAuth2Response(oauth2Response);
* ```
*/
declare function transformOAuth2Response(response: OAuth2Response): APIGatewayProxyResult;
/**
* Error Handling
* ==============
*/
/**
* Transforms OAuth2Error into API Gateway result.
* Formats errors according to OAuth 2.0 specifications.
*/
declare function transformOAuth2Error(error: OAuth2Error): APIGatewayProxyResult;
/**
* OAuth 2.0 Endpoint Handlers
* ===========================
*/
/**
* AWS Lambda handler for OAuth2 authorization requests.
*
* @example
* ```typescript
* import { createOAuth2Server } from 'oauth';
* import { apiGatewayAuthorizeHandler } from 'oauth/adapters/aws';
*
* const server = createOAuth2Server({ ... });
* export const authorize = apiGatewayAuthorizeHandler(server);
* ```
*/
declare function apiGatewayAuthorizeHandler(server: OAuth2Server): APIGatewayProxyHandler;
/**
* AWS Lambda handler for OAuth2 token requests.
*
* @example
* ```typescript
* const server = createOAuth2Server({ ... });
* export const token = apiGatewayTokenHandler(server);
* ```
*/
declare function apiGatewayTokenHandler(server: OAuth2Server): APIGatewayProxyHandler;
/**
* AWS Lambda handler for OAuth2 revoke requests.
*
* @example
* ```typescript
* const server = createOAuth2Server({ ... });
* export const revoke = apiGatewayRevokeHandler(server);
* ```
*/
declare function apiGatewayRevokeHandler(server: OAuth2Server): APIGatewayProxyHandler;
/**
* AWS Lambda handler for OAuth2 introspect requests.
*
* @example
* ```typescript
* const server = createOAuth2Server({ ... });
* export const introspect = apiGatewayIntrospectHandler(server);
* ```
*/
declare function apiGatewayIntrospectHandler(server: OAuth2Server): APIGatewayProxyHandler;
export { apiGatewayAuthorizeHandler, apiGatewayIntrospectHandler, apiGatewayRevokeHandler, apiGatewayTokenHandler, createOAuth2Router, expressAuthorizeHandler, expressIntrospectHandler, expressRevokeHandler, expressTokenHandler, extractOAuth2Request, transformOAuth2Error, transformOAuth2Response, validateOAuth2Token };
export type { ExpressOAuth2Options };
//# sourceMappingURL=index.d.ts.map