A comprehensive OAuth2.0 and OpenID Connect authentication service built with Spring Boot, providing enterprise-grade authentication and authorization for the SmartDrive platform.
- β Authorization Code Flow (Most secure for web applications)
- β Client Credentials Flow (Service-to-service authentication)
- β Password Grant (For trusted clients)
- β Refresh Token Flow (Token renewal)
- β JWT Tokens (Access and Refresh tokens)
- β Role-Based Access Control (RBAC)
- β PKCE Support (Proof Key for Code Exchange)
- β Token Introspection (RFC 7662)
- β Token Revocation (RFC 7009)
- β Audit Logging (Complete authentication events)
- β Rate Limiting (Per-user and per-endpoint)
- β User Registration with email verification
- β Password Security (BCrypt with strength 12)
- β Account Lockout (After failed attempts)
- β Session Management (Redis-based)
- β Multi-Factor Authentication (Ready for implementation)
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Frontend β β API Gateway β β Auth Service β
β (Future) β β (Port 8080) β β (Port 8085) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βββββββββββββββββββββββββΌββββββββββββββββββββββββ
β
βββββββββββββββββββββββββΌββββββββββββββββββββββββ
β β β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β PostgreSQL β β Redis β β Business β
β (User Data) β β (Sessions) β β Services β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
GET /oauth2/authorize
Parameters:
response_type
(required): "code"client_id
(required): OAuth2 client identifierredirect_uri
(required): Callback URLscope
(optional): Requested scopesstate
(optional): CSRF protectioncode_challenge
(optional): PKCE challengecode_challenge_method
(optional): PKCE method (S256)
Example:
curl "http://localhost:8085/oauth2/authorize?response_type=code&client_id=smartdrive-web&redirect_uri=http://localhost:3000/callback&scope=read write&state=abc123"
POST /oauth2/token
Authorization Code Grant:
curl -X POST http://localhost:8085/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "client_id=smartdrive-web" \
-d "client_secret=your-client-secret" \
-d "code=authorization_code" \
-d "redirect_uri=http://localhost:3000/callback"
Password Grant:
curl -X POST http://localhost:8085/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=password" \
-d "client_id=smartdrive-api" \
-d "client_secret=your-client-secret" \
-d "[email protected]" \
-d "password=password123" \
-d "scope=read write"
Refresh Token Grant:
curl -X POST http://localhost:8085/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token" \
-d "client_id=smartdrive-web" \
-d "client_secret=your-client-secret" \
-d "refresh_token=your-refresh-token"
GET /oauth2/userinfo
Example:
curl -H "Authorization: Bearer your-access-token" \
http://localhost:8085/oauth2/userinfo
POST /oauth2/introspect
Example:
curl -X POST http://localhost:8085/oauth2/introspect \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "token=your-access-token" \
-d "client_id=smartdrive-api" \
-d "client_secret=your-client-secret"
POST /oauth2/revoke
Example:
curl -X POST http://localhost:8085/oauth2/revoke \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "token=your-access-token" \
-d "client_id=smartdrive-web" \
-d "client_secret=your-client-secret"
GET /.well-known/oauth-authorization-server
Example:
curl http://localhost:8085/.well-known/oauth-authorization-server
GET /oauth2/jwks
Example:
curl http://localhost:8085/oauth2/jwks
- Client ID:
smartdrive-web
- Type: Public
- Grant Types: authorization_code, refresh_token
- Redirect URIs:
- Scopes: read, write
- Client ID:
smartdrive-api
- Type: Confidential
- Grant Types: client_credentials, password, refresh_token
- Scopes: read, write, admin
- SMARTDRIVE_ADMIN: Full system access
- SMARTDRIVE_USER: Personal file management
- SMARTDRIVE_VIEWER: Read-only access
- SMARTDRIVE_GUEST: Limited access
-
Admin User:
- Username:
admin
- Email:
[email protected]
- Password:
admin123
- Role:
SMARTDRIVE_ADMIN
- Username:
-
Test User:
- Username:
user
- Email:
[email protected]
- Password:
user123
- Role:
SMARTDRIVE_USER
- Username:
# Database Configuration
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=smartdrive_auth
POSTGRES_USER=keycloak
POSTGRES_PASSWORD=password
# Redis Configuration
REDIS_HOST=redis
REDIS_PORT=6379
# JWT Configuration
JWT_SECRET=your-256-bit-secret-key-here-make-it-long-and-secure-for-production
JWT_EXPIRATION=86400000
JWT_REFRESH_EXPIRATION=604800000
# Security Configuration
CORS_ALLOWED_ORIGINS=http://localhost:3000,http://localhost:8080
server:
port: 8085
spring:
application:
name: auth-service
datasource:
url: jdbc:postgresql://${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
username: ${POSTGRES_USER}
password: ${POSTGRES_PASSWORD}
jpa:
hibernate:
ddl-auto: validate
show-sql: false
data:
redis:
host: ${REDIS_HOST}
port: ${REDIS_PORT}
app:
jwt:
secret: ${JWT_SECRET}
expiration: ${JWT_EXPIRATION}
refresh-expiration: ${JWT_REFRESH_EXPIRATION}
issuer: smartdrive-auth-service
security:
password:
encoder:
strength: 12
cors:
allowed-origins: ${CORS_ALLOWED_ORIGINS}
- Docker and Docker Compose
- Java 17+
- PostgreSQL
- Redis
-
Start the services
docker compose up -d postgres redis
-
Run the auth service
cd auth-service ./gradlew bootRun
-
Test the endpoints
# Test health check curl http://localhost:8085/actuator/health # Test OAuth2 discovery curl http://localhost:8085/.well-known/oauth-authorization-server
# Build and start all services
docker compose up -d
# View logs
docker compose logs -f auth-service
# Stop services
docker compose down
# Service health
curl http://localhost:8085/actuator/health
# Database health
curl http://localhost:8085/actuator/health/db
# Redis health
curl http://localhost:8085/actuator/health/redis
# Prometheus metrics
curl http://localhost:8085/actuator/prometheus
# Application metrics
curl http://localhost:8085/actuator/metrics
# View service logs
docker compose logs -f auth-service
# Filter authentication events
docker compose logs auth-service | grep "AUTH"
- Secret Key: Use a strong, randomly generated secret (256+ bits)
- Token Expiration: Short-lived access tokens (1 hour)
- Refresh Tokens: Longer-lived but revocable (7 days)
- Token Storage: Store in HttpOnly cookies or secure storage
- PKCE: Always use PKCE for public clients
- State Parameter: Use state parameter for CSRF protection
- Redirect URIs: Validate redirect URIs strictly
- Scope Validation: Validate requested scopes
- Password Hashing: BCrypt with strength 12
- SQL Injection: Use parameterized queries
- Connection Security: Use SSL/TLS for database connections
./gradlew test
./gradlew integrationTest
# 1. Get authorization code
curl "http://localhost:8085/oauth2/authorize?response_type=code&client_id=smartdrive-web&redirect_uri=http://localhost:3000/callback&scope=read write&state=test123"
# 2. Exchange code for token
curl -X POST http://localhost:8085/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code&client_id=smartdrive-web&code=YOUR_CODE&redirect_uri=http://localhost:3000/callback"
# 3. Use access token
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
http://localhost:8085/oauth2/userinfo
- URL: http://localhost:8085/swagger-ui.html
- OpenAPI Spec: http://localhost:8085/api-docs
- RFC 6749: OAuth 2.0 Authorization Framework
- RFC 6750: OAuth 2.0 Bearer Token Usage
- RFC 7009: OAuth 2.0 Token Revocation
- RFC 7662: OAuth 2.0 Token Introspection
-
Database Connection
# Check PostgreSQL is running docker compose ps postgres # Check database connection docker compose logs auth-service | grep "database"
-
JWT Token Issues
# Validate token format echo "YOUR_TOKEN" | cut -d'.' -f2 | base64 -d | jq # Check token expiration curl -X POST http://localhost:8085/oauth2/introspect \ -d "token=YOUR_TOKEN"
-
Redis Connection
# Check Redis is running docker compose ps redis # Test Redis connection docker compose exec redis redis-cli ping
# View all logs
docker compose logs auth-service
# Filter by log level
docker compose logs auth-service | grep "ERROR"
# Follow logs in real-time
docker compose logs -f auth-service
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.