A comprehensive Identity and Access Management (IAM) service built with Rust, featuring authentication, authorization, and user management capabilities.
- User authentication with JWT (access & refresh tokens)
- Role-Based Access Control (RBAC)
- Attribute-Based Access Control (ABAC)
- CRUD for users, roles, permissions, ABAC policies
- Assign roles/permissions/policies to users and roles
- Token validation, refresh, and logout
- API versioning (
/v1/...) - OpenAPI/Swagger documentation
- Modular, testable, and extensible architecture
- Rust 1.88+
- Axum (HTTP API)
- Postgres (primary DB)
- sqlx (async DB toolkit)
- bcrypt (password hashing)
- jsonwebtoken (JWT)
- utoipa (OpenAPI/Swagger)
- Docker (local dev)
- Domain Driven Design (DDD)
- Hexagonal/Clean Architecture
- CQRS (Commands/Queries)
- Modular:
domain,application,infrastructure,interface
- Rust 1.88.0 or later
- PostgreSQL 15
- Docker (for development)
-
Clone the repository
git clone <repository-url> cd authentication-service
-
Set up environment
cp .env.example .env # Edit .env with your database configuration -
Start the database
docker-compose up -d db
-
Run migrations
cargo install sqlx-cli --no-default-features --features postgres sqlx migrate run
-
Run the service
cargo run
-
View Swagger UI:
-
Start development environment:
make docker-run-dev
-
View logs:
make logs-dev
-
Access services:
- API: http://localhost:8080
- Swagger: http://localhost:8080/swagger
- pgAdmin: http://localhost:5050 (admin@example.com / admin)
-
Deploy to production:
make deploy-prod
-
With reverse proxy:
make docker-run-full
DATABASE_URL- Postgres connection stringJWT_SECRET- Secret for signing JWTsHTTP_HOST/HTTP_PORT- Server bind addressAPI_MODE- Server Mode option: http, grpc (future implement), both
- Swagger UI:
/swagger - OpenAPI JSON:
/openapi.json - All endpoints are tagged (
Auth,RBAC,ABAC) and documented.
# Lint and format
cargo clippy --all -- -D warnings
cargo fmt --all -- --check
# Unit tests (fast, no database)
unset DATABASE_URL cargo test --lib --bins --tests --workspace# Set up test database
./scripts/setup_test_db.sh
# Run integration tests
DATABASE_URL="postgres://test_user:test_pass@localhost:5433/test_auth_db" cargo test --test infrastructure_tests
# Clean up
./scripts/cleanup_test_db.sh# Run all tests with database
./scripts/run_tests_with_db.sh# Unit tests only
cargo test --lib
# Integration tests
cargo test --test integration_tests
# Infrastructure tests with database
DATABASE_URL="postgres://test_user:test_pass@localhost:5433/test_auth_db" cargo test --test infrastructure_testsThe project includes a comprehensive CI/CD pipeline with:
- Build: Compilation and dependency management
- Testing: Unit and integration tests with database
- Linting: Code quality checks with Clippy
- Formatting: Code style validation with rustfmt
- Coverage: Test coverage analysis (60% minimum)
- SonarCloud: Code quality and security analysis
- Coverage: Minimum 60% test coverage
- Code Quality: SonarCloud quality gates
- Security: Automated security scanning
- Performance: Build and test performance monitoring
- API Documentation - Interactive API docs
- Test Database Setup - Database testing guide
- CI/CD Setup - Complete pipeline documentation
- SonarCloud Setup - Quality analysis setup
- Production:
Dockerfile- Optimized multi-stage build - Development:
Dockerfile.dev- With hot reloading
- Production:
docker-compose.prod.yml- Full production stack - Development:
docker-compose.dev.yml- Development environment
# Development
make docker-run-dev
# Production
make deploy-prod
# With reverse proxy
make docker-run-full
# View logs
make logs-dev
# Stop services
make docker-stop- User: User entity with authentication and role management
- Role: Role-based access control implementation
- Permission: Fine-grained permission system
- ABAC Policy: Attribute-based access control policies
- AuthService: Authentication logic
- TokenService: JWT token management
- PasswordService: Password hashing and verification
- AuthZService: Authorization logic
- PostgreSQL Repositories: Database implementations
- In-Memory Repositories: Testing and development
- Migration System: Database schema management
- HTTP Handlers: REST API endpoints
- gRPC Services: High-performance RPC endpoints
- Middleware: Authentication and authorization middleware
POST /v1/iam/login- User loginPOST /v1/iam/validate-token- Token validationPOST /v1/iam/refresh-token- Token refreshPOST /v1/iam/logout- User logout
POST /v1/iam/roles- Create roleGET /v1/iam/roles- List rolesDELETE /v1/iam/roles/{id}- Delete rolePOST /v1/iam/roles/assign- Assign role to userPOST /v1/iam/roles/remove- Remove role from user
POST /v1/iam/permissions- Create permissionGET /v1/iam/permissions- List permissionsDELETE /v1/iam/permissions/{id}- Delete permissionPOST /v1/iam/permissions/assign- Assign permission to rolePOST /v1/iam/permissions/remove- Remove permission from role
POST /v1/iam/abac/policies- Create ABAC policyGET /v1/iam/abac/policies- List ABAC policiesDELETE /v1/iam/abac/policies/{id}- Delete ABAC policyPOST /v1/iam/abac/assign- Assign ABAC policy
The project maintains high code quality standards:
- Rustfmt: Consistent code formatting
- Clippy: Linting and best practices
- SonarCloud: Code quality and security analysis
- Test Coverage: Comprehensive test suite
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
- Unit Tests: Test individual components in isolation
- Integration Tests: Test component interactions
- Database Tests: Test with real PostgreSQL database
- API Tests: Test HTTP endpoints end-to-end
# Build the image
docker build -t authentication-service .
# Run with environment variables
docker run -p 8080:8080 \
-e DATABASE_URL=postgres://user:pass@host:5432/db \
-e JWT_SECRET=your_secret \
authentication-service# Deploy with Docker Compose
make deploy-prod
# Deploy with reverse proxy
make docker-run-full
# View logs
make logs
# Health check
make health-dockerFor production deployment, set these environment variables:
# Database
DATABASE_URL=postgres://user:pass@host:5432/db
# Security
JWT_SECRET=your-super-secret-jwt-key-change-in-production
# Server
HTTP_HOST=0.0.0.0
HTTP_PORT=8080
# Logging
RUST_LOG=info- Domain models: User, Role, Permission, Token (with business logic and unit tests)
- Application layer: Commands, handlers, services for login, token issuance, validation, refresh
- Infrastructure: PostgresUserRepository, PostgresRefreshTokenRepository
- Interface: Axum HTTP endpoints for /iam/login, /iam/validate-token, /iam/refresh-token, /iam/logout, /iam/roles, /iam/permissions, /iam/abac/policies, etc.
- Environment-based config (dotenvy for secrets, DB URL, API mode, host, and port)
- Docker Compose for Postgres
- Database migrations for users, roles, user_roles, refresh_tokens
- Manual E2E testing via curl/Postman
- DDD/hexagonal structure (domain, application, infrastructure, interface)
- Secure JWT/refresh token logic (with jti, DB storage, revocation, and tests for revoked tokens)
- OpenTelemetry tracing for all major flows (HTTP), with golden signal events (latency, traffic, errors, success) instrumented in all handlers
- OpenAPI/Swagger spec for all endpoints (all endpoints documented, secured, and described in Swagger UI)
- Modular handler/middleware structure: all HTTP handlers and AppState are shared and testable
- Global JWT authentication middleware (all routes except /iam/login)
- Per-route RBAC/ABAC checks via extractor and AuthZService
- Full integration and E2E test coverage for all endpoints
- Endpoint versioning (if required)
- RBAC: Role hierarchies (role inheritance)
- RBAC: Permission groups and metadata
- RBAC: User-role assignment/listing endpoints
- RBAC: Role-permission assignment/listing endpoints
- RBAC: List effective permissions for a user
- ABAC: Policy CRUD (create, update, delete, list)
- ABAC: Policy assignment to users/roles/resources
- ABAC: Support more condition operators (in, gt, lt, etc.)
- ABAC: Policy evaluation endpoint
- ABAC: Policy priorities/conflict resolution
- Harden refresh token rotation, add audit logging
- Add user registration, password change/reset endpoints
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support:
- Create an issue in the GitHub repository
- Check the documentation in the
/docsdirectory - Review the API documentation at
/swagger