문제

a에 대한 답 이전 질문 넥서스가 구현된다는 것을 보여 주었다 맞춤 인증 도우미 "nxbasic"이라고합니다.

파이썬에서 핸들러를 구현하려면 어떻게해야합니까?


업데이트:

Alex의 제안에 따라 핸들러를 구현하는 것은 올바른 접근 방식 인 것처럼 보이지만 Authreq에서 체계와 영역을 추출하려는 시도는 실패합니다. Authreq의 반환 된 값은 다음과 같습니다.

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

AbstractBasicauthhandler.rx.search (authreq)는 단일 튜플 만 반환합니다.

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

따라서 체계, realm = mo.groups ()가 실패합니다. 제한된 Regex 지식에서 AbstractBasicauthhandler의 표준 Regex는 체계와 영역과 일치해야하지만 그렇지 않은 것 같습니다.

regex는 다음과 같습니다.

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

업데이트 2 : AbstractBasicauthhandler의 검사에서 기본 처리는 다음과 같습니다.

scheme, quote, realm = mo.groups()

이 작품으로 변경. 이제 올바른 영역에 비해 암호를 설정해야합니다. 감사합니다 알렉스!

도움이 되었습니까?

해결책

설명 된 바와 같이, 이름과 설명 이이 "nxbasic"과 좋은 오래된 "기본"사이의 유일한 차이점이라면, 당신은 기본적으로 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

검사에서 볼 수 있듯이 두 문자열이 "기본"에서 "nxbasic"(및 소문자 등가)로 Urrlib2.py (HTTP Basic Auth Handler Class의 추상 기본 인증 핸들러 슈퍼 클래스)에서 두 문자열을 변경했습니다. .

이 버전을 사용해보십시오. 그리고 여전히 작동하지 않는 경우 적어도 코드가되면 인쇄/로깅 진술, 중단 점 등을 추가하여 무엇을 깨뜨리는지를 더 잘 이해할 수 있습니다. 행운을 빌어 요! (더 이상 도울 수 없지만 실험 할 넥서스가 없습니다).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top