How chatok-jnr/legalaid-user-auth works
1. Service role and deployment boundary
This repository is a single Spring Boot 3.2 microservice for the Legal Aid platform’s user authentication and profile domain. It exposes REST endpoints under /api/v1 and runs as a standalone Java 21 application, packaged with Maven and deployable as a JAR or Docker container.
2. Primary request flow
Incoming HTTP requests enter the controller layer, are authenticated/authorized by Spring Security and a JWT filter, then delegated to service classes. Services orchestrate validation, token logic, persistence, and external uploads before returning DTO responses. Error handling is centralized so controllers stay thin.
3. Security and identity subsystem
The core security boundary is JWT-based authentication built on Spring Security 6. Access tokens are HS256-signed JWTs handled by JwtService and enforced by JwtAuthenticationFilter. UserDetailsServiceImpl bridges Spring Security to the user store. SecurityConfig and CorsConfig define the filter chain, role-based access control, and cross-origin policy.
4. Authentication and session lifecycle
AuthController and AuthServiceImpl implement registration, login, refresh, and “me” endpoints. The system uses BCrypt for password hashing, opaque UUID refresh tokens persisted in the database, and refresh-token rotation with reuse detection. RefreshToken is a first-class entity, so session state is not purely stateless JWT.
5. User and role data model
The shared identity model centers on User, Role, and UserRole, with UserAddress and UserAddressId for user-owned addresses. UserRole and RoleRepository support multi-role users such as CLIENT, LAWYER, and ADMIN. The model is relational and JPA-backed rather than document-based.
6. Role-specific profile subsystems
The repository separates profile management by audience: ClientController / ClientService, LawyerController / LawyerService, and AdminController / AdminService. Each role has its own entity package, DTOs, and repositories, which keeps client, lawyer, and admin data isolated while still anchored to the shared User identity.
7. Address management subsystem
AddressController and UserAddressServiceImpl manage authenticated users’ addresses. The presence of UserAddress, UserAddressRepository, and a composite ID indicates addresses are uniquely keyed per user and address type. This is a user-owned subdomain layered on top of the common identity model.
8. Cloudinary integration boundary
Lawyer document uploads are the only explicit external file-storage integration. CloudinaryServiceImpl and CloudinaryConfig wrap Cloudinary credentials and upload settings, and are used for lawyer profile document storage. This is a separate external I/O boundary from the core database-backed profile logic.
9. Persistence layer
All durable state lives in PostgreSQL through Spring Data JPA/Hibernate repositories. The repository package is split by domain: shared identity, refresh tokens, user addresses, and role-specific profile repositories. Projection interfaces in the lawyer repository suggest read-optimized queries for admin or profile views without loading full entity graphs.
10. DTO and API contract layer
Request and response payloads are kept in dedicated DTO packages, grouped by domain and by direction. This indicates the API contract is intentionally decoupled from entity shapes, which is important for protecting internal persistence structure and supporting PATCH-style partial updates.
11. Error handling and response consistency
AuthExceptions and GlobalExceptionHandler define the domain error boundary. The exception handler likely maps business failures into consistent API errors rather than exposing persistence or security internals directly. This is important for diagramming because it sits cross-cutting over all controllers and services.
12. Operational and build context
The project is Maven-based, uses application.yml plus environment variables for runtime configuration, and includes Docker and deployment scripts, which means it is designed to run consistently across local, container, and production environments. The tests cover auth, JWT, cloudinary, controller, and lawyer service paths, showing those are the most critical integration seams.
Main entry points: repository Repositories · UserAuthApplication.java UserAuth App · dto DTOs · GlobalExceptionHandler.java Error Handler
Components 21
Platform
Application entrypoint and runtime configuration
- UserAuth App · Standalone Java 21 Spring Boot service entrypoint
src/main/java/com/legalaid/userauth/UserAuthApplication.java
API
REST controllers and DTO contracts
- Auth API · Registration, login, refresh, and me endpoints
src/main/java/com/legalaid/userauth/controller/AuthController.java - Client API · Client profile endpoints
src/main/java/com/legalaid/userauth/controller/ClientController.java - Lawyer API · Lawyer profile and credential endpoints
src/main/java/com/legalaid/userauth/controller/LawyerController.java - Admin API · Admin management endpoints
src/main/java/com/legalaid/userauth/controller/AdminController.java - Address API · Authenticated address endpoints
src/main/java/com/legalaid/userauth/controller/AddressController.java - DTOs · Request and response payloads separated from entities
src/main/java/com/legalaid/userauth/dto
Security
JWT authentication and Spring Security boundary
- Security Config · Filter chain, CORS, and authorization rules
src/main/java/com/legalaid/userauth/config/SecurityConfig.java - JWT Filter · Extracts and validates bearer tokens on requests
src/main/java/com/legalaid/userauth/security/JwtAuthenticationFilter.java - JWT Service · Creates and validates HS256 access tokens
src/main/java/com/legalaid/userauth/security/JwtService.java - User Details · Adapts stored users to Spring Security
src/main/java/com/legalaid/userauth/security/UserDetailsServiceImpl.java
Domain
Identity, profile, and session models
- Auth Service · Registration, login, refresh rotation, and current-user logic
src/main/java/com/legalaid/userauth/service/impl/AuthServiceImpl.java - Client Service · Client profile orchestration
src/main/java/com/legalaid/userauth/service/client/impl/ClientServiceImpl.java - Lawyer Service · Lawyer profile, credentials, and document flow
src/main/java/com/legalaid/userauth/service/lawyer/impl/LawyerServiceImpl.java - Admin Service · Admin profile and action management
src/main/java/com/legalaid/userauth/service/admin/impl/AdminServiceImpl.java - Address Service · User-owned address management
src/main/java/com/legalaid/userauth/service/impl/UserAddressServiceImpl.java - Identity Model · User, role, user-role, refresh token, and address entities
src/main/java/com/legalaid/userauth/entity - Role Profiles · Client, lawyer, and admin profile entities
src/main/java/com/legalaid/userauth/entity
Persistence
JPA repositories and stored state
- Repositories · Spring Data persistence for users, tokens, addresses, and role profiles
src/main/java/com/legalaid/userauth/repository
External
Third-party integration boundary
- Cloudinary · External document upload integration for lawyer profiles
src/main/java/com/legalaid/userauth/service/cloudinary/impl/CloudinaryServiceImpl.java
Errors
Centralized exception mapping
- Error Handler · Converts domain and auth failures into API errors
src/main/java/com/legalaid/userauth/exception/GlobalExceptionHandler.java
Connections 40
- UserAuth App → Security Config: boots with · Application starts with security and web configuration
- UserAuth App → Auth API: exposes · Primary authentication REST surface
- UserAuth App → Client API: exposes · Client profile REST surface
- UserAuth App → Lawyer API: exposes · Lawyer profile REST surface
- UserAuth App → Admin API: exposes · Admin management REST surface
- UserAuth App → Address API: exposes · Address management REST surface
- Auth API → Auth Service: delegates to · Thin controller hands off auth logic
- Client API → Client Service: delegates to · Client endpoints route to service layer
- Lawyer API → Lawyer Service: delegates to · Lawyer endpoints route to service layer
- Admin API → Admin Service: delegates to · Admin endpoints route to service layer
- Address API → Address Service: delegates to · Address endpoints route to service layer
- Auth API → Error Handler: errors mapped by · Auth errors are normalized centrally
- Client API → Error Handler: errors mapped by · Client errors are normalized centrally
- Lawyer API → Error Handler: errors mapped by · Lawyer errors are normalized centrally
- Admin API → Error Handler: errors mapped by · Admin errors are normalized centrally
- Address API → Error Handler: errors mapped by · Address errors are normalized centrally
- Security Config → JWT Filter: registers · Security chain includes JWT request filtering
- Security Config → User Details: uses · Security config relies on user lookup bridge
- JWT Filter → JWT Service: validates with · Bearer token parsing and verification
- JWT Filter → User Details: loads principal · Filter resolves authenticated user details
- Auth Service → JWT Service: issues tokens · Access tokens are generated and validated here
- Auth Service → Repositories: persists session data · Uses user and refresh-token repositories
- Auth Service → Identity Model: works with · Operates on users, roles, and refresh tokens
- Client Service → Repositories: persists profile · Stores client profile state
- Client Service → Identity Model: anchors on · Client profile is attached to shared identity
- Lawyer Service → Repositories: persists profile · Stores lawyer profile and credential state
- Lawyer Service → Cloudinary: uploads via · Lawyer documents are sent to external storage
- Lawyer Service → Role Profiles: manages · Operates on lawyer-specific profile entities
- Admin Service → Repositories: persists profile · Stores admin profile and audit state
- Admin Service → Role Profiles: manages · Operates on admin-specific profile entities
- Address Service → Repositories: persists addresses · Uses composite-key address repository
- Address Service → Identity Model: anchors on · Addresses belong to the authenticated user
- Repositories → Identity Model: stores · User, role, refresh token, and address persistence
- Repositories → Role Profiles: stores · Client, lawyer, and admin profile persistence
- DTOs → Auth API: contracts for · Auth payloads define API request and response shapes
- DTOs → Client API: contracts for · Client payloads define API request and response shapes
- DTOs → Lawyer API: contracts for · Lawyer payloads define API request and response shapes
- DTOs → Admin API: contracts for · Admin payloads define API request and response shapes
- DTOs → Address API: contracts for · Address payloads define API request and response shapes
- Cloudinary → Repositories: supports · Uploads complement persisted lawyer profile data