File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99from flask_restful import Resource , request
1010from flask import current_app , render_template
1111
12+ from app .helpers .authenticate import (
13+ jwt_required
14+ )
15+
1216# Todo: figure out a way to connect to projects and get projects
1317
1418
1519class ProjectMemoryUsageView (Resource ):
1620
17- # @jwt_required
21+ @jwt_required
1822 def post (self , project_id ):
1923 return dict (status = 'success' , data = dict ()), 200
2024
Original file line number Diff line number Diff line change 1+ from flask_jwt_extended import (
2+ jwt_required as jwt ,
3+ verify_jwt_in_request
4+ )
5+ from functools import wraps
6+ import jwt
7+ from flask import request , jsonify
8+ import os
9+
10+
11+ def jwt_required (fn ):
12+ @wraps (fn )
13+ def wrapper (* args , ** kwargs ):
14+
15+ access_token = request .headers .get ('Authorization' )
16+
17+ payload : object = {}
18+
19+ if access_token is None :
20+ return jsonify ({'message' : 'Access token was not supplied' }), 401
21+
22+ try :
23+ token = access_token .split (' ' )[1 ]
24+ if (access_token .split (' ' )[0 ] != "Bearer" ):
25+ return jsonify ({'message' : "Bad Authorization header. Expected value 'Bearer <JWT>'" }) , 422
26+
27+ payload = jwt .decode (token , os .getenv ('JWT_Token' ), algorithms = ['HS256' ])
28+
29+ # kwargs['current_user'] = payload
30+
31+ except jwt .exceptions .DecodeError :
32+ return dict (message = "Access token is not valid or key" ) , 401
33+
34+ return fn (* args , ** kwargs )
35+ return wrapper
36+
37+
38+ def admin_required (fn ):
39+ @wraps (fn )
40+ @jwt_required
41+ def wrapper (* args , ** kwargs ):
42+
43+ return fn (* args , ** kwargs )
44+ return wrapper
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ class Base:
77 # main
88 SECRET_KEY = os .getenv ("FLASK_APP_SECRET" )
99 VERIFICATION_SALT = os .getenv ("FLASK_VERIFY_SALT" )
10+ PASSWORD_SALT = os .getenv ("FLASK_APP_SALT" )
1011
1112
1213class Development (Base ):
Original file line number Diff line number Diff line change 66from flask_cors import CORS
77from flasgger import Swagger
88
9+ from flask_jwt_extended import JWTManager
10+
11+
912from app .routes import api
1013
1114
@@ -39,6 +42,8 @@ def create_app(config_name):
3942
4043 Swagger (app , template_file = 'api_docs.yml' )
4144
45+ jwt = JWTManager (app )
46+
4247 # handle default 404 exceptions with a custom response
4348
4449 @app .errorhandler (404 )
You can’t perform that action at this time.
0 commit comments