Skip to content

Commit 802833e

Browse files
authored
Merge pull request #1 from crane-cloud/ft-auth
Add auth for the monitoring application
2 parents 5570a59 + 647b744 commit 802833e

5 files changed

Lines changed: 56 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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 dict(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 dict(message = "Bad Authorization header. Expected value 'Bearer <JWT>'") , 422
26+
27+
payload = jwt.decode(token, os.getenv('JWT_Token'), algorithms= ['HS256'])
28+
29+
30+
except jwt.exceptions.DecodeError:
31+
return dict(message = "Access token is not valid or key") , 401
32+
33+
return fn(*args, **kwargs)
34+
return wrapper
35+
36+
37+
def admin_required(fn):
38+
@wraps(fn)
39+
@jwt_required
40+
def wrapper(*args, **kwargs):
41+
return fn(*args, **kwargs)
42+
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):

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ blinker==1.7.0
44
certifi==2024.2.2
55
charset-normalizer==3.3.2
66
click==8.1.7
7+
colorama==0.4.6
78
flasgger==0.9.7.1
89
Flask==3.0.3
910
Flask-Cors==4.0.0
11+
Flask-JWT-Extended==4.6.0
1012
Flask-RESTful==0.3.10
1113
idna==3.6
1214
itsdangerous==2.1.2
@@ -17,6 +19,7 @@ MarkupSafe==2.1.5
1719
mistune==3.0.2
1820
packaging==24.0
1921
prometheus-http-client==1.0.0
22+
PyJWT==2.8.0
2023
python-dotenv==1.0.1
2124
pytz==2024.1
2225
PyYAML==6.0.1

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)