Security and Authorizers
Relevant source files
The following files were used as context for generating this wiki page:
Security and Authorizers provide a framework for securing GraphQL endpoints and controlling data access based on user context. The system integrates standard NestJS guards with specialized authorizers that apply filters to incoming queries and mutations.
This module ensures that users only interact with records they have permission to access. It achieves this by intercepting requests at the resolver level and injecting authorization-specific filters into the underlying service calls.
Sources: packages/query-graphql/src/auth/authorizer.ts, examples/auth/src/auth/jwt-auth.guard.ts
Authentication and Guards
Authentication identifies the user through guards that intercept HTTP or GraphQL requests. The project commonly uses JWT-based authentication to establish a security context.
JWT Authentication Guard
The JwtAuthGuard extends the NestJS AuthGuard for 'jwt'. In GraphQL contexts, it extracts the request from the ExecutionContext using GqlExecutionContext. If the authorization header is missing or the token is invalid, the guard throws an UnauthorizedException.
Sources: examples/auth/src/auth/jwt-auth.guard.ts:7-17, examples/auth/e2e/todo-item.resolver.spec.ts:54-68
The diagram shows how the JwtAuthGuard validates the user before the request reaches the resolver.
Sources: examples/auth/src/auth/jwt-auth.guard.ts:7-17
Authorizers
Authorizers define the business logic for data access control. They translate a user's security context into filters that restrict the scope of CRUD operations.
Authorizer Interface
The Authorizer<DTO> interface defines methods to generate filters for different GraphQL operations. Each method receives an AuthorizationContext, which contains the authenticated user information.
Sources: packages/query-graphql/src/auth/authorizer.ts:18-28
| Method | Purpose |
|---|---|
authorize(context) | Generates a filter for read/find operations. |
authorizeRelation(relationName, context) | Generates a filter for a specific relation. |
Custom Authorizer Implementation
Developers implement custom authorizers by extending the base class and providing logic for the authorize method. For example, a SubTaskAuthorizer might restrict sub-tasks based on the owner of the parent todo item.
Sources: examples/auth/src/sub-task/sub-task.authorizer.ts:6-14
export class SubTaskAuthorizer implements Authorizer<SubTaskDTO> {
authorize(context: UserContext): Promise<Filter<SubTaskDTO>> {
return Promise.resolve({ todoItem: { ownerId: { eq: context.user.id } } });
}
}
Sources: examples/auth/src/sub-task/sub-task.authorizer.ts:6-14
Data Access Flow
Security is enforced by applying authorization filters to standard query filters. When a user requests data, the system merges the user's input filter with the filter generated by the authorizer.
Filter Application Process
- Intercept Request: The resolver receives a query or mutation.
- Generate Auth Filter: The authorizer uses the
AuthorizationContextto create a security filter. - Merge Filters: The system combines the user's provided filter with the security filter.
- Execute Service: The combined filter is passed to the
QueryService.
Sources: packages/query-graphql/src/auth/authorizer.ts:18-28, examples/auth/e2e/todo-item.resolver.spec.ts:392-410
The flowchart illustrates the path from the initial request to the final database query with enforced security filters.
Sources: packages/query-graphql/src/auth/authorizer.ts, examples/auth/src/auth/jwt-auth.guard.ts
Operation-Level Security
Authorization is applied across all CRUD operations, ensuring consistency between reading, creating, and modifying data.
Read Operations
For find or query operations, the authorizer applies a filter to the where clause. If a user tries to access an ID they do not own, the service returns "Unable to find" or an empty result set.
Sources: examples/auth/e2e/todo-item.resolver.spec.ts:88-102, examples/auth/e2e/todo-item.resolver.spec.ts:610-625
Mutations (Create, Update, Delete)
Mutations utilize the same authorizer logic.
- Update/Delete One: The system filters by ID and the authorizer filter. If the record is outside the authorized scope, the operation fails.
- Create One: Authorizers can enforce ownership by validating the input or applying hooks to set owner fields.
Sources: examples/auth/e2e/todo-item.resolver.spec.ts:608-638, examples/hooks/e2e/todo-item.resolver.spec.ts:515-545
Relation Security
Relation authorization prevents unauthorized access to linked entities. The authorizeRelation method allows specific rules for different DTO relationships.
Sources: packages/query-graphql/src/auth/authorizer.ts:27-28, examples/auth/e2e/todo-item.resolver.spec.ts:1015-1048
Error Handling
The security system generates specific errors when authorization fails or is misconfigured.
- Unauthorized: Thrown by guards when credentials are missing or invalid.
- Forbidden resource: Thrown when a user attempts a mutation they do not have permission for.
- Unable to find: Returned when a filter (including the authorizer filter) excludes the requested ID.
- Missing AuthorizationContext: Thrown if a custom method uses
@AuthorizerFilterbut the context is not provided.
Sources: examples/auth/e2e/todo-item.resolver.spec.ts:54-68, examples/auth/e2e/todo-item.resolver.spec.ts:392-410, examples/hooks/e2e/todo-item.resolver.spec.ts:480-495
Summary
Security and Authorizers in nestjs-query provide a robust mechanism for multi-tenant and ownership-based access control. By using standard NestJS guards for identity and specialized Authorizer classes for data filtering, the system ensures that security rules are consistently applied across all GraphQL resolvers and operations.