2017-09-27 58 views
0

摘要式身份驗證要啓用消化CherryPy的權威性,他們說用這樣的代碼:能夠在CherryPy的server.conf中

from cherrypy.lib import auth_digest 

USERS = {'jon': 'secret'} 

conf = { 
    '/protected/area': { 
     'tools.auth_digest.on': True, 
     'tools.auth_digest.realm': 'localhost', 
     'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(USERS), 
     'tools.auth_digest.key': 'a565c27146791cfb' 
    } 
} 

cherrypy.quickstart(myapp, '/', conf) 

而且它工作得很好。但我使用server.conf文件來存儲我的應用程序的所有配置,我想繼續使用此文件。所以,我在裏面寫新的一節:

[/protected/area] 
tools.auth_digest.on = True 
tools.auth_digest.realm = 'localhost', 
tools.auth_digest.get_ha1 = auth_digest.get_ha1_dict_plain({'jon': 'secret'}), 
tools.auth_digest.key = 'a565c27146791cfb' 

thjis我有errr後:

ValueError: ('Config error in section: \'/protected/area\', option: \'tools.auth_digest.get_ha1\', value: "auth_digest.get_ha1_dict_plain({\'jon\': \'secret\'}),". Config values must be valid Python.', 'TypeError', ("unrepr could not resolve the name 'auth_digest'",)) 

我明白其中的道理,但我不知道如何爲客戶提供「有效的Python」與server.conf中。請幫幫我。

回答

2

可以作出這樣的函數調用在您的應用程序,然後用這個功能在配置,如:

myapp/__init__.py

get_ha1 = auth_digest.get_ha1_dict_plain({'jon': 'secret'}) 

server.conf

[/protected/area] 
tools.auth_digest.on = True 
tools.auth_digest.realm = 'localhost' 
tools.auth_digest.get_ha1 = myapp.get_ha1 
tools.auth_digest.key = 'a565c27146791cfb' 

有這個問題您正在代碼中定義憑據。

可能值得一提的是,您可以使用其他功能,不僅僅是您用dict中的純文本密碼定義您的用戶的功能,您可以使用cherrypy.lib.auth_digest.get_ha1_file_htdigest中的htdigest文件或實現您自己的ha1功能,如一個該get_ha1_dict_plain回報:

def get_ha1_dict_plain(user_password_dict): 
    """Returns a get_ha1 function which obtains a plaintext password from a 
    dictionary of the form: {username : password}. 
    If you want a simple dictionary-based authentication scheme, with plaintext 
    passwords, use get_ha1_dict_plain(my_userpass_dict) as the value for the 
    get_ha1 argument to digest_auth(). 
    """ 
    def get_ha1(realm, username): 
     password = user_password_dict.get(username) 
     if password: 
      return md5_hex('%s:%s:%s' % (username, realm, password)) 
     return None 

    return get_ha1 

予實現的一個,使用此模型SQLAlchemy的(https://github.com/cyraxjoe/maki/blob/master/maki/db/models.py#L174-L189)從數據庫中獲取HA1,例如:

class User(Base): 
    __tablename__ = 'users' 

    name = Column(String(32), unique=True, nullable=False) 
    vname = Column(String(64)) 
    email = Column(String(64), nullable=False) 
    ha1 = Column(String(32), nullable=False) 
    active = Column(Boolean, server_default='True') 


    @validates('ha1') 
    def validates_ha1(self, key, passwd): 
     if self.name is None: 
      raise Exception('Set the name first') 
     pack = ':'.join([self.name, maki.constants.REALM, passwd]) 
     return hashlib.md5(pack.encode()).hexdigest() 

甲找到一個get_ha1功能(https://github.com/cyraxjoe/maki/blob/master/maki/db/utils.py#L63):

def get_user_ha1(realm, username): 
    # realm is not used the stored hash already used it. 
    user = db.ses.query(db.models.User).filter_by(name=username).scalar() 
    if user is not None: 
     return user.ha1 

的重要組成部分,是一個HA1只是「用戶:真名:密碼」的MD5哈希值,你可以實現在很多不同的地方。