Skip to content

Commit b7fdbf8

Browse files
committed
Add auth
1 parent 5570a59 commit b7fdbf8

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

app/controllers/project.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
from flask_restful import Resource, request
1010
from 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

1519
class ProjectMemoryUsageView(Resource):
1620

17-
# @jwt_required
21+
@jwt_required
1822
def post(self, project_id):
1923
return dict(status='success', data=dict()), 200
2024

app/helpers/authenticate.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

config/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

1213
class Development(Base):

server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from flask_cors import CORS
77
from flasgger import Swagger
88

9+
from flask_jwt_extended import JWTManager
10+
11+
912
from 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)

0 commit comments

Comments
 (0)