2017-09-27 35 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哈希值,你可以实现在很多不同的地方。