Nova Coder's authentication and authorization system provides comprehensive user authentication with PBKDF2 password hashing, role-based access control (RBAC), session management with expiration, multi-factor authentication support, and user management.
Overview
The authentication and authorization system is implemented in novacoder/security/auth.py and provides:
- User authentication with secure password hashing
- Role-based access control (RBAC) with predefined roles
- Session management with expiration and cleanup
- Multi-factor authentication (MFA) support
- User management with CRUD operations
Components
AuthenticationManager
Manages user authentication and user management operations.
from novacoder.security.auth import AuthenticationManager
auth_manager = AuthenticationManager()
Methods:
create_user(username, email, password)- Create a new userauthenticate(username, password)- Authenticate a userenable_mfa(user_id)- Enable multi-factor authenticationverify_mfa(user_id, code)- Verify MFA codechange_password(user_id, old_password, new_password)- Change user password
AuthorizationManager
Manages role-based access control and permission checking.
from novacoder.security.auth import AuthorizationManager
authz_manager = AuthorizationManager()
Methods:
has_permission(user, permission)- Check if user has permissiongrant_permission(user_id, permission)- Grant permission to userrevoke_permission(user_id, permission)- Revoke permission from userassign_role(user_id, role)- Assign role to userremove_role(user_id, role)- Remove role from user
SessionManager
Manages user sessions with expiration and cleanup.
from novacoder.security.auth import SessionManager
session_manager = SessionManager()
Methods:
create_session(user_id, duration_seconds)- Create a new sessionvalidate_session(session_id)- Validate a sessioninvalidate_session(session_id)- Invalidate a sessioncleanup_expired_sessions()- Clean up expired sessions
User Management
Creating Users
from novacoder.security.auth import AuthenticationManager
auth_manager = AuthenticationManager()
user = auth_manager.create_user("username", "email@example.com", "password")
print(f"User created: {user.username}")
Authenticating Users
from novacoder.security.auth import AuthenticationManager
auth_manager = AuthenticationManager()
authenticated = auth_manager.authenticate("username", "password")
if authenticated:
print("Authentication successful")
else:
print("Authentication failed")
Changing Passwords
from novacoder.security.auth import AuthenticationManager
auth_manager = AuthenticationManager()
success = auth_manager.hash_password("user_id", "old_password", "new_password")
Role-Based Access Control
Predefined Roles
The system includes predefined roles with default permissions:
- ADMIN: Full administrative access
- DEVELOPER: Development access
- READER: Read-only access
- USER: Basic user access
Assigning Roles
from novacoder.security.auth import AuthorizationManager, Role
authz_manager = AuthorizationManager()
authz_manager.assign_role("user_id", Role.ADMIN)
Checking Permissions
from novacoder.security.auth import AuthorizationManager
authz_manager = AuthorizationManager()
has_permission = authz_manager.has_permission(user, "read_files")
if has_permission:
print("User has permission")
Granting Custom Permissions
from novacoder.security.auth import AuthorizationManager
authz_manager = AuthorizationManager()
authz_manager.grant_permission("user_id", "custom_permission")
Session Management
Creating Sessions
from novacoder.security.auth import SessionManager
session_manager = SessionManager()
session_id = session_manager.create_session("user_id", duration_seconds=3600)
print(f"Session created: {session_id}")
Validating Sessions
from novacoder.security.auth import SessionManager
session_manager = SessionManager()
is_valid = session_manager.validate_session("session_id")
if is_valid:
print("Session is valid")
Invalidating Sessions
from novacoder.security.auth import SessionManager
session_manager = SessionManager()
session_manager.validate_session("session_id")
Multi-Factor Authentication
Enabling MFA
from novacoder.security.auth import AuthenticationManager
auth_manager = AuthenticationManager()
auth_manager.enable_mfa("user_id")
Verifying MFA Codes
from novacoder.security.auth import AuthenticationManager
auth_manager = AuthenticationManager()
is_valid = auth_manager.verify_mfa("user_id", "123456")
if is_valid:
print("MFA code verified")
Security Features
Password Hashing
The system uses PBKDF2 with SHA-256 for secure password hashing:
- Salt generation using cryptographically secure random
- Configurable iteration count (default: 100,000)
- Secure password comparison using constant-time comparison
Session Security
- Session IDs generated using cryptographically secure random
- Configurable session expiration
- Automatic cleanup of expired sessions
- Session invalidation on logout
MFA Security
- Time-based one-time passwords (TOTP)
- Configurable code expiration
- Secure code verification
- Backup codes for recovery
API Token Validation
The web interface includes client-side API token validation to ensure token security:
- Token Format Validation: Tokens must be 16-256 characters
- Character Validation: Only alphanumeric characters, hyphens, and underscores allowed
- Required Field: Token cannot be empty
- No Hardcoded Tokens: Removed hardcoded default tokens for security
- No Auto-Login: Removed automatic login functionality to require user consent
// Client-side token validation
function validateToken(token){
if(!token||!token.trim()){
return {valid:false,error:'Token is required'};
}
const trimmed=token.trim();
if(trimmed.length<16){
return {valid:false,error:'Token must be at least 16 characters'};
}
if(trimmed.length>256){
return {valid:false,error:'Token must not exceed 256 characters'};
}
if(!/^[a-zA-Z0-9\-_]+$/.test(trimmed)){
return {valid:false,error:'Token can only contain letters, numbers, hyphens, and underscores'};
}
return {valid:true};
}
Best Practices
- Strong Passwords: Enforce strong password policies (minimum length, complexity)
- Session Expiration: Use appropriate session expiration times
- MFA: Enable MFA for sensitive operations
- Least Privilege: Grant only necessary permissions
- Regular Audits: Regularly audit user permissions and roles
- Password Rotation: Implement password rotation policies
- Secure Storage: Never store passwords in plain text
Integration
The authentication and authorization system is integrated with:
- SecurityService: Uses authentication for security operations
- API Endpoints: Authentication required for sensitive operations
- TUI: Login page for user authentication
- Audit Logging: All authentication events logged
Related Documentation
- Security
- Audit Logging
- Permissions
- Security Policies