金字塔中的多种认证策略

否定

我收到了将HTTP身份验证(BasicAuthAuthenticationPolicy添加到已经实现AuthTktAuthenticationPolicy的Pyramid应用程序中的任务...

基本上,我需要创建一个RESTful API来验证用户身份(我可以为此使用BasicAuthAuthenticationPolicy吗?)。

有没有一种方法可以检查用户是使用Web界面还是使用api-检查要使用的身份验证策略?

我还没有遇到在一个金字塔应用程序中涵盖两个不同身份验证策略的文档(如果可能的话)。

PS:
我遇到了一个博客系列,开始展示如何使用金字塔框架创建RESTful API ... Blogger报告说,系列文章中有6篇文章,但是我只设法找到了其中两篇文章:建设有金字塔一个RESTful API -设置建设一个RESTful API与金字塔-资源和穿越我/很期待他的上一篇文章:使用Pyramid构建RESTful API-身份验证和ACL,但看来他并不会完成本系列。

回顾我的问题:

  1. 我可以使用BasicAuthAuthenticationPolicy构建RESTful api来验证用户身份吗?
  2. 有没有一种方法可以检查用户是使用Web界面还是使用API​​-检查要使用的身份验证策略?

任何帮助,将不胜感激。

否定

所以我所做的是我合并了金字塔BasicAuthAuthenticationPolicyCallbackAuthenticationPolicy最后我得到了这个

我已经修改了callback使用redis会话方法

要使用此类(HTTPAthentication),您可以做类似的事情(这是我为用例实现它的示例):

def _get_userid(details, request):
    userre = re.compile("""^([a-zA-Z1-9\.]+):([a-zA-Z1-9\.:-]+)$""", re.I)
    userpass = base64.b64decode(details[1])
    res = userre.search(userpass)
    if res:
        return res.group()[0]

def authcheck(username, password, request):
    user = Users.by_username(username, enabled=True)
    host = request.registry.settings.get("authentication.host", "127.0.0.1")
    maxattempts = request.registry.settings.get("authentication.maxattempts",5)
    base = _get_userid(request.authorization, request)
    if request.redis.exists("pimssess:%s" % base64.b64encode(request.remote_addr+":"+base)):
        store = pickle.loads(request.redis.get("pimssess:%s" % base64.b64encode(request.remote_addr+":"+base)))
        if store.get("auth.attempts").get(request.remote_addr):
            if store["auth.attempts"][request.remote_addr] >= maxattempts:
                raise HTTPMethodNotAllowed(body="You have been locked out for 5 minutes")

    if user and user.agent and not user.is_http_auth:
        raise HTTPMethodNotAllowed(body="You are not permitted http access")
    if user and user.agent and user.host != host:
        raise HTTPMethodNotAllowed(body="Your host is not permitted http access")
    if user and user.agent and not user.validate_password(password):
        time.sleep(1.5)
        raise HTTPForbidden(body="Failed login, Incorrect password")
    return getGroups(username)

getGroups函数重新运行groups附加到用户的列表,即['admin', 'reporting']

我遵循以下示例:BasicAuthAuthenticationPolicy(滚动到底部)

对于Web界面登录(CallbackAuthentication),您将创建一个登录界面,并创建视图以容纳该模板(检查密码和用户名是否匹配等)。

哦,我差点忘了...在您的项目中__init__.py,当您在中调用时AuthPolicydef main(...)我做了:

authentication = AuthPolicy(secret='@#^&*$!DSYUIDSA8321789DS',
                        hashalg='sha512', check=authcheck, debug=True)
authorization = ACLAuthorizationPolicy()

config = Configurator(settings=settings, root_factory=RootFactory,
                  authentication_policy=authentication,
                  authorization_policy=authorization)

我希望这可以帮助某人。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章