1
votes

I am trying to create a back end using Flask-JWT for a website, so I am at the authentication part.

I have started with that link :

https://pythonhosted.org/Flask-JWT/

with the code provided as example.

But I am trying to link that with a database that contains credential, so here is the code "cleaner" by using my database function :

(to understand, main function of Identification.login check if user/password is in database, and return information on the login (wrong user, wrong password, success) + Id of the user from database)

from flask import Flask
from flask_jwt import JWT, jwt_required, current_identity
from Identification.login import main #Function that test password within database


class User(object):
    def __init__(self, id, username, password):
        self.id = id
        self.username = username
        self.password = password


def authenticate(username, password):
    result = main(username, password)
    if result['value'] == 0:
        return User(result['id'], username, password)


def identity(payload):
    # TODO
    # user_id = payload['identity']
    # return userid_table.get(user_id, None)


app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'super-secret'

jwt = JWT(app, authenticate, identity)


@app.route('/protected')
@jwt_required()
def protected():
    return '%s' % current_identity


if __name__ == '__main__':
    app.run()

my question is about the identity function : I don't understand how I need to manage it. From the example of the site, the parameter is simple "payload" but don't get what it is exactly. The only description I have is : https://pythonhosted.org/Flask-JWT/_modules/flask_jwt.html#JWT

So my question is how I need to user identity function? and in my example, how I should adapt it?

Also, if you have a clear documentation on flask-jwt that allow to understand clearly this module, it would be a great help

Thanks

1

1 Answers

2
votes

As was pointed out in this this response, the Flask-JWT library you are looking at has not been updated since 2015. Instead, the Flask-JWT-Extended, which is found here, has been updated more recently and has better documentation.