-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
restauth.py
54 lines (44 loc) · 1.54 KB
/
restauth.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
from functools import wraps
from flask import request
from werkzeug.security import check_password_hash
from model.Slip import Slip
from shared import db
def _slip_check():
if request.authorization is None:
return None
username = request.authorization.username
slip = db.session.query(Slip).filter(Slip.name == username).one_or_none()
if slip is None:
return None
password = request.authorization.password
if check_password_hash(slip.pass_hash, password) is False:
return None
return slip
def slip_required(endpoint):
@wraps(endpoint)
def auth_decorator(*args, **kwargs):
slip = _slip_check()
if slip is None:
return {"error": "Authentication failed"}, 401
return endpoint(*args, **kwargs)
return auth_decorator
def admin_required(endpoint):
@wraps(endpoint)
def auth_decorator(*args, **kwargs):
slip = _slip_check()
if slip is None:
return {"error": "Authentication failed"}, 401
if slip.is_admin is False:
return {"error": "Authentication failed"}, 401
return endpoint(*args, **kwargs)
return auth_decorator
def mod_required(endpoint):
@wraps(endpoint)
def auth_decorator(*args, **kwargs):
slip = _slip_check()
if slip is None:
return {"error": "Authentication failed"}, 401
if slip.is_admin is False and slip.is_mod is False:
return {"error": "Authentication failed"}, 401
return endpoint(*args, **kwargs)
return auth_decorator