Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VOA-17 Add python code to the examples
- Loading branch information
Jan Krause
committed
May 28, 2015
1 parent
82af14e
commit 2795dc5
Showing
5 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Vitadock OAuth dance Python Client Implementation | ||
============= | ||
|
||
Source: https://github.com/gkotsis/vitadock-oauth | ||
|
||
```python | ||
import vitadock | ||
|
||
c = vitadock.VitadockOauthClient("APP_KEY_GOES_HERE","APP_SECRET_GOES_HERE") | ||
|
||
c.getRequestTokens() | ||
|
||
print "request token:", c.request_token | ||
print "request token secret:", c.request_token_secret | ||
|
||
print "GOTO: " + c.authorization_url + "?oauth_token=" + c.request_token | ||
|
||
verifier = raw_input("What is the verifier? (see callback url)") | ||
|
||
c.getAccessTokens(verifier) | ||
|
||
print "access token:", c.access_token | ||
print "access token secret:", c.access_token_secret | ||
``` | ||
|
||
|
||
Requirements | ||
============ | ||
|
||
* Python 2.6+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Vitadock OAuth dance Library | ||
------------------ | ||
""" | ||
|
||
from .api import VitadockOauthClient | ||
|
||
# Meta. | ||
|
||
__title__ = 'Vitadock OAuth' | ||
__author__ = 'George Gkotsis' | ||
__author_email__ = 'gkotsis@gmail.com' | ||
|
||
__version__ = '0.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import requests | ||
import json | ||
import hashlib | ||
import hmac | ||
from urlparse import parse_qs | ||
from binascii import b2a_base64 | ||
import urllib | ||
|
||
from vitadock.utils import generate_timestamp, generate_nonce | ||
|
||
|
||
class VitadockOauthClient(object): | ||
API_ENDPOINT = "https://cloud.vitadock.com" | ||
|
||
request_token_url = "%s/auth/unauthorizedaccesses" % API_ENDPOINT | ||
access_token_url = "%s/auth/accesses/verify" % API_ENDPOINT | ||
authorization_url = "%s/desiredaccessrights/request" % API_ENDPOINT | ||
|
||
def __init__(self, app_key, app_secret, request_token=None,request_token_secret=None): | ||
self.app_key = app_key | ||
self.app_secret = app_secret | ||
self.request_token = request_token | ||
self.request_token_secret = request_token_secret | ||
return | ||
# return self.app_key | ||
|
||
def getRequestTokens(self): | ||
h = hashlib.sha256 | ||
nonce = generate_nonce() | ||
|
||
data = {} | ||
data['oauth_consumer_key'] = self.app_key | ||
data['oauth_signature_method'] = "HMAC-SHA256" | ||
data['oauth_timestamp'] = str(generate_timestamp()) | ||
data['oauth_nonce'] = nonce | ||
data['oauth_version'] = "1.0" | ||
|
||
base_string = [] | ||
for k in sorted(data.keys()): | ||
base_string.append(k + urllib.quote("=",'') +urllib.quote(data[k],'')) | ||
|
||
base_string = "POST&" + urllib.quote(self.request_token_url,'') + "&" + urllib.quote("&",'').join(base_string) | ||
sign = hmac.new(self.app_secret+"&", base_string, h) | ||
sign = b2a_base64(sign.digest()).strip().decode('utf-8') | ||
|
||
data2 = [] | ||
for k in sorted(data.keys()): | ||
data2.append(k + "=\"" +data[k] + "\"") | ||
|
||
data2.append('oauth_signature="' + urllib.quote(sign,'') + '"') | ||
|
||
data2 = "OAuth " + ",".join(data2) | ||
data3 = {} | ||
data3['Authorization'] = data2 | ||
|
||
|
||
rs = requests.post(self.request_token_url, headers=data3) | ||
rs = parse_qs(rs.text) | ||
|
||
request_token = rs['oauth_token'][0] | ||
request_token_secret = rs['oauth_token_secret'][0] | ||
self.request_token = request_token | ||
self.request_token_secret = request_token_secret | ||
return request_token, request_token_secret | ||
|
||
def getAccessTokens(self, verifier): | ||
if self.request_token is None: | ||
return "no request_token" | ||
if self.request_token_secret is None: | ||
return "no request_token_secret" | ||
h = hashlib.sha256 | ||
nonce = generate_nonce() | ||
|
||
data = {} | ||
data['oauth_consumer_key'] = self.app_key | ||
data['oauth_signature_method'] = "HMAC-SHA256" | ||
data['oauth_timestamp'] = str(generate_timestamp()) | ||
data['oauth_nonce'] = nonce | ||
data['oauth_version'] = "1.0" | ||
data['oauth_token'] = self.request_token | ||
data['oauth_verifier'] = verifier | ||
|
||
base_string = [] | ||
for k in sorted(data.keys()): | ||
base_string.append(k + urllib.quote("=",'') +urllib.quote(data[k],'')) | ||
|
||
base_string = "POST&" + urllib.quote(self.access_token_url,'') + "&" + urllib.quote("&",'').join(base_string) | ||
new_key = self.app_secret + "&" + self.request_token_secret | ||
sign = hmac.new(new_key, base_string, h) | ||
sign = b2a_base64(sign.digest()).strip().decode('utf-8') | ||
|
||
data2 = [] | ||
for k in sorted(data.keys()): | ||
data2.append(k + "=\"" +data[k] + "\"") | ||
|
||
data2.append('oauth_signature="' + urllib.quote(sign,'') + '"') | ||
|
||
data2 = "OAuth " + ",".join(data2) | ||
data3 = {} | ||
data3['Authorization'] = data2 | ||
|
||
|
||
rs = requests.post(self.access_token_url, headers=data3) | ||
|
||
rs = parse_qs(rs.text) | ||
access_token = rs['oauth_token'][0] | ||
access_token_secret = rs['oauth_token_secret'][0] | ||
|
||
self.access_token = access_token | ||
self.access_token_secret = access_token_secret | ||
|
||
return access_token, access_token_secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def generate_timestamp(): | ||
import time | ||
"""Get seconds since epoch (UTC). | ||
Per `section 3.2.1`_ of the MAC Access Authentication spec. | ||
.. _`section 3.2.1`: http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1 | ||
""" | ||
return unicode(int(time.time())*1000) | ||
|
||
def generate_nonce(): | ||
import random | ||
"""Generate parseudorandom nonce that is unlikely to repeat. | ||
Per `section 3.2.1`_ of the MAC Access Authentication spec. | ||
A random 64-bit number is appended to the epoch timestamp for both | ||
randomness and to decrease the likelihood of collisions. | ||
.. _`section 3.2.1`: http://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1 | ||
""" | ||
return unicode(unicode(random.getrandbits(74)) + generate_timestamp()) |