Ident/auth 在金字塔中使用临时 URL

Ident/auth with temporary URLs in Pyramid

我正在构建一个 Pyramid 应用程序。在 "normal" 用法中,用户必须使用典型的 username/password 登录并执行大部分操作。 Pyramid 文档使剪切和粘贴变得非常容易。

但是,现在我想将有限的(在权限和时间上 -- 权限在给定日期到期)编辑能力扩展到我不想体验的人 account/password UI.我只是想通过电子邮件向他们发送我生成的 link,当他们单击 link 时,我希望他们登陆相关页面并获得识别和授权进行一些有限的更改。

所有显而易见的东西,例如生成 link、将其存储在数据库中、关联用户名和到期日期,都没有问题。它将其插入 Pyramid ident/auth 框架,我不知道该怎么做。到目前为止,我并没有真正深入理解他们的代码,我希望有人有一个代码示例,说明我想做什么,这可以让我继续不深入那个话题。

或者,如果答案是停止偷懒并阅读文档,那么,我问这个问题的代价很小。 :-)

创建一个随机数和有效期并将它们存储在数据库中。用这个数字生成一个 link 并将其发送给用户。检查单击 link 时其生成的随机数是否与数据库中的随机数匹配。手动验证 Pyramid 用户:

from pyramid.security import remember, forget

def authenticate_user(request, user)

    if not user.can_login():
        raise AuthenticationFailure('This user account cannot log in at the moment.')

    # get_session_token_from_user() is your custom function.
    # It usually returns user.id - it's the key how session backend maps sessions 
    # back to authenticated user.
    token = get_session_token_from_user(user)

    headers = remember(request, token)
    # assert headers, "Authentication backend did not give us any session headers"

    if not location:
        location = get_config_route(request, 'websauna.login_redirect')

    # Send any custom events related to user login your appplication cares of
    e = events.Login(request, user)
    request.registry.notify(e)

    # Redirect user to the post-login form location
    return HTTPFound(location=location, headers=headers)

有关使用 Slack 或 Medium 等一次性电子邮件 link 登录的具体用例,请参阅 websauna.magiclogin addon