-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
42 lines (34 loc) · 1.37 KB
/
auth.py
File metadata and controls
42 lines (34 loc) · 1.37 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
from flask import Flask, request, jsonify
import jwt, os, json
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
# load credential
SECRET_KEY = os.getenv('SECRET_KEY')
USERS = os.getenv('USERS')
ALGORITHM = os.getenv('ALGORITHM')
@app.route('/auth/login', methods=['POST'])
def login() -> dict:
"""Endpoint for user authentication."""
username = request.json.get('username')
password = request.json.get('password')
USERS_DB = json.loads(USERS)
if username in USERS_DB and USERS_DB[username]==password:
access_token = generate_access_token(username)
return jsonify({'access_token': access_token}), 200
return jsonify({'message': 'Invalid username and password'}), 401
def generate_access_token(username:str) -> str:
"""Helper function to generate a jwt token."""
payload = {'username': username}
token = jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
return token
@app.route('/auth/validate/<access_token>', methods=["GET"])
def validate_access_token(access_token:str) -> bool:
"""Function to validate access token"""
try:
jwt.decode(access_token, key=SECRET_KEY, algorithms=[ALGORITHM])
return jsonify({'token_valid': True}), 200
except jwt.exceptions.InvalidTokenError:
return jsonify({'token_valid': False}), 401
if __name__ == '__main__':
app.run(debug=True)