2014-10-27 39 views
0

run.py:eve U.R.R.A.不HMAC工作

class HMACAuth(HMACAuth): 
    def check_auth(self, userid, hmac_hash, headers, data, allowed_roles, resource, method): 
     accounts = app.data.driver.db['accounts'] 
     user = accounts.find_one({'username': userid}) 
     if user and '_id' in user: 
      secret_key = user['secret_key'] 
      self.set_request_auth_value(user['_id']) 

     # in this implementation we only hash request data, ignoring the headers. 
     hm = hmac.new(bytes(secret_key, encoding='utf-8'), data, sha1).digest() 

     return user and base64.b64encode(hm).decode() == hmac_hash 

settings.py:

vms = { 
    'additional_lookup': { 
     'url': 'regex("[\w]+")', 
     'field': 'name', 
    }, 
    'cache_control': '', 
    'cache_expires': 0, 
    'public_methods': [], 
    'public_item_methods': [], 
    'resource_methods': ['GET', 'POST'], 
    'item_methods': ['GET','PATCH','DELETE'], 
    'auth_field': 'user_id', 
    'schema': vm_schema, 
} 

我的問題是,每個用戶都接收所有的虛擬機信息時,他/她發送GET請求到localhost:5000/vms
由於TokenAuth認證沒有發生。
我在想什麼?

PS:對Python的前夜0.5-dev的3.3.5

回答

0

既然一切工作正常使用基於令牌的認證,並且因爲沒有什麼真正不同的兩種方法之間預計自定義類本身,我將調查圍繞其行爲。

我會從檢查文檔是否實際存儲在正確的user_id值(可能是使用mongo shell)開始。如果沒有,請確保您正在檢查的文檔已保存,並且您的自定義HMAC類處於活動狀態。添加一個斷點並跟蹤你的代碼,這樣簡單的東西。希望這可以幫助

+0

這很奇怪!創建的用戶的'_id'是'544ff79fe555c028869380f7',而創建的vms的'_id'是'544ff79fe555c028869380f8'和'544ff79fe555c028869380f9'。這怎麼可能? :O 如果我編寫'print(user ['_ id'])'它可以,但它存儲了一個不同的值:/ – polslinux 2014-10-28 20:12:16

+0

問題是字段'user_id'不是用HMACAuth類創建的。我不明白爲什麼我會得到這種奇怪的行爲... – polslinux 2014-10-28 21:17:32

+0

我檢查與'self.get_request_auth_value()'和值是正確的:(所以爲什麼它不存儲在數據庫? - 。 – polslinux 2014-10-28 21:59:45