|
| 1 | +#!/usr/bin/python2.4 |
| 2 | +# |
| 3 | +# Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | + |
| 5 | +"""Utilities for OAuth. |
| 6 | +
|
| 7 | +Utilities for making it easier to work with OAuth. |
| 8 | +""" |
| 9 | + |
| 10 | +__author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 11 | + |
| 12 | +import copy |
| 13 | +import urllib |
| 14 | +import oauth2 as oauth |
| 15 | + |
| 16 | +try: |
| 17 | + from urlparse import parse_qs, parse_qsl |
| 18 | +except ImportError: |
| 19 | + from cgi import parse_qs, parse_qsl |
| 20 | +class MissingParameter(Exception): |
| 21 | + pass |
| 22 | + |
| 23 | +def abstract(): |
| 24 | + raise NotImplementedError("You need to override this function") |
| 25 | + |
| 26 | + |
| 27 | +class TokenStore(object): |
| 28 | + def get(user, service): |
| 29 | + """Returns an oauth.Token based on the (user, service) returning |
| 30 | + None if there is no Token for that (user, service). |
| 31 | + """ |
| 32 | + abstract() |
| 33 | + |
| 34 | + def set(user, service, token): |
| 35 | + abstract() |
| 36 | + |
| 37 | +buzz_discovery = { |
| 38 | + 'required': ['domain', 'scope'], |
| 39 | + 'request': { |
| 40 | + 'url': 'https://www.google.com/accounts/OAuthGetRequestToken', |
| 41 | + 'params': ['xoauth_displayname'] |
| 42 | + }, |
| 43 | + 'authorize': { |
| 44 | + 'url': 'https://www.google.com/buzz/api/auth/OAuthAuthorizeToken', |
| 45 | + 'params': ['iconUrl', 'oauth_token'] |
| 46 | + }, |
| 47 | + 'access': { |
| 48 | + 'url': 'https://www.google.com/accounts/OAuthGetAccessToken', |
| 49 | + 'params': [] |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | +def _oauth_uri(name, discovery, params): |
| 54 | + if name not in ['request', 'access', 'authorize']: |
| 55 | + raise KeyError(name) |
| 56 | + keys = [] |
| 57 | + keys.extend(discovery['required']) |
| 58 | + keys.extend(discovery[name]['params']) |
| 59 | + query = {} |
| 60 | + for key in keys: |
| 61 | + if key in params: |
| 62 | + query[key] = params[key] |
| 63 | + return discovery[name]['url'] + '?' + urllib.urlencode(query) |
| 64 | + |
| 65 | +class Flow3LO(object): |
| 66 | + def __init__(self, discovery, consumer_key, consumer_secret, user_agent, |
| 67 | + **kwargs): |
| 68 | + self.discovery = discovery |
| 69 | + self.consumer_key = consumer_key |
| 70 | + self.consumer_secret = consumer_secret |
| 71 | + self.user_agent = user_agent |
| 72 | + self.params = kwargs |
| 73 | + self.request_token = {} |
| 74 | + for key in discovery['required']: |
| 75 | + if key not in self.params: |
| 76 | + raise MissingParameter('Required parameter %s not supplied' % key) |
| 77 | + |
| 78 | + def step1(self, oauth_callback='oob'): |
| 79 | + """Returns a URI to redirect to the provider. |
| 80 | +
|
| 81 | + If oauth_callback is 'oob' then the next call |
| 82 | + should be to step2_pin, otherwise oauth_callback |
| 83 | + is a URI and the next call should be to |
| 84 | + step2_callback() with the query parameters |
| 85 | + received at that callback. |
| 86 | + """ |
| 87 | + consumer = oauth.Consumer(self.consumer_key, self.consumer_secret) |
| 88 | + client = oauth.Client(consumer) |
| 89 | + |
| 90 | + headers = { |
| 91 | + 'user-agent': self.user_agent, |
| 92 | + 'content-type': 'application/x-www-form-urlencoded' |
| 93 | + } |
| 94 | + body = urllib.urlencode({'oauth_callback': oauth_callback}) |
| 95 | + uri = _oauth_uri('request', self.discovery, self.params) |
| 96 | + resp, content = client.request(uri, 'POST', headers=headers, |
| 97 | + body=body) |
| 98 | + if resp['status'] != '200': |
| 99 | + print content |
| 100 | + raise Exception('Invalid response %s.' % resp['status']) |
| 101 | + |
| 102 | + self.request_token = dict(parse_qsl(content)) |
| 103 | + |
| 104 | + auth_params = copy.copy(self.params) |
| 105 | + auth_params['oauth_token'] = self.request_token['oauth_token'] |
| 106 | + |
| 107 | + uri = _oauth_uri('authorize', self.discovery, auth_params) |
| 108 | + return uri |
| 109 | + |
| 110 | + def step2_pin(self, pin): |
| 111 | + """Returns an oauth_token and oauth_token_secret in a dictionary""" |
| 112 | + |
| 113 | + token = oauth.Token(self.request_token['oauth_token'], |
| 114 | + self.request_token['oauth_token_secret']) |
| 115 | + token.set_verifier(pin) |
| 116 | + consumer = oauth.Consumer(self.consumer_key, self.consumer_secret) |
| 117 | + client = oauth.Client(consumer, token) |
| 118 | + |
| 119 | + headers = { |
| 120 | + 'user-agent': self.user_agent, |
| 121 | + 'content-type': 'application/x-www-form-urlencoded' |
| 122 | + } |
| 123 | + |
| 124 | + uri = _oauth_uri('access', self.discovery, self.params) |
| 125 | + resp, content = client.request(uri, 'POST', headers=headers) |
| 126 | + return dict(parse_qsl(content)) |
| 127 | + |
| 128 | + def step2_callback(self, query_params): |
| 129 | + """Returns an access token via oauth.Token""" |
| 130 | + pass |
0 commit comments