-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauth.py
72 lines (67 loc) · 1.99 KB
/
auth.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
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
#!/usr/bin/env python3
"""
Definition of class Auth
"""
import os
from flask import request
from typing import (
List,
TypeVar
)
class Auth:
"""
Manages the API authentication
"""
def require_auth(self, path: str, excluded_paths: List[str]) -> bool:
"""
Determines whether a given path requires authentication or not
Args:
- path(str): Url path to be checked
- excluded_paths(List of str): List of paths that do not require
authentication
Return:
- True if path is not in excluded_paths, else False
"""
if path is None:
return True
elif excluded_paths is None or excluded_paths == []:
return True
elif path in excluded_paths:
return False
else:
for i in excluded_paths:
if i.startswith(path):
return False
if path.startswith(i):
return False
if i[-1] == "*":
if path.startswith(i[:-1]):
return False
return True
def authorization_header(self, request=None) -> str:
"""
Returns the authorization header from a request object
"""
if request is None:
return None
header = request.headers.get('Authorization')
if header is None:
return None
return header
def current_user(self, request=None) -> TypeVar('User'):
"""
Returns a User instance from information from a request object
"""
return None
def session_cookie(self, request=None):
"""
Returns a cookie from a request
Args:
request : request object
Return:
value of _my_session_id cookie from request object
"""
if request is None:
return None
session_name = os.getenv('SESSION_NAME')
return request.cookies.get(session_name)