2017-03-01 15 views
1

リソースの認証にbcryptを使用し、ユーザー名とパスワードを使用してmydatabaseにアカウントを格納しています。次のように私は、データベース内のハッシュとパスワードを手動で保存されています:Eve認証でbcrypt driver.dbエラーが発生しました。

私はPythonのはbashを開始し、次のコードで入力:その後、私はプリントの出力をコピーして、アカウントのテーブルに格納し

import bcrypt 
password = u'passwordtobehashed' 
password_hashed = bcrypt.hashpw(password, bcrypt.gensalt()) 
print (password_hashed) 

をPOSTリクエスト経由(認証O/W):

curl -d '{"username": "someuser", "password": "somehashedpassword", "roles_id": 1}' -H 'Content-Type: application/json' http://127.0.0.1:5000/account 

は、まあ、私はSQLAlchemyのを使用して、前夜も(バージョン:0.7.1)最新の状態です。 まあ、私は、人々は次のようにBcrypted認証を使用して、リソース、たとえば要求:

curl -u username 127.0.0.1:5000/people 

は、その後、私は自分のパスワードを入力し、私はエラー以下の取得:

File "/home/vagrant/erpghost/restapi/oldtrivial.py", line 57, in check_auth 
    accounts = app.data.driver.db['account'] 
AttributeError: 'SQLAlchemy' object has no attribute 'db' 

デシベル属性が利用できないいくつかの理由。また、Eve.app.data.driver.dbを使用しようとしましたが、フラスコからcurrent_appをインポートしようとしましたが、すべて動作しませんでした。さてここで

は私のコードです:

oldtrivial.py


from eve.auth import BasicAuth 
from eve import Eve 
from eve_sqlalchemy import SQL 
from eve_sqlalchemy.validation import ValidatorSQL 
import bcrypt 
from connection import connect 
from connection import Base 

con, meta = connect() 
Base.metadata.create_all(con) 


class BCryptAuth(BasicAuth): 
    def check_auth(self, username, password, allowed_roles, resource, method): 
     accounts = app.data.driver.db['account'] 
     account = accounts.find_one({'username': username}) 
     return account and \ 
      bcrypt.hashpw(password, account['password']) == account['password'] 

app = Eve(validator=ValidatorSQL, data=SQL, auth=BCryptAuth) 

db = app.data.driver 
Base.metadata.bind = db.engine 
db.Model = Base 
db.create_all() 


if __name__ == '__main__': 
    app.run(debug=True, use_reloader=False) 

tables.py


from sqlalchemy.orm import column_property 
from sqlalchemy import Column, Integer, String, DateTime, func, ForeignKey 
from connection import connect 
from eve.auth import BasicAuth 
from connection import Base 
from sqlalchemy.orm import relationship 

con, meta = connect() 

class CommonColumns(Base): 
    __abstract__ = True 
    _created = Column(DateTime, default=func.now()) 
    _updated = Column(DateTime, default=func.now(), onupdate=func.now()) 
    _etag = Column(String(40)) 


class People(CommonColumns): 
    __tablename__ = 'people' 
    _id = Column(Integer, primary_key=True, autoincrement=True) 
    firstname = Column(String(80)) 
    lastname = Column(String(120)) 
    fullname = column_property(firstname + " " + lastname) 


class Roles(CommonColumns): 
    __tablename__ = 'roles' 
    _id = Column(Integer, primary_key=True, autoincrement=True) 
    role = Column(String(80)) 


class Account(CommonColumns): 
    __tablename__ = 'account' 
    _id = Column(Integer, primary_key=True, autoincrement=True) 
    username = Column(String(50), nullable=False, unique=True) 
    password = Column(String(200), nullable=False) 
    roles = relationship("Roles", backref="account") 
    roles_id = Column(Integer, ForeignKey('roles._id')) 

settings.py


from eve_sqlalchemy.decorators import registerSchema 
from eve.utils import config 
from tables import People 
from tables import Account 
from tables import Roles 


registerSchema('people')(People) 
registerSchema('roles')(Roles) 
registerSchema('account')(Account) 

DOMAIN = { 
     'people': People._eve_schema['people'], 
     'roles': Roles._eve_schema['roles'], 
     'account': Account._eve_schema['account'], 
} 

DOMAIN['account'].update({ 
    'additional_lookup': { 
     'url': 'regex("[\w]+")', 
     'field': 'username' 
    }, 
    'cache_control': '', 
    'cache_expires': 0, 
    'allowed_roles': ['superuser', 'admin'], 
    'authentication': None, 
}) 

SQLALCHEMY_DATABASE_URI = 'postgresql://databaseuser:[email protected]:5432/database' 

RESOURCE_METHODS = ['GET', 'POST'] 

ITEM_METHODS = ['GET', 'DELETE', 'PATCH', 'PUT'] 

DEBUG = True 

config.ID_FIELD = config.ITEM_LOOKUP_FIELD = '_id' 

DOMAIN['people']['id_field'] = config.ID_FIELD 
DOMAIN['roles']['id_field'] = config.ID_FIELD 
DOMAIN['account']['id_field'] = config.ID_FIELD 

誰かが私を助けてくれることを願っています。これらの線に沿って

答えて

2

何か作業をする必要があります:

from flask import current_app 
from tables import Account 

# ... 

def check_auth(...): 
    session = current_app.data.driver.session 
    return session.query(Account) \ 
        .filter(Account.username == username, 
          Account.password == hashed_password) \ 
        .count() > 0 

私はあなたがhttp://python-eve.org/authentication.html#basic-authentication-with-bcryptでコードを模倣しようとしたと思いますか?これは、SQLAlchemyの代わりにMongo-DBを使用するためのものです。

関連する問題