2009-07-03 29 views
3

的答案,previous question顯示的Nexus實現custom authentication helper稱爲「NxBASIC」。實現自定義的Python認證處理

如何開始在python中實現一個處理程序?


更新:

實現按Alex的建議的處理看起來是正確的做法,但沒有試圖提取從AUTHREQ方案和境界。 爲AUTHREQ返回的值是:

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

AbstractBasicAuthHandler.rx.search(AUTHREQ)僅返回單個元組:

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

所以方案中,境界= mo.groups()失敗。從我有限的正則表達式知識看來,AbstractBasicAuthHandler的標準正則表達式應該與方案和領域相匹配,但似乎沒有。

的正則表達式是:

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

更新2: 從AbstractBasicAuthHandler的檢查,默認處理是要做到:

scheme, quote, realm = mo.groups() 

更改爲這個工程。我現在只需要在正確的領域設置密碼。謝謝Alex!

+1

可惜我不能解密給定的Java源代碼足夠的把握如何不同於基本身份驗證 - 我也許能幫助Python的一部分,如果有人解釋這種「NxBasic」認證(從基本的區別,特別是!)。 – 2009-07-03 22:03:36

+0

與HttpBasicHelper比較表明HttpNxBasicHelper是直副本。唯一的區別似乎是ChallengeScheme的名稱和描述。 – 2009-07-04 13:21:37

回答

1

如上所述,如果名稱和描述是這個「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 

正如你可以看到檢查,我只是改變了兩個字符串從‘基礎’到‘NxBasic’ (和小寫的等價物)從urrlib2.py(在http基本認證處理程序類的抽象基本認證處理程序超類中)中找到。

嘗試使用這個版本 - 如果它仍然沒有工作,至少讓它成爲你的代碼可以幫助你添加打印/記錄語句,斷點等,以便更好地瞭解發生了什麼打破,以及如何。祝你好運! (對不起,我不能進一步幫助,但我沒有任何Nexus試驗)。