-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask-jwt-server.py
More file actions
46 lines (35 loc) · 1.32 KB
/
flask-jwt-server.py
File metadata and controls
46 lines (35 loc) · 1.32 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
# Since I was not provided with API key by recruiter ¯\_(ツ)_/¯
# the only interesting way to simulate token validation and sending request
# is to write Flask app serving as JWT token endpoint
from flask import Flask, render_template, request
from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity
from flask import jsonify
from flask_jwt_extended import JWTManager
from datetime import timedelta
ACCESS_EXPIRES = timedelta(minutes=1)
API_KEY = '00000000-0000-0000-0000-000000000000'
user = "adam.nowak"
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "12345"
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = ACCESS_EXPIRES
jwt = JWTManager(app)
@app.route('/', methods=['GET'])
def hello_world():
return render_template('index.html')
@app.route('/validate', methods=['GET'])
@jwt_required()
def validate():
current_user = get_jwt_identity()
if current_user == user:
return jsonify({"user": current_user}), 200
@app.route('/token', methods=['POST'])
def token():
headers = request.headers
auth = headers.get("X-Api-Key")
if auth == API_KEY:
access_token = create_access_token(identity=user)
return jsonify({'token': access_token, "user_id": user}), 200
else:
return jsonify({"message": "Unauthorized"}), 401
if __name__ == "__main__":
app.run()