Junior — Middle
What methods did you use to implement identity verification and access control mechanisms in the system?
sobes.tech AI
Answer from AI
To implement user identity verification and access control in Python systems, the following methods are commonly used:
-
Authentication:
- Using login and password, verified against stored hashes (e.g., with
bcryptorhashlib). - Using tokens (JWT) to confirm identity after login.
- OAuth or other authorization protocols via third-party services.
- Using login and password, verified against stored hashes (e.g., with
-
Authorization:
- Role-based access control (RBAC), where each user is assigned a role with specific permissions.
- Checking access rights to resources before performing operations.
-
Example of simple authentication:
import hashlib
users_db = {
'user1': '5f4dcc3b5aa765d61d8327deb882cf99' # md5('password')
}
def check_password(username, password):
hashed = hashlib.md5(password.encode()).hexdigest()
return users_db.get(username) == hashed
if check_password('user1', 'password'):
print('Access granted')
else:
print('Invalid login or password')
For more complex systems, frameworks like Flask-Login, Django auth provide ready-made authentication and authorization mechanisms.