Skip to content

Commit

Permalink
Fix the bug where signature does not handle params of GET request
Browse files Browse the repository at this point in the history
  • Loading branch information
pslestang committed Sep 9, 2013
1 parent 162da86 commit 9abc212
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
###v0.1.1###
*Release date: 2013-09-09*

* [Fix] Corectly handle parameters of GET request in signature generation

###v0.1.0###
*Release date: 2013-09-02*

Expand Down
53 changes: 35 additions & 18 deletions OCAPy/OCAPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import hashlib
import time
import urllib
import json

import requests
from requests.auth import AuthBase
Expand All @@ -40,29 +41,36 @@ class OVHAuth(AuthBase):

def __init__(self, consumer_key=None, app_secret=None, app_key=None,
base_url=None, time_path='auth/time', url_path='/',
request_type='GET'):
self.consumer_key=consumer_key
self.app_key=app_key
self.app_secret=app_secret
self.base_url=base_url
self.url_path=url_path
self.request_type=request_type
self.time_path=time_path
self.time_url= '%s/%s' % (str(self.base_url).rstrip('/'),
str(self.time_path).lstrip('/'))
self.url='%s/%s' % (str(self.base_url).rstrip('/'),
str(self.url_path).lstrip('/'))
request_type='GET', params=None):
self.consumer_key = consumer_key
self.app_key = app_key
self.app_secret = app_secret
self.base_url = base_url
self.url_path = url_path
self.params = params
self.request_type = request_type
self.time_path = time_path
self.time_url = '%s/%s' % (str(self.base_url).rstrip('/'),
str(self.time_path).lstrip('/'))
self.url = '%s/%s' % (str(self.base_url).rstrip('/'),
str(self.url_path).lstrip('/'))
if self.params is not None:
self.url += '?%s' % self.params

# Compute the request signature
# Refer to http://www.ovh.com/fr/g934.premiers-pas-avec-l-api
def signature(self, timestamp=None):

if timestamp is None:
timestamp=self.now()
timestamp = self.now()

sha1=hashlib.sha1()
sha1.update('+'.join([self.app_secret, self.consumer_key,
str(self.request_type).upper(), self.url, "", timestamp]))
sha1.update('+'.join([self.app_secret,
self.consumer_key,
str(self.request_type).upper(),
self.url,
"",
timestamp]))
return '$1$'+sha1.hexdigest()

# time is needed to sign the requests
Expand Down Expand Up @@ -129,11 +137,19 @@ def __getattr__(self, name):
# fail (return code != requests.codes.ok )
def _request(self, type='GET', kwargs=None):

url='%s/%s' %(self.api.base_url.rstrip('/'), self.path.lstrip('/'))
url = '%s/%s' %(self.api.base_url.rstrip('/'), self.path.lstrip('/'))
# method: get/post/put/delete
method=str(type).lower()

logging.debug("%s %s" % (method, url))
params = None
if 'params' in kwargs:
params = urllib.urlencode(kwargs['params'])

full_url = url
if params is not None:
full_url += '?%s' % (params)

logging.debug("%s %s" % (method, full_url ))
logging.debug("path is %s" % self.path)

# call requests
Expand All @@ -144,7 +160,8 @@ def _request(self, type='GET', kwargs=None):
app_key=self.api.app_key,
app_secret=self.api.app_secret,
base_url=self.api.base_url,
request_type=method
request_type=method,
params = params
),
**kwargs)

Expand Down
2 changes: 1 addition & 1 deletion OCAPy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

__author__ = 'Pierre-Samuel Le Stang'
__email__ = '[email protected]'
__version__ = '0.1.0'
__version__ = '0.1.1'

from OCAPy import OCAPy

0 comments on commit 9abc212

Please sign in to comment.