Authentication and Authorization
Relevant source files
The following files were used as context for generating this wiki page:
- packages/auth/src/lib/mcp/server/oauth-authorization-server.ts
- apps/mcp/README.md
- packages/mcp-server/README.md
- README.md
- packages/auth/package.json
- apps/mcp-auth/package.json
- packages/ui-auth/package.json
- packages/mcp-server/examples/generate-test-token.js
- apps/gauzy-e2e/src/support/step_definitions/ReportsTest/ReportsTest.ts
The Gauzy platform implements a comprehensive and robust authentication and authorization system designed to secure its various components, including the main platform API, the UI, and the Model Context Protocol (MCP) server used for AI assistant integrations. The system is built on modern standards like OAuth 2.0 and JWT, utilizing a modular architecture based on NestJS and Passport.js. This allows for flexible integration of multiple authentication strategies and provides enterprise-grade security features.
A key component is the dedicated OAuth 2.0 Authorization Server, which handles user authentication, client registration, token issuance, and validation. This server supports standard grant types, ensuring secure and standardized access for both first-party applications (like the Gauzy UI) and third-party clients. The system also includes features like smart authentication, automatic token management, session persistence, and scope-based access control to enforce fine-grained permissions across the platform.
Sources: README.md, packages/mcp-server/README.md:1-10, packages/auth/src/lib/mcp/server/oauth-authorization-server.ts:1-8
Core Components and Packages
The authentication and authorization functionality is distributed across several key packages within the monorepo.
| Package Name | Description | Source File |
|---|---|---|
@gauzy/auth | The core authentication module. It provides various Passport.js strategies (JWT, OAuth2, social logins) and the implementation of the OAuth 2.0 Authorization Server. | packages/auth/package.json |
@gauzy/mcp-auth | A dedicated, standalone OAuth 2.0 Authorization Server application built using the @gauzy/auth package. It acts as the Identity Server for the MCP ecosystem. | apps/mcp-auth/package.json |
@gauzy/ui-auth | An Angular UI module providing reusable components and services for managing user authentication in the frontend application. | packages/ui-auth/package.json |
@gauzy/mcp-server | The core MCP server package. It consumes the authentication services to secure its HTTP and WebSocket transports via OAuth 2.0 Bearer tokens. | packages/mcp-server/README.md |
OAuth 2.0 Authorization Server
The platform includes a full-featured OAuth 2.0 Authorization Server, implemented in the OAuth2AuthorizationServer class. This server is central to securing the MCP server and other resources. It is compliant with several RFCs, including RFC 6749 (OAuth 2.0), RFC 6750 (Bearer Token), and RFC 8414 (Authorization Server Metadata).
Sources: packages/auth/src/lib/mcp/server/oauth-authorization-server.ts:1-8, apps/mcp/README.md:315-320
Architecture and Key Features
The server is built with Express.js and includes a suite of security and session management features.
- Supported Grant Types:
- Authorization Code Flow (with PKCE)
- Client Credentials
- Refresh Token
- Token Management: Generates, manages, and validates JWTs. It uses the
joselibrary and supports key rotation via a JWKS endpoint. - Session Management: Uses
express-sessionwith an optionalconnect-redisstore for scalable, persistent sessions in production environments. - Security Middleware:
helmet: Applies various security-related HTTP headers.express-rate-limit: Protects against brute-force attacks.csrf-csrf: Implements Double Submit Cookie pattern for CSRF protection on state-changing form submissions (e.g., login, consent).
- Client Management: Supports dynamic client registration (RFC 7591) and validation.
This diagram illustrates the main components of the OAuth2AuthorizationServer.
Endpoints
The server exposes standard OAuth 2.0 and related endpoints.
| Endpoint | Method | Description |
|---|---|---|
/.well-known/oauth-authorization-server | GET | Publishes server metadata as per RFC 8414, including endpoint locations and supported capabilities. |
/.well-known/jwks.json | GET | Exposes the JSON Web Key Set (JWKS) containing public keys for verifying JWT signatures. |
/oauth2/authorize | GET, POST | Handles the user authentication and consent step of the Authorization Code flow. The POST endpoint is CSRF-protected. |
/oauth2/token | POST | Exchanges authorization codes, refresh tokens, or client credentials for access tokens. |
/oauth2/introspect | POST | Validates a token and returns its metadata, as per RFC 7662. Requires client authentication. |
/oauth2/userinfo | GET | Returns claims about the authenticated end-user. Requires an access token with openid, profile, or email scope. |
/oauth2/register | POST | Allows dynamic registration of new OAuth clients (if enabled). |
/login | GET, POST | A user-facing login form. The POST endpoint for submitting credentials is CSRF-protected. |
Sources: packages/auth/src/lib/mcp/server/oauth-authorization-server.ts:548-615, apps/mcp/README.md:337-346
Authorization Code Flow with PKCE
The following sequence diagram shows the Authorization Code flow, which is the primary and most secure method for third-party applications to obtain user-delegated access.
Configuration
The OAuth2AuthorizationServer is configured via the OAuth2ServerConfig interface.
| Parameter | Type | Description |
|---|---|---|
issuer | string | The identifier for the authorization server (e.g., https://auth.gauzy.co). |
baseUrl | string | The base URL of the server, used to construct full endpoint URLs. |
audience | string | The default audience for JWTs, typically the URI of the resource server (e.g., https://mcp.gauzy.co). |
enableClientRegistration | boolean | Toggles the client registration endpoint. |
authorizationEndpoint | string | Path for the authorization endpoint (e.g., /oauth2/authorize). |
tokenEndpoint | string | Path for the token endpoint (e.g., /oauth2/token). |
jwksEndpoint | string | Path for the JWKS endpoint (e.g., /.well-known/jwks.json). |
sessionSecret | string | A secret key for signing session ID cookies and CSRF tokens. |
redisUrl | string | Optional connection URL for a Redis server to be used as a session store. |
Sources: packages/auth/src/lib/mcp/server/oauth-authorization-server.ts:24-39
MCP Server Authentication
The Model Context Protocol (MCP) server, which integrates with AI assistants, supports multiple transport layers, each with a different authentication strategy.
| Transport | Authentication Method - |
|---|---|
| Stdio | Relies on environment variables (GAUZY_AUTH_EMAIL, GAUZY_AUTH_PASSWORD) for authentication. It is designed for trusted, local execution environments like the Claude Desktop app, where the server process is isolated. OAuth is not supported. |
| HTTP | When enabled (MCP_AUTH_ENABLED=true), it secures its JSON-RPC endpoint (/sse) using OAuth 2.0 Bearer tokens. Unauthorized requests receive a 401 Unauthorized response with a WWW-Authenticate header pointing to the resource metadata. |
| WebSocket | Similar to HTTP, it uses OAuth 2.0 Bearer tokens for authorization when enabled. The token is typically passed during the connection upgrade request. It also supports session management via Redis for stateful connections. |
Sources: apps/mcp/README.md:23-49, 273-278, packages/mcp-server/README.md:12-20
Passport.js Integration
The @gauzy/auth package heavily relies on Passport.js, the standard authentication middleware for Node.js. This is evident from the numerous passport-* strategies listed as dependencies, which enable a wide array of authentication methods.
passport-jwt: For authenticating with JSON Web Tokens.passport-oauth2: Base strategy for OAuth 2.0.- Social Logins:
passport-google-oauth20,passport-facebook,passport-github2,passport-linkedin-oauth2,passport-microsoft,passport-twitter. - Enterprise:
passport-auth0,passport-keycloak-oauth2-oidc.
This modular approach allows developers to easily enable or disable different authentication providers based on configuration.
Sources: packages/auth/package.json:28-49
Conclusion
The authentication and authorization system in Gauzy is a sophisticated, standards-based framework designed for security, scalability, and flexibility. By leveraging OAuth 2.0, JWT, and Passport.js, it provides a comprehensive solution for protecting APIs and resources. The standalone OAuth 2.0 Authorization Server is a critical piece of infrastructure that enables secure delegated access for both internal and external applications, while features like CSRF protection, rate limiting, and robust session management ensure a secure user experience.
Sources: README.md, packages/auth/src/lib/mcp/server/oauth-authorization-server.ts