-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbasic_auth.py
More file actions
96 lines (89 loc) · 3.41 KB
/
basic_auth.py
File metadata and controls
96 lines (89 loc) · 3.41 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
Definition of class BasicAuth
"""
import base64
from .auth import Auth
from typing import TypeVar
from models.user import User
class BasicAuth(Auth):
""" Implement Basic Authorization protocol methods
"""
def extract_base64_authorization_header(self,
authorization_header: str) -> str:
"""
Extracts the Base64 part of the Authorization header for a Basic
Authorization
"""
if authorization_header is None:
return None
if not isinstance(authorization_header, str):
return None
if not authorization_header.startswith("Basic "):
return None
token = authorization_header.split(" ")[-1]
return token
def decode_base64_authorization_header(self,
base64_authorization_header:
str) -> str:
"""
Decode a Base64-encoded string
"""
if base64_authorization_header is None:
return None
if not isinstance(base64_authorization_header, str):
return None
try:
decoded = base64_authorization_header.encode('utf-8')
decoded = base64.b64decode(decoded)
return decoded.decode('utf-8')
except Exception:
return None
def extract_user_credentials(self,
decoded_base64_authorization_header:
str) -> (str, str):
"""
Returns user email and password from Base64 decoded value
"""
if decoded_base64_authorization_header is None:
return (None, None)
if not isinstance(decoded_base64_authorization_header, str):
return (None, None)
if ':' not in decoded_base64_authorization_header:
return (None, None)
email = decoded_base64_authorization_header.split(":")[0]
password = decoded_base64_authorization_header[len(email) + 1:]
return (email, password)
def user_object_from_credentials(self, user_email: str,
user_pwd: str) -> TypeVar('User'):
"""
Return a User instance based on email and password
"""
if user_email is None or not isinstance(user_email, str):
return None
if user_pwd is None or not isinstance(user_pwd, str):
return None
try:
users = User.search({"email": user_email})
if not users or users == []:
return None
for u in users:
if u.is_valid_password(user_pwd):
return u
return None
except Exception:
return None
def current_user(self, request=None) -> TypeVar('User'):
"""
Returns a User instance based on a received request
"""
Auth_header = self.authorization_header(request)
if Auth_header is not None:
token = self.extract_base64_authorization_header(Auth_header)
if token is not None:
decoded = self.decode_base64_authorization_header(token)
if decoded is not None:
email, pword = self.extract_user_credentials(decoded)
if email is not None:
return self.user_object_from_credentials(email, pword)
return