This document outlines the security considerations and implementation details for the saudiMART B2B marketplace platform.
The platform employs a JSON Web Token (JWT) based authentication strategy. Upon successful login via the MartAuth service, a user is issued a JWT. This token is then used by the client to authenticate subsequent requests to other microservices. JWTs are stateless, making them suitable for a microservices architecture.
A dedicated API Gateway (not explicitly shown as a separate module in the current project structure, but assumed for a production setup) serves as the entry point for all external requests. The gateway is responsible for:
- Receiving Requests: Intercepting all incoming API requests.
- Routing Authentication Requests: Directing login and signup requests to the
MartAuthservice. - JWT Validation: For all other requests (to downstream services like
MartProduct), the gateway validates the JWT provided in theAuthorization: Bearer <token>header. - User Information Propagation: Upon successful JWT validation, the gateway extracts key user information, such as the
user_idandrole(from theuserstable in the database, as defined inDatabaseDesign.md), and injects this information into the request headers (e.g.,X-User-Id,X-User-Roles) before forwarding the request to the appropriate downstream microservice.
User roles are defined in the users table of the database (role column) and can be 'BUYER', 'SELLER', or 'ADMIN'. These roles are crucial for authorization:
- MartAuth Module: The
MartAuthmodule uses the roles extracted from the JWT (validated by its internalJwtRequestFilter) to enforce method-level authorization, particularly in theUserController, using@PreAuthorizeannotations to restrict access to specific user management operations based on roles (e.g., only ADMINs can manage user accounts). - Downstream Services (e.g., MartProduct): Microservices like
MartProductrely on the user information (specifically the roles) passed by the API Gateway in the request headers. TheGatewayAuthFilterin these services reads these headers and sets the security context. This allows the downstream services to perform their own authorization checks based on the user's role. For example, in theMartProductservice, authorization logic would ensure that only sellers can create or update products (linked via theseller_idin theproductstable), while buyers can only view product listings.
- Password Hashing: User passwords are not stored in plain text. The
MartAuthservice uses a strong one-way hashing algorithm (BCrypt, as configured inSecurityConfig) to store password hashes in thepassword_hashcolumn of theuserstable. - Data Separation by Role: The database schema (
DatabaseDesign.md) incorporates mechanisms for data separation based on roles, particularly for sellers. For example, theproductstable includes aseller_idforeign key referencing theuserstable. This allows theMartProductservice to easily filter and retrieve only the products belonging to the authenticated seller, preventing sellers from accessing or modifying products owned by others. Similar patterns are expected for other seller-specific data (e.g.,warehouses,inventory,contractswhere the seller is involved).
+-----------+ +----------------+ +-----------+ | Client | <--> | API Gateway | <--> | MartAuth | +-----------+ +----------------+ +-----------+ ^ | | | (Validated JWT, | | User Info Headers) | | | +------------+ | | Downstream | +------------> | Service | (e.g., MartProduct) +------------+
Security Flow:
- Client sends Login/Signup request to API Gateway.
- API Gateway routes request to MartAuth service.
- MartAuth authenticates user and issues JWT.
- Client includes JWT in subsequent requests to API Gateway for other services.
- API Gateway intercepts request, validates JWT, extracts user info (ID, roles).
- API Gateway adds user info as headers (X-User-Id, X-User-Roles) to the request.
- API Gateway routes request with headers to the appropriate Downstream Service (e.g., MartProduct).
- Downstream Service's GatewayAuthFilter reads user info from headers.
- Downstream Service performs authorization based on user roles.
+-----------+ +-----------+ | Client | | Eureka | +-----------+ | Server | | +-----------+ | HTTP Request (with JWT in Auth Header) ^ |----------------------------------------------->| | | Service Registration | | Service Discovery | | +-----------+ | | API Gateway | | +-----------+ | | 1. Intercepts Request | | 2. JWT Validation (Auth, AuthZ, Expire Check)| | (If invalid/expired, returns 401) | | | | 3. Modify Request (Add X-User-Id, X-User-Roles Headers) | | | 4. Service Discovery (via Eureka)------------->| | (Checks Eureka for Microservice location) | | | | 5. Forwards Request (with Headers) --------->| | | | +--------------+ | | Microservice | | | (e.g., MartProduct) | +--------------+ | | | | 6. Security Filter (e.g., GatewayAuthFilter) | | Reads X-User-Id, X-User-Roles Headers | | Sets Security Context | | | | 7. Authorization Checks (based on User/Role) | | 8. Executes Business Logic | | |<---------------------------------------------| | 9. HTTP Response (Data) | |<---------------------------------------------| | 10. Forwards Response | +-----------+ | Client | +-----------+