0

I used flask-jwt in my project and I have declared in controller.py:

def _validate_user(email, password):
    """
    validates user from db
    """
    user = Users.get_user(email)
    if check_password_hash(user.password_hash, password):
        return user
    else:
        return None


def verify(email, password):
    if not (email and password):
        return False
    valid_user = _validate_user(email, password)
    if valid_user:
        return valid_user


def identity(payload):
    user_id = payload["identity"]
    return {"userid", user_id}

and then on the api's view.py module I have this

from flask_jwt import JWT, jwt_required
from ..controller import (
    verify,
    identity
    )
jwt = JWT(app, verify, identity)

I am using email as username concept have I am not asking user for his username, and instead uses user's email address as username and I have defined the table in models.py like this:

class Users(UserMixin, db.Model):
    """ Stores the user information
    """

    __tablename__ = "users"

    userid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(60), index=True, unique=True, nullable=False)
    password_hash = db.Column(db.String(255), nullable=False)
    registered_on = db.Column(db.DateTime, default=datetime.utcnow())
    authenticated = db.Column(db.Boolean, default=False)
    admin = db.Column(db.Boolean, nullable=False, default=False)

curl -H "Content-Type: application/json"  -u san@test.com:test -i -X GET http://127.0.0.1:5000/auth

but using the above I keep getting error method not allowed, if I try use POST I get error saying failed to decode <p>Failed to decode JSON object: No JSON object could be decoded</p> .

I am very confused what is going on , similar setup at office was working but I implemented from what I can remember ontop of my head.. seems I am missing the bit.

Ciasto piekarz
  • 7,853
  • 18
  • 101
  • 197
  • 1
    Not an answer to your question, but perhaps consider checking out Flask-JWT-Extended instead: http://flask-jwt-extended.readthedocs.io/en/latest/. Flask-JWT has been abandoned for quiet a while now. – vimalloc Feb 17 '18 at 16:58
  • @vimalloc what still twists my head is when the similar setup was working at office why it is not working now at personal dev project. – Ciasto piekarz Feb 17 '18 at 18:21

1 Answers1

0

Sort of a late reply, but I guess that your problem is in the curl command:

curl -H "Content-Type: application/json"  -u san@test.com:test -i -X GET http://127.0.0.1:5000/auth

The -u option is used to specify the username and password for server authentication, which uses HTTP basic auth, so that data is packed in the headers.

The JWT library expects the username and password as JSON, so:

  • You have to send a POST request
  • You have to add the username and password in the JSON body

    curl --header "Content-Type: application/json" \
         --request POST \
         --data '{"username":"san@test.com","password":"test"}' \
         http://127.0.0.1:5000/auth
    
el.atomo
  • 5,200
  • 3
  • 30
  • 28