Python CherryPy身份验证超时

Python CherryPy身份验证超时,python,cherrypy,Python,Cherrypy,我在我的CherryPy服务器中添加了摘要身份验证,我想知道用户的身份验证是根据什么条件撤销的,并且会提示他们再次输入凭据。删除Cookie不会强制提示,但使用Incognito或其他浏览器会强制提示 我的配置: { 'tools.auth_digest.on': True, 'tools.auth_digest.realm': 'localhost', 'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(USERS),

我在我的CherryPy服务器中添加了摘要身份验证,我想知道用户的身份验证是根据什么条件撤销的,并且会提示他们再次输入凭据。删除Cookie不会强制提示,但使用Incognito或其他浏览器会强制提示

我的配置:

{ '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': key,
  'tools.auth_digest.accept_charset': 'UTF-8' }

谢谢

您需要有正确的HTTP响应,以便浏览器清除用户凭据,基本上是以
401 Unauthorized
响应,以及如何使用
WWW-authenticate
头进行身份验证的挑战

这是一个使用定制CherryPy工具和
Cookie
的实现,该Cookie被用作向浏览器和后端传达意图的方式(HTTP auth是无状态的,我们必须来回执行deauth和重定向)

import cherrypy
从cherrypy.lib导入验证摘要
领域='localhost'
键='2468451368351320ASD1WDASD'
字符集='UTF-8'
@cherrypy.tools.register('before_handler')
带有“注销”处理程序()的def:
如果cherrypy.request.cookie.get('Unauthorize')不是None:
response=cherrypy.response
response.headers['WWW-Authenticate']=auth\u digest.WWW\u Authenticate(
领域=领域,
键=键,
接受字符集=字符集
)
#删除用于标记注销意图的cookie
响应。cookie['Unauthorize']=1
响应。cookie['Unauthorize']['expires']=0
raise cherrypy.HTTPError(
401,‘您无权访问该资源’)
类应用程序:
@樱桃树
@cherrypy.tools.with_logout_handler()
def索引(自):
return('欢迎{}!你想吗?'
.format(cherrypy.request.login))
@樱桃树
def注销(自我):
"""
设置cookie,为其提供索引方法的线索
从以下请求中删除用户凭据。
这将由工具“with\u logout\u handler”处理。
"""
cherrypy.response.cookie['Unauthorize']=1
raise cherrypy.HTTPRedirect(“/”)
def main():
用户={
“foo”:“bar”
}
cherrypy.quickstart(App(),config={
'/': {
'tools.auth_digest.on':True,
'tools.auth_digest.realm':领域,
'tools.auth_digest.get_ha1':auth_digest.get_ha1_dict_plain(用户),
'tools.auth_digest.key':key,
'tools.auth\u digest.accept\u charset':字符集
},
})
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

此外,我不知道后端可以为任何类型的本机HTTP身份验证配置客户端凭据上的任何超时,您当然可以基于
cherrypy.request.login
在后端管理会话,如果会话已过期,则重定向到
/logout
(如果您不想自己管理会话,可以使用cherrypy会话)。
import cherrypy
from cherrypy.lib import auth_digest


REALM = 'localhost'
KEY = '24684651368351320asd1wdasd'
CHARSET = 'UTF-8'


@cherrypy.tools.register('before_handler')
def with_logout_handler():
    if cherrypy.request.cookie.get('Unauthorize') is not None:
        response = cherrypy.response
        response.headers['WWW-Authenticate'] = auth_digest.www_authenticate(
            realm=REALM,
            key=KEY,
            accept_charset=CHARSET
        )
        # delete the cookie that was used to mark the intention to logout
        response.cookie['Unauthorize'] = 1
        response.cookie['Unauthorize']['expires'] = 0
        raise cherrypy.HTTPError(
            401, 'You are not authorized to access that resource')


class App:
    @cherrypy.expose
    @cherrypy.tools.with_logout_handler()
    def index(self):
        return ('Welcome {}! Do you want to <a href="/logout">logout</a>?'
                .format(cherrypy.request.login))

    @cherrypy.expose
    def logout(self):
        """
        Set a cookie to give it a clue to the index method to
        remove the user credentials from the following requests.

        This will be handled by the tool `with_logout_handler`.
        """
        cherrypy.response.cookie['Unauthorize'] = 1
        raise cherrypy.HTTPRedirect("/")


def main():
    users = {
        'foo': 'bar'
    }
    cherrypy.quickstart(App(), config={
        '/': {
            'tools.auth_digest.on': True,
            'tools.auth_digest.realm': REALM,
            'tools.auth_digest.get_ha1': auth_digest.get_ha1_dict_plain(users),
            'tools.auth_digest.key': KEY,
            'tools.auth_digest.accept_charset': CHARSET
        },
    })

if __name__ == '__main__':
    main()