-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
81 lines (69 loc) · 2.19 KB
/
app.py
File metadata and controls
81 lines (69 loc) · 2.19 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
#!/usr/bin/env python3
"""
Route module for the API
"""
from os import getenv
from api.v1.views import app_views
from flask import Flask, jsonify, abort, request
from flask_cors import (CORS, cross_origin)
import os
app = Flask(__name__)
app.register_blueprint(app_views)
CORS(app, resources={r"/api/v1/*": {"origins": "*"}})
auth = None
AUTH_TYPE = os.getenv("AUTH_TYPE")
if AUTH_TYPE == "auth":
from api.v1.auth.auth import Auth
auth = Auth()
elif AUTH_TYPE == "basic_auth":
from api.v1.auth.basic_auth import BasicAuth
auth = BasicAuth()
elif AUTH_TYPE == "session_auth":
from api.v1.auth.session_auth import SessionAuth
auth = SessionAuth()
elif AUTH_TYPE == "session_exp_auth":
from api.v1.auth.session_exp_auth import SessionExpAuth
auth = SessionExpAuth()
elif AUTH_TYPE == "session_db_auth":
from api.v1.auth.session_db_auth import SessionDBAuth
auth = SessionDBAuth()
@app.before_request
def bef_req():
"""
Filter each request before it's handled by the proper route
"""
if auth is None:
pass
else:
setattr(request, "current_user", auth.current_user(request))
excluded = [
'/api/v1/status/',
'/api/v1/unauthorized/',
'/api/v1/forbidden/',
'/api/v1/auth_session/login/'
]
if auth.require_auth(request.path, excluded):
cookie = auth.session_cookie(request)
if auth.authorization_header(request) is None and cookie is None:
abort(401, description="Unauthorized")
if auth.current_user(request) is None:
abort(403, description="Forbidden")
@app.errorhandler(404)
def not_found(error) -> str:
""" Not found handler
"""
return jsonify({"error": "Not found"}), 404
@app.errorhandler(401)
def unauthorized(error) -> str:
""" Request unauthorized handler
"""
return jsonify({"error": "Unauthorized"}), 401
@app.errorhandler(403)
def forbidden(error) -> str:
""" Request unauthorized handler
"""
return jsonify({"error": "Forbidden"}), 403
if __name__ == "__main__":
host = getenv("API_HOST", "0.0.0.0")
port = getenv("API_PORT", "5000")
app.run(host=host, port=port)