2

我无法让 Pyramid 的基本身份验证机制为我工作。我做错了吗?

为了调试,我在我的一个视图中运行了这段代码:

print '$$$1', pyramid.security.remember(request, 12)
print '$$$2', pyramid.security.unauthenticated_userid(request)
print '$$$3', pyramid.security.authenticated_userid(request)

这是我得到的输出:

$$$1 [('Set-Cookie', 'auth_tkt="45a66a6e860356b991cc8fc8acf9bf7f4d8b3d2212!userid_type:int"; Path=/'), ('Set-Cookie', 'auth_tkt="45a66a6e860356b991cc8fc8acf9bf7f4d8bt!"Path=23d;23d; Domain=127.0.0.1:6543'), ('Set-Cookie', 'auth_tkt="45a66a6e860356b991cc8fc8acf9bf7f4d8b3d2212!userid_type:int"; Path=/; Domain=.127.0.0.1:6543')]

$$$2 无

$$$3 无

我确实有 request.session 为我工作,所以我猜问题不在于 cookie。

这是我__init__在配置 Pyramid 中使用的代码:

authn_policy = AuthTktAuthenticationPolicy( 'secret', callback=lambda x:[])
engine = engine_from_config(settings, 'sqlalchemy.')
initialize_sql(engine)
my_session_factory = UnencryptedCookieSessionFactoryConfig('anothersecret')
config = Configurator(settings=settings, session_factory=my_session_factory,
                      authentication_policy=authn_policy,
        )

请帮忙!

4

2 回答 2

4

“记住”只是返回标题。您需要将这些标头设置到响应中。另请参阅添加授权文档的这一部分,特别是下面第 21 行和第 22 行中的代码示例。

于 2011-03-25T08:17:34.763 回答
1

您可能犯了与我在阅读教程时所说的相同的错误 group_finder /only/ 返回其他组。此处引用的情况并非如此:http: //plope.com/pyramid_auth_design_api_postmortem

如果您使用回调函数,它必须仅在用户无效时返回 None。本教程的示例将为不在枚举用户中的任何用户返回 none(即使您通过某种其他机制对用户进行身份验证)。在我自己的代码中,我明确地返回一个空列表 ( [] ),以防用户尚未在记住的列表/组中。这样我就有了三种访问级别:公共、经过身份验证、基于组的权限。

除了教程的示例之外,还有这组食谱条目: http ://docs.pylonsproject.org/projects/pyramid_cookbook/dev/authentication.html

于 2011-07-11T21:11:03.243 回答