python - 实现自定义 Python 身份验证处理程序

标签 python restlet nexus httplib2

previous question 的答案表明 Nexus 实现了一个 custom authentication helper称为“NxBASIC”。

我如何开始在 python 中实现处理程序?


更新:

按照 Alex 的建议实现处理程序看起来是正确的方法,但未能尝试从 authreq 中提取方案和领域。 authreq 的返回值为:

str: NxBASIC realm="Sonatype Nexus Repository Manager API""

AbstractBasicAuthHandler.rx.search(authreq) 只返回一个元组:

tuple: ('NxBASIC', '"', 'Sonatype Nexus Repository Manager API')

所以 scheme,realm = mo.groups() 失败了。根据我有限的正则表达式知识,AbstractBasicAuthHandler 中的标准正则表达式似乎应该匹配方案和领域,但似乎不匹配。

正则表达式是:

rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'
                'realm=(["\'])(.*?)\\2', re.I)

更新 2: 从AbstractBasicAuthHandler的检查来看,默认的处理是:

scheme, quote, realm = mo.groups()

改成这个作品。我现在只需要针对正确的领域设置密码。谢谢亚历克斯!

最佳答案

如果如前所述,名称和描述是这个“NxBasic”和旧的“Basic”之间的唯一区别,那么您基本上可以从 urllib2.py 中复制-粘贴-编辑一些代码(不幸的是,它没有公开方案名称本身很容易覆盖),如下所示(参见 urllib2.py 的在线资源):

import urllib2

class HTTPNxBasicAuthHandler(urllib2.HTTPBasicAuthHandler):

    def http_error_auth_reqed(self, authreq, host, req, headers):
        # host may be an authority (without userinfo) or a URL with an
        # authority
        # XXX could be multiple headers
        authreq = headers.get(authreq, None)
        if authreq:
            mo = AbstractBasicAuthHandler.rx.search(authreq)
            if mo:
                scheme, realm = mo.groups()
                if scheme.lower() == 'nxbasic':
                    return self.retry_http_basic_auth(host, req, realm)

    def retry_http_basic_auth(self, host, req, realm):
        user, pw = self.passwd.find_user_password(realm, host)
        if pw is not None:
            raw = "%s:%s" % (user, pw)
            auth = 'NxBasic %s' % base64.b64encode(raw).strip()
            if req.headers.get(self.auth_header, None) == auth:
                return None
            req.add_header(self.auth_header, auth)
            return self.parent.open(req)
        else:
            return None

正如您通过检查所见,我刚刚将 urrlib2.py 中的两个字符串从“Basic”更改为“NxBasic”(以及小写等价物)(在 http basic auth 的抽象 basic auth 处理程序父类(super class)中)处理程序类)。

尝试使用此版本——如果它仍然无法正常工作,至少将它作为您的代码可以帮助您添加打印/日志语句、断点等,以更好地了解什么是中断以及如何中断。祝你好运! (抱歉,我无法提供更多帮助,但我周围没有任何 Nexus 可供试验)。

关于python - 实现自定义 Python 身份验证处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1080920/

相关文章:

python CGI : upload error

python - 从 CNN 层转到线性层时层尺寸不匹配

java - 可扩展的 API 服务器,带有 ReSTLet?

scala - "Repository for publishing is not specified"尽管发布成功

python - 为什么我的 tanh 激活函数表现如此糟糕?

python - 如何使用 Xpath (lxml) 从多个标签中提取文本?

java - 使用 GAE 在应用程序内处理子域

java - 使用 ReSTLet 自动处理方法签名?

scala - 如何从nexus下载sbt插件?

java - 如何在 Maven 管理的框架中修补子组件?