Im attempting to validate user credentials stored in a sqlite db via sqlalchemy.
The relevant code is as follows:
from flask import Flask
from flask.ext.httpauth import HTTPBasicAuth
from models import User, Base
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///users.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
app = Flask(__name__)
auth = HTTPBasicAuth()
@auth.get_password
def get_password(username):
return session.query(User.password).first()[0]
@app.route('/')
@auth.login_required
def welcome():
return 'Hello, %s' %auth.username()
if __name__ == '__main__':
app.run(host='localhost', port=8080, debug=True)
However, i get 'None' back even though there is a matching record in the DB.
When I attempt to query the DB directly from my models.py, I get the expected results. My models.py code is as follows:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class User(Base):
__tablename__ = 'userdb'
id = Column(Integer, primary_key=True, nullable=False)
username = Column(String(64), nullable=False)
password = Column(String(64), nullable=False)
email = Column(String(255), nullable=True)
engine = create_engine('sqlite:///users.db')
Base.metadata.create_all(engine)
DBSession = sessionmaker(bind=engine)
session = DBSession()
username = 'x'
match = session.query(User).filter(User.username == username).all()[0]
print match.password
Aucun commentaire:
Enregistrer un commentaire