-
Notifications
You must be signed in to change notification settings - Fork 0
/
openid_google.py
138 lines (112 loc) · 4.34 KB
/
openid_google.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
from openid.extension import Extension
from openid.extensions import ax
from openid.consumer.consumer import SUCCESS
oauth_ns_uri = 'http://specs.openid.net/extensions/oauth/1.0'
class OpenIDOAuthRequest(Extension):
ns_alias = 'ext2'
def __init__(self, consumer, scope, ns_uri=None):
Extension.__init__(self)
self.consumer = consumer
self.scope = scope
self.ns_uri = ns_uri or oauth_ns_uri
def getExtensionArgs(self):
return {
'consumer': self.consumer,
'scope': ' '.join(self.scope),
}
class OpenIDOAuthResponse(Extension):
ns_alias = 'ext2'
@classmethod
def fromSuccessResponse(cls, success_response):
if not success_response.status == SUCCESS:
return None
self = cls()
self.ns_url = oauth_ns_uri
args = success_response.getSignedNS(oauth_ns_uri)
if not args or 'request_token' not in args or 'scope' not in args:
return None
self.request_token = args['request_token']
self.scope = args['scope'].split()
return self
def getRequestTokenObject(self, consumer_key, consumer_secret):
"""
Return the gdata.gauth.OAuthHmacToken object suitable to be exchanged
to Auth token later on.
Requires gdata library to be installed
"""
# Create a request token object
try:
from gdata.gauth import OAuthHmacToken, AUTHORIZED_REQUEST_TOKEN
except ImportError:
raise RuntimeError('Please install gdata library')
request_token_object = OAuthHmacToken(
consumer_key = consumer_key,
consumer_secret = consumer_secret,
token = self.request_token,
token_secret = '',
auth_state = AUTHORIZED_REQUEST_TOKEN,
)
return request_token_object
class OpenIDUIExtension(Extension):
"""
Set up extra UI options
Options are set according to https://developers.google.com/accounts/docs/OpenID#Parameters
:param mode: UI mode. Can be None, 'popup' or 'x-has-session'
:param icon: Displays the favicon of the referring domain in the OpenID
approval page if set to True
"""
ns_uri = 'http://specs.openid.net/extensions/ui/1.0'
ns_alias = 'ui'
def __init__(self, mode=None, icon=False):
Extension.__init__(self)
self.mode = mode
self.icon = icon
def getExtensionArgs(self):
ret = {}
if self.mode:
ret['mode'] = self.mode
if self.icon:
ret['icon'] = 'true'
return ret
# OpenID AX constants
AX_EMAIL = 'http://axschema.org/contact/email'
AX_FIRST_NAME = 'http://axschema.org/namePerson/first'
AX_LAST_NAME = 'http://axschema.org/namePerson/last'
AX_LANGUAGE = 'http://axschema.org/pref/language'
AX_COUNTRY = 'http://axschema.org/contact/country/home'
# Shortcuts
AX_SHORTCUTS = {
AX_EMAIL: 'email',
AX_FIRST_NAME: 'firstname',
AX_LAST_NAME: 'lastname',
AX_LANGUAGE: 'language',
AX_COUNTRY: 'country'
}
def makeOpenIDAXRequest(firstname=True, lastname=True, email=True,
language=True, country=True):
""" Make the AX OpenID request according to Google specification.
By default OpenID consumer tries to receive first name, last name, email,
language and country of the user. Set corresponding function keys to False
if some of the fields are not required by your application"""
# Add Attribute Exchange request information.
ax_request = ax.FetchRequest()
for type_uri, alias in AX_SHORTCUTS.iteritems():
ax_request.add(ax.AttrInfo(type_uri, alias=alias, required=True))
return ax_request
def extractOpenIDAXData(response):
""" Extract AX fields from the OpenID response in usable plain dict format.
Keys of the dict are :const:`AX_SHORTCUTS` dict values (firstname,
lastname, etc). Values are unicode objects.
"""
# Get a AX response object if response
# information was included in the OpenID response.
ax_items = {}
if response.status == SUCCESS:
ax_response = ax.FetchResponse.fromSuccessResponse(response)
if ax_response:
for k, v in ax_response.data.iteritems():
if v:
shortcut = AX_SHORTCUTS.get(k, k)
ax_items[shortcut] = v[0]
return ax_items