Fixed Library: Added signing to AWS requests.

This commit is contained in:
Michael Ellis 2009-09-08 17:04:44 -04:00
parent 0413dcaaa4
commit c931a6bedb
3 changed files with 125 additions and 103 deletions

View File

@ -3,6 +3,10 @@
from xml.dom import minidom, Node
import urllib
import time
import datetime
import hashlib
import base64
import hmac
class PyMazonError(Exception):
"""Holds information about an error that occured during a pymazon request"""
@ -70,20 +74,37 @@ class PyMazonBook:
class PyMazon:
"""A method of looking up book information on Amazon."""
def __init__(self, accesskey):
def __init__(self, accesskey, secretkey):
self.__key = accesskey
self.__secret = secretkey
self.__last_query_time = 0
def __form_request(self, isbn):
return 'http://webservices.amazon.com/onca/xml?' + \
'Service=AWSECommerceService' + \
'&Version=2008-08-19' + \
'&AWSAccessKeyId=' + self.__key + \
'&Operation=ItemLookup' + \
'&ResponseGroup=ItemAttributes' + \
'&IdType=ISBN' + \
'&SearchIndex=Books' + \
'&ItemId=' + isbn
content = {}
dstamp = datetime.datetime.utcfromtimestamp(time.time())
content['Timestamp'] = dstamp.strftime('%Y-%m-%dT%H:%M:%S.000Z')
content['Service'] = 'AWSECommerceService'
content['Version'] = '2008-08-19'
content['Operation'] = 'ItemLookup'
content['ResponseGroup'] = 'ItemAttributes'
content['IdType'] = 'ISBN'
content['SearchIndex'] = 'Books'
content['ItemId'] = isbn
content['AWSAccessKeyId'] = self.__key
URI_String = []
for key, value in sorted(content.items()):
URI_String.append('%s=%s' % (key, urllib.quote(value)))
req = '&'.join(URI_String)
to_sign_req = 'GET\necs.amazonaws.com\n/onca/xml\n' + req
h = hmac.new(self.__secret, to_sign_req, hashlib.sha256)
sig = base64.b64encode(h.digest())
req += '&Signature=%s' % urllib.quote(sig)
return 'http://ecs.amazonaws.com/onca/xml?' + req
def __elements_text(self, element, name):
result = []

View File

@ -21,7 +21,7 @@ def configure():
"""
Load configuration
"""
cfg_fields = [ "aws_account_key" ]
cfg_fields = [ "aws_account_key", "aws_secret_key" ]
temp_cfg = conf.read(CONFIG_FILE)
conf.check_string_fields(CONFIG_FILE, cfg_fields, temp_cfg)
cfg.update(temp_cfg)
@ -127,7 +127,7 @@ class BookAddPage(WizardPanel):
isbn = self.isbn.get_edit_text()
try:
pymazon = PyMazon(cfg["aws_account_key"])
pymazon = PyMazon(cfg["aws_account_key"], cfg["aws_secret_key"])
book = pymazon.lookup(isbn)
currents = lib.Book.select(lib.Book.q.isbn==isbn)

View File

@ -2,3 +2,4 @@
library_connect_string = "postgres://librarian:PWPWPWPWPWPWPWPWPWPW@127.0.0.1/library"
aws_account_key = "KEYKEYKEYKEYKEYKEYKY"
aws_secret_key = "KEYKEYKEYKEYKEYKEYKY"