-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Json web token/ login #5
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .private import PrivateAPI | ||
from .public import PublicAPI | ||
from .json_web_token import generate_jwt, token_required |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from flask import jsonify, request, current_app | ||
import jwt | ||
import datetime | ||
from functools import wraps | ||
|
||
def token_required(f): | ||
""" | ||
THIS function requires the use of a json web token in order to be accepted | ||
""" | ||
@wraps(f) | ||
def decorated(*args, **kwargs): | ||
token = None | ||
if 'x-access-token' in request.headers: | ||
token = request.headers['x-access-token'] | ||
if not token: | ||
jsonify({'message': 'Token is missing'}), 401 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to jsonify stuff here, just return the dict and the status code! |
||
try: | ||
data = jwt.decode(token, current_app.config['SECRET_KEY'], algorithms=['HS256']) | ||
except: | ||
return jsonify({'message':'Token is false'}), 401 | ||
return f(*args,**kwargs) | ||
return decorated | ||
|
||
|
||
def generate_jwt(username): | ||
""" | ||
Generats a Jwt given a username more could be added in the future | ||
""" | ||
token = jwt.encode({'username': username, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should rename |
||
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60)}, | ||
current_app.config['SECRET_KEY']) | ||
return jsonify({'token': token.decode('UTF-8')}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,33 @@ | ||
from flask import Blueprint | ||
from flask import Blueprint, Response, request, make_response, jsonify | ||
from director.model import User | ||
|
||
from .json_web_token import generate_jwt, token_required | ||
from director.authentication import Authenticator | ||
PublicAPI = Blueprint('public_api', __name__, url_prefix='/api/public') | ||
|
||
@PublicAPI.route('/user/<username>') | ||
def get_user(username): | ||
user = User.query.filter_by(username=username).first_or_404() | ||
return user.serialize() | ||
|
||
|
||
|
||
@PublicAPI.route('/login', methods=['POST']) | ||
def login(): | ||
""" | ||
public api login with ldap authentication and returns a JWT | ||
""" | ||
if not 'username' in request.form: | ||
return jsonify({'message': 'username must not be empty'}), 401 | ||
if not 'password' in request.form: | ||
return jsonify({'message': ' password must not be empty'}), 401 | ||
authenticator = Authenticator() | ||
user = authenticator.authenticate(request.form['username'], request.form['password']) | ||
if user == None: | ||
#if user do not exist | ||
return Response(status=401) | ||
return generate_jwt(request.form['username']), 200 | ||
|
||
@PublicAPI.route('/test') | ||
@token_required | ||
def test(): | ||
return Response(status=200) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,4 @@ wcwidth==0.1.8 | |
Werkzeug==0.16.1 | ||
wrapt==1.11.2 | ||
zipp==3.0.0 | ||
pyjwt==1.7.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the plain
Authorization: Bearer <token>
headers for this? 😄