The access control mechanism in pyTMBot ensures that only authorized users can access certain functionalities. This process involves several key components working together: AccessControl middleware, SessionManager, user identification, authentication, authorization, and comprehensive security monitoring with real-time admin alerts.
The SessionManager implements several advanced features:
_StateFabric
)AUTHENTICATED
: User is fully authenticatedPROCESSING
: Authentication in progressBLOCKED
: User is temporarily blockedUNAUTHENTICATED
: User needs authenticationglobal_chat_id
MAX_ATTEMPTS
)BLOCK_DURATION
)CLEANUP_INTERVAL
)mask_username()
functionmask_user_id()
functiongraph TD
UserRequest[๐ค User Request] --> AccessMiddleware[๐ก๏ธ Access Control Middleware]
AccessMiddleware --> CheckBlocked{๐ซ User Blocked?}
CheckBlocked -->|Yes| BlockResponse[โ Block Response]
CheckBlocked -->|No| CheckAllowed{โ
User Allowed?}
CheckAllowed -->|No| HandleUnauth[๐จ Handle Unauthorized]
CheckAllowed -->|Yes| SessionCheck[๐ Session Check]
HandleUnauth --> IncrementAttempts[๐ Increment Attempts]
IncrementAttempts --> CheckMaxAttempts{Max Attempts?}
CheckMaxAttempts -->|Yes| BlockUser[๐ซ Block User]
CheckMaxAttempts -->|No| NotifyAdmin[๐ข Notify Admin]
BlockUser --> NotifyAdmin
NotifyAdmin --> DenyAccess[โ Deny Access]
SessionCheck --> SessionManager[๐ Session Manager]
SessionManager --> CheckAuthState{Auth State?}
CheckAuthState -->|BLOCKED| BlockResponse
CheckAuthState -->|UNAUTHENTICATED| StartAuth[๐ Start Authentication]
CheckAuthState -->|PROCESSING| ContinueAuth[โณ Continue Authentication]
CheckAuthState -->|AUTHENTICATED| ValidateSession{Session Valid?}
ValidateSession -->|Expired| ExpireSession[โฐ Expire Session]
ValidateSession -->|Valid| HandleRequest[โ
Handle Request]
ExpireSession --> StartAuth
StartAuth --> CheckTOTP{TOTP Required?}
CheckTOTP -->|Yes| Generate2FA[๐ Generate 2FA]
CheckTOTP -->|No| SetAuthenticated[โ
Set Authenticated]
Generate2FA --> ShowQR[๐ฑ Show QR Code]
ShowQR --> SetProcessing[โณ Set Processing State]
SetProcessing --> WaitTOTP[โฑ๏ธ Wait for TOTP]
ContinueAuth --> VerifyTOTP{Verify TOTP?}
VerifyTOTP -->|Valid| ResetAttempts[๐ Reset TOTP Attempts]
VerifyTOTP -->|Invalid| IncrementTOTP[๐ Increment TOTP Attempts]
ResetAttempts --> SetAuthenticated
IncrementTOTP --> CheckTOTPMax{Max TOTP Attempts?}
CheckTOTPMax -->|Yes| BlockUserTOTP[๐ซ Block User - TOTP]
CheckTOTPMax -->|No| RetryTOTP[๐ Retry TOTP]
BlockUserTOTP --> SecurityAlert[๐จ Security Alert]
SecurityAlert --> DenyAccess
RetryTOTP --> WaitTOTP
SetAuthenticated --> SetLoginTime[โฐ Set Login Time]
SetLoginTime --> HandleRequest
HandleRequest --> LogAccess[๐ Log Access]
LogAccess --> ProcessRequest[โ๏ธ Process Request]
ProcessRequest --> Done[โ
Done]
DenyAccess --> Done
BlockResponse --> Done
%% Background Processes
CleanupThread[๐งน Cleanup Thread] --> CleanupExpired[๐๏ธ Clean Expired Sessions]
CleanupThread --> CleanupBlocked[๐๏ธ Clean Expired Blocks]
CleanupExpired --> CleanupBlocked
%% Admin Monitoring
AdminDashboard[๐ Admin Dashboard] --> SessionStats[๐ Session Statistics]
AdminDashboard --> SecurityAlerts[๐จ Security Alerts]
AdminDashboard --> AuditTrail[๐ Audit Trail]
style UserRequest fill:#e1f5fe
style AccessMiddleware fill:#f3e5f5
style SessionManager fill:#e8f5e8
style HandleRequest fill:#e8f5e8
style SecurityAlert fill:#ffebee
style BlockUser fill:#ffebee
style CleanupThread fill:#fff3e0
style AdminDashboard fill:#f1f8e9
When a user initiates a request, it passes through multiple security layers:
# Key Constants
MAX_ATTEMPTS = 3 # Maximum failed attempts
BLOCK_DURATION = 3600 # 1 hour block duration
ADMIN_NOTIFY_SUPPRESSION = 300 # 5 minutes notification suppression
Process Flow:
MAX_ATTEMPTS
class _StateFabric:
AUTHENTICATED = "authenticated" # Full access granted
PROCESSING = "processing" # Authentication in progress
BLOCKED = "blocked" # Temporarily blocked
UNAUTHENTICATED = "unauthenticated" # Needs authentication
UNAUTHENTICATED
state# Available Session Statistics
{
"total_sessions": int,
"authenticated_sessions": int,
"blocked_sessions": int,
"expired_sessions": int,
"processing_sessions": int
}
MAX_ATTEMPTS = 3 # Failed attempts before blocking
BLOCK_DURATION = 3600 # Block duration in seconds
CLEANUP_INTERVAL = 3600 # Cleanup interval in seconds
ADMIN_NOTIFY_SUPPRESSION = 300 # Admin notification suppression
cleanup_interval = 600 # Background cleanup interval
session_timeout = 10 # Session timeout in minutes
max_totp_attempts = 5 # Maximum TOTP attempts
block_duration = 10 # Block duration in minutes
This comprehensive access control system provides enterprise-grade security through multi-layered protection, intelligent monitoring, and automated threat response. The combination of AccessControl middleware and SessionManager ensures robust security while maintaining usability and performance.
The system balances security with user experience through intelligent blocking, session management, and privacy-compliant monitoring, making it suitable for production environments requiring strict access control.
For further information or to report issues, please refer to our GitHub repository or contact support.