From 32f48d7605612d95436116d86d66c68b581e0cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veljko=20Tekelerovi=C4=87?= Date: Sat, 29 Feb 2020 21:16:17 +0100 Subject: [PATCH] Login method coding Added basic password check Initial response decoding Custom secret_key implementation --- auth-module.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/auth-module.py b/auth-module.py index ce08873..866a918 100644 --- a/auth-module.py +++ b/auth-module.py @@ -1,9 +1,13 @@ -from flask import Flask +from flask import Flask, jsonify, request, make_response +import jwt +import datetime # initialize main Flask object if __name__ == '__main__': app = Flask(__name__) +app.config['SECRET_KEY'] = 'some_secret_key' + # ROUTES DEFINITION: @app.route('/unprotected') def unprotected(): @@ -15,7 +19,18 @@ def protected(): @app.route('/login') def login(): - return 'You are now logged in' + auth = request.authorization + if auth and auth.password == 'password': + token_expiration = str(datetime.datetime.utcnow() + datetime.timedelta(minutes=30)) + token = jwt.encode( + { + 'user': auth.username, + 'expiration': token_expiration + }, app.config['SECRET_KEY'], algorithm='HS256') + + return jsonify({'token': token.decode('UTF-8')}) + + return make_response('Could not verify!', 401, {'WWW-Authenticate': 'Basic realm="Login required"'}) # start the server if __name__ == '__main__':