-
Notifications
You must be signed in to change notification settings - Fork 0
/
userAuth.py
42 lines (37 loc) · 1.19 KB
/
userAuth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import hashlib
# User database (example)
users = {
'john': {
'password': hashlib.sha256('password123'.encode()).hexdigest(),
'roles': ['user'],
},
'admin': {
'password': hashlib.sha256('admin123'.encode()).hexdigest(),
'roles': ['user', 'admin'],
}
}
# Function to authenticate user
def authenticate(username, password):
if username in users:
hashed_password = hashlib.sha256(password.encode()).hexdigest()
if hashed_password == users[username]['password']:
return True
return False
# Function to authorize user roles
def authorize(username, role):
if username in users and role in users[username]['roles']:
return True
return False
# Example usage
username = input("Enter your username: ")
password = input("Enter your password: ")
if authenticate(username, password):
print("Authentication successful!")
required_role = input("Enter the role you want to access: ")
if authorize(username, required_role):
print("Authorization granted!")
# Perform authorized actions here
else:
print("You don't have the required authorization.")
else:
print("Authentication failed!")