2017-06-05 39 views
0

Duo網絡安全API,我想用雙核的Python客戶端(https://github.com/duosecurity/duo_client_python)工作,我相信我只是缺少與我的新手蟒蛇的眼睛的東西。我不知何故需要使用HMAC簽名來驗證我對API的請求 - 我之前沒有使用它,但似乎生成微不足道(它們甚至提供了python函數)。文件在這裏https://duo.com/docs/adminapi#authentication與Python客戶

我在茫然,如何制定此簽名進行驗證,通過我的請求,API之前。從https://duo.com/docs/adminapi#authentication

The API uses HTTP Basic Authentication to authenticate requests. Use your >Duo application’s integration key as the HTTP Username.

Generate the HTTP Password as an HMAC signature of the request. This will >be different for each request and must be re-generated each time.

它然後進入其中添加和必要的參數要正確生成HMAC簽名。我理解這部分。我的問題是如何以及何時傳遞我生成的HMAC簽名。由於Duo文檔沒有具體涉及到,我認爲這是我應該已經知道的東西[我是不是這樣的python新手]。

例如,簡單的身份驗證電話正常工作(因爲它們不需要身份驗證):

[email protected]:~# python -m duo_client.client --ikey XXXXXXXX --skey XXXXXXXX --host XXXXXXXX.duosecurity.com --method GET --path /auth/v2/check 200 OK 
{ 
"response": 
{ "time": 1496437236 } 
, 
"stat": "OK" 
}

然而,呼叫使用管理員需要進行身份驗證,因此不能用:提前

[email protected]:~# python -m duo_client.client --ikey XXXXXXXX --skey XXXXXXXX --host XXXXXXXX.duosecurity.com --method GET --path /admin/v1/users signature=XXXXXXXX 
401 Unauthorized 
{ 
    "code": 40103, 
    "message": "Invalid signature in request credentials", 
    "stat": "FAIL" 
}

謝謝任何見解!

== ==編輯

所以我想我會張貼鐸爲創建簽名,這似乎類似於在StackOverflow的問題,我發現,我認爲可能有助於發生的功能(Python, HTTPS GET with basic authentication )。從https://duo.com/docs/adminapi#authentication

 
def sign(method, host, path, params, skey, ikey): 
    """ 
    Return HTTP Basic Authentication ("Authorization" and "Date") headers. 
    method, host, path: strings from request 
    params: dict of request parameters 
    skey: secret key 
    ikey: integration key 
    """ 

    # create canonical string 
    now = email.Utils.formatdate() 
    canon = [now, method.upper(), host.lower(), path] 
    args = [] 
    for key in sorted(params.keys()): 
     val = params[key] 
     if isinstance(val, unicode): 
      val = val.encode("utf-8") 
     args.append(
      '%s=%s' % (urllib.quote(key, '~'), urllib.quote(val, '~'))) 
    canon.append('&'.join(args)) 
    canon = '\n'.join(canon) 

    # sign canonical string 
    sig = hmac.new(skey, canon, hashlib.sha1) 
    auth = '%s:%s' % (ikey, sig.hexdigest()) 

    # return headers 
    return {'Date': now, 'Authorization': 'Basic %s' % base64.b64encode(auth)} 

我使用上述功能在一個簡單的腳本打印出什麼應該劇本我將創建到服務器我們的需求內獲得通過(只是這樣我就可以想像) - 我添加功能的腳本,並通過添加以下使用打印:

 
# Printing Signature Headers ### TESTING ### 
print sign('GET', 'XXXhostXXX', '/admin/v1/users', 'XXXparamXXX', 'XXXskeyXXX', 'XXXXikeyXXXX') 

不過,我得到這個錯誤:

 
[email protected]:~# ./duo.py 
Traceback (most recent call last): 
    File "./duo.py", line 39, in 
    print sign('GET', 'XXXhostXXX', '/admin/v1/users', 'XXXparamXXX', 'XXXskeyXXX', 'XXXikeyXXX') 
    File "./duo.py", line 18, in sign 
    for key in sorted(params.keys()): 
AttributeError: 'str' object has no attribute 'keys' 

我是不是失去了一些東西?我一直在尋找什麼可能導致這個錯誤,但我想我也會在這裏問。

+0

發現了一些事情,應該幫助我前進的道路 - 即,這裏的第二個答案:https://stackoverflow.com/questions/6999565/python-https-get-with-basic-authentication –

回答

0

這是Duo的一個「問題」......錯誤的skey被使用,我的用戶沒有正確的訪問權限。