The Authentication Service uses SQLite3 as the primary database, providing a lightweight, file-based solution that's perfect for development and small to medium-scale deployments.
spring:
datasource:
url: jdbc:sqlite:authdb.sqlite
driver-class-name: org.sqlite.JDBC
username:
password:
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.community.dialect.SQLiteDialect
format_sql: true- Location:
authdb.sqlite(project root) - Size: Typically 1-10 MB
- Backup: Simple file copy
- Portability: Single file deployment
CREATE TABLE users (
id BIGINT PRIMARY KEY AUTOINCREMENT,
username VARCHAR(50) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
first_name VARCHAR(100),
last_name VARCHAR(100),
profile_image_url VARCHAR(255),
email_verified BOOLEAN DEFAULT FALSE,
enabled BOOLEAN DEFAULT TRUE,
account_non_locked BOOLEAN DEFAULT TRUE,
account_non_expired BOOLEAN DEFAULT TRUE,
credentials_non_expired BOOLEAN DEFAULT TRUE,
email_verified_at TIMESTAMP,
last_login_at TIMESTAMP,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL,
mfa_enabled BOOLEAN DEFAULT FALSE,
mfa_secret VARCHAR(255)
);CREATE TABLE refresh_tokens (
id BIGINT PRIMARY KEY AUTOINCREMENT,
token VARCHAR(255) UNIQUE NOT NULL,
user_id BIGINT NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL,
revoked_at TIMESTAMP,
revoked_by VARCHAR(100),
user_agent VARCHAR(500),
ip_address VARCHAR(45),
FOREIGN KEY (user_id) REFERENCES users(id)
);CREATE TABLE email_verification_tokens (
id BIGINT PRIMARY KEY AUTOINCREMENT,
token VARCHAR(255) UNIQUE NOT NULL,
user_id BIGINT NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL,
confirmed_at TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);CREATE TABLE password_reset_tokens (
id BIGINT PRIMARY KEY AUTOINCREMENT,
token VARCHAR(255) UNIQUE NOT NULL,
user_id BIGINT NOT NULL,
expires_at TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL,
used_at TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);CREATE TABLE user_mfa_backup_codes (
user_id BIGINT NOT NULL,
backup_code VARCHAR(255) NOT NULL,
PRIMARY KEY (user_id, backup_code),
FOREIGN KEY (user_id) REFERENCES users(id)
);CREATE TABLE user_oauth_providers (
user_id BIGINT NOT NULL,
provider VARCHAR(100) NOT NULL,
PRIMARY KEY (user_id, provider),
FOREIGN KEY (user_id) REFERENCES users(id)
);CREATE TABLE user_roles (
user_id BIGINT NOT NULL,
role VARCHAR(100) NOT NULL,
PRIMARY KEY (user_id, role),
FOREIGN KEY (user_id) REFERENCES users(id)
);-- User lookups
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_username ON users(username);
-- Token lookups
CREATE INDEX idx_refresh_tokens_token ON refresh_tokens(token);
CREATE INDEX idx_refresh_tokens_user_id ON refresh_tokens(user_id);
CREATE INDEX idx_email_verification_tokens_token ON email_verification_tokens(token);
CREATE INDEX idx_password_reset_tokens_token ON password_reset_tokens(token);
-- Expiration cleanup
CREATE INDEX idx_refresh_tokens_expires_at ON refresh_tokens(expires_at);
CREATE INDEX idx_email_verification_tokens_expires_at ON email_verification_tokens(expires_at);
CREATE INDEX idx_password_reset_tokens_expires_at ON password_reset_tokens(expires_at);User (1) ββ (N) RefreshToken
User (1) ββ (N) EmailVerificationToken
User (1) ββ (N) PasswordResetToken
User (1) ββ (N) MfaBackupCodes
User (1) ββ (N) OAuthProviders
User (1) ββ (N) Roles
sqlite3 authdb.sqlite.tables.schema users
.schema refresh_tokens
.schema email_verification_tokens
.schema password_reset_tokens-- View all users
SELECT id, username, email, email_verified, created_at FROM users;
-- View active refresh tokens
SELECT * FROM refresh_tokens WHERE revoked_at IS NULL;
-- View expired tokens
SELECT * FROM refresh_tokens WHERE expires_at < datetime('now');
-- Count users by verification status
SELECT email_verified, COUNT(*) FROM users GROUP BY email_verified;-- Remove expired refresh tokens
DELETE FROM refresh_tokens WHERE expires_at < datetime('now');
-- Remove expired email verification tokens
DELETE FROM email_verification_tokens WHERE expires_at < datetime('now');
-- Remove expired password reset tokens
DELETE FROM password_reset_tokens WHERE expires_at < datetime('now');-- Remove revoked refresh tokens
DELETE FROM refresh_tokens WHERE revoked_at IS NOT NULL;- INTEGER: User IDs, timestamps
- TEXT: Usernames, emails, tokens, URLs
- BOOLEAN: Flags (stored as INTEGER 0/1)
- BLOB: Binary data (if needed)
- @Id: Primary key with auto-increment
- @Column: Explicit column definitions
- @Enumerated: Enum mappings
- @ElementCollection: Collection mappings
- @Temporal: Date/time mappings
- Passwords: BCrypt hashed (not encrypted)
- Tokens: UUID-based, cryptographically secure
- Database: File-level encryption (optional)
- Backup: Encrypted backups recommended
- File Permissions: Restrict database file access
- Application Level: JPA/Hibernate security
- Connection Pooling: HikariCP configuration
- Audit Logging: Track database operations
- Indexes: Strategic indexing on frequently queried columns
- Batch Operations: Use batch inserts/updates
- Connection Pooling: Configure HikariCP settings
- Query Caching: JPA query result caching
- VACUUM: Reclaim unused space
- ANALYZE: Update query planner statistics
- REINDEX: Rebuild indexes
- Backup: Regular database backups
- Schema Validation: Verify schema consistency
- Data Migration: Export/import data if needed
- Index Creation: Ensure all indexes are created
- Performance Testing: Validate query performance
- Schema Changes: Track in version control
- Migration Scripts: Automated schema updates
- Rollback Plan: Ability to revert changes
- Testing: Validate migrations in test environment
#!/bin/bash
# Backup database
cp authdb.sqlite authdb.sqlite.backup.$(date +%Y%m%d_%H%M%S)#!/bin/bash
# Restore database
cp authdb.sqlite.backup.20240101_120000 authdb.sqlite#!/bin/bash
# Clean expired tokens
sqlite3 authdb.sqlite << EOF
DELETE FROM refresh_tokens WHERE expires_at < datetime('now');
DELETE FROM email_verification_tokens WHERE expires_at < datetime('now');
DELETE FROM password_reset_tokens WHERE expires_at < datetime('now');
VACUUM;
EOF- File Permissions: Ensure read/write access
- Disk Space: Monitor available space
- Concurrent Access: SQLite3 limitations
- Performance: Index optimization needed
- File Size: Monitor database growth
- Query Performance: Track slow queries
- Connection Pool: Monitor connection usage
- Error Logs: Review database errors