|
a |
|
b/osp-auth-example.py |
|
|
1 |
#!/usr/bin/python
|
|
|
2 |
|
|
|
3 |
"""
|
|
|
4 |
Example on how to use the Allura API w/ auth
|
|
|
5 |
"""
|
|
|
6 |
|
|
|
7 |
import hmac, hashlib
|
|
|
8 |
from datetime import datetime
|
|
|
9 |
import urllib
|
|
|
10 |
from urllib2 import urlopen
|
|
|
11 |
from urlparse import urlparse
|
|
|
12 |
|
|
|
13 |
class Signer(object):
|
|
|
14 |
|
|
|
15 |
def __init__(self, secret_key, api_key):
|
|
|
16 |
self.secret_key = secret_key
|
|
|
17 |
self.api_key = api_key
|
|
|
18 |
|
|
|
19 |
def __call__(self, path, params):
|
|
|
20 |
if self.api_key is None:
|
|
|
21 |
return params
|
|
|
22 |
params.append(('api_key', self.api_key))
|
|
|
23 |
params.append(('api_timestamp', datetime.utcnow().isoformat()))
|
|
|
24 |
message = path + '?' + urllib.urlencode(sorted(params))
|
|
|
25 |
digest = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()
|
|
|
26 |
params.append(('api_signature', digest))
|
|
|
27 |
return params
|
|
|
28 |
|
|
|
29 |
SECRET_KEY = ''
|
|
|
30 |
API_KEY = ''
|
|
|
31 |
URL = 'https://opensourceprojects.eu/rest/p/pristine/wp1/wp1tickets/'
|
|
|
32 |
|
|
|
33 |
def main():
|
|
|
34 |
sign = Signer(SECRET_KEY, API_KEY)
|
|
|
35 |
queryparams = sign(urlparse(URL).path, [])
|
|
|
36 |
result = urlopen(URL+'?'+urllib.urlencode(queryparams))
|
|
|
37 |
print(result.read())
|
|
|
38 |
|
|
|
39 |
main()
|