Parent: [55929e] (diff)

Child: [94b175] (diff)

Download this file

auth.py    365 lines (310 with data), 12.7 kB

  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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import logging
import os
import urllib
import hmac
import hashlib
from datetime import timedelta, datetime
from hashlib import sha256
import iso8601
import pymongo
from pylons import c, g, request
from ming import schema as S
from ming.orm.ormsession import ThreadLocalORMSession
from ming.orm import session, state, MappedClass
from ming.orm import FieldProperty, RelationProperty, ForeignIdProperty
from allura.lib import helpers as h
from allura.lib import plugin
from .session import ProjectSession
from .session import main_doc_session, main_orm_session
from .session import project_doc_session, project_orm_session
log = logging.getLogger(__name__)
def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
Returns a bytestring version of 's', encoded as specified in 'encoding'.
If strings_only is True, don't convert (some) non-string-like objects.
This function was borrowed from Django
"""
if strings_only and isinstance(s, (types.NoneType, int)):
return s
elif not isinstance(s, basestring):
try:
return str(s)
except UnicodeEncodeError:
if isinstance(s, Exception):
# An Exception subclass containing non-ASCII data that doesn't
# know how to print itself properly. We shouldn't raise a
# further exception.
return ' '.join([smart_str(arg, encoding, strings_only,
errors) for arg in s])
return unicode(s).encode(encoding, errors)
elif isinstance(s, unicode):
r = s.encode(encoding, errors)
return r
elif s and encoding != 'utf-8':
return s.decode('utf-8', errors).encode(encoding, errors)
else:
return s
def generate_smart_str(params):
for (key, value) in params:
yield smart_str(key), smart_str(value)
def urlencode(params):
"""
A version of Python's urllib.urlencode() function that can operate on
unicode strings. The parameters are first case to UTF-8 encoded strings and
then encoded as per normal.
"""
return urllib.urlencode([i for i in generate_smart_str(params)])
class ApiToken(MappedClass):
class __mongometa__:
name='api_token'
session = main_orm_session
_id = FieldProperty(S.ObjectId)
user_id = ForeignIdProperty('User')
api_key = FieldProperty(str, if_missing=lambda:h.nonce(20))
secret_key = FieldProperty(str, if_missing=h.cryptographic_nonce)
user = RelationProperty('User')
def authenticate_request(self, path, params):
try:
# Validate timestamp
timestamp = iso8601.parse_date(params['api_timestamp'])
timestamp_utc = timestamp.replace(tzinfo=None) - timestamp.utcoffset()
if abs(datetime.utcnow() - timestamp_utc) > timedelta(minutes=10):
return False
# Validate signature
api_signature = params['api_signature']
params = sorted((k,v) for k,v in params.iteritems() if k != 'api_signature')
string_to_sign = path + '?' + urlencode(params)
digest = hmac.new(self.secret_key, string_to_sign, hashlib.sha256)
return digest.hexdigest() == api_signature
except KeyError:
return False
def sign_request(self, path, params):
if hasattr(params, 'items'): params = params.items()
has_api_key = has_api_timestamp = has_api_signature = False
for k,v in params:
if k == 'api_key': has_api_key = True
if k == 'api_timestamp': has_api_timestamp = True
if k == 'api_signature': has_api_signature = True
if not has_api_key: params.append(('api_key', self.api_key))
if not has_api_timestamp:
params.append(('api_timestamp', datetime.utcnow().isoformat()))
if not has_api_signature:
string_to_sign = path + '?' + urlencode(sorted(params))
digest = hmac.new(self.secret_key, string_to_sign, hashlib.sha256)
params.append(('api_signature', digest.hexdigest()))
return params
class EmailAddress(MappedClass):
class __mongometa__:
name='email_address'
session = main_orm_session
_id = FieldProperty(str)
claimed_by_user_id=FieldProperty(S.ObjectId, if_missing=None)
confirmed = FieldProperty(bool)
nonce = FieldProperty(str)
def claimed_by_user(self):
return User.query.get(_id=self.claimed_by_user_id)
@classmethod
def upsert(cls, addr):
addr = cls.canonical(addr)
result = cls.query.get(_id=addr)
if not result:
result = cls(_id=addr)
return result
@classmethod
def canonical(cls, addr):
user, domain = addr.split('@')
return '%s@%s' % (user, domain.lower())
def send_verification_link(self):
self.nonce = sha256(os.urandom(10)).hexdigest()
log.info('Sending verification link to %s', self._id)
text = '''
To verify the email address %s belongs to the user %s,
please visit the following URL:
%s
''' % (self._id, self.claimed_by_user().username, g.url('/auth/verify_addr', a=self.nonce))
log.info('Verification email:\n%s', text)
g.publish('audit', 'forgemail.send_email', {
'destinations':[self._id],
'from':self._id,
'reply_to':'',
'message_id':'',
'subject':'Email address verification',
'text':text})
class OpenId(MappedClass):
class __mongometa__:
name='openid'
session = main_orm_session
_id = FieldProperty(str)
claimed_by_user_id=FieldProperty(S.ObjectId, if_missing=None)
display_identifier=FieldProperty(str)
@classmethod
def upsert(cls, url, display_identifier):
result = cls.query.get(_id=url)
if not result:
result = cls(
_id=url,
display_identifier=display_identifier)
return result
def claimed_by_user(self):
if self.claimed_by_user_id:
result = User.query.get(_id=self.claimed_by_user_id)
else: # pragma no cover
result = User.register(
dict(username=None, password=None,
display_name=self.display_identifier,
open_ids=[self._id]),
make_project=False)
self.claimed_by_user_id = result._id
return result
class User(MappedClass):
SALT_LEN=8
class __mongometa__:
name='user'
session = main_orm_session
unique_indexes = [ 'username' ]
_id=FieldProperty(S.ObjectId)
sfx_userid=FieldProperty(S.Deprecated)
username=FieldProperty(str)
display_name=FieldProperty(str)
open_ids=FieldProperty([str])
email_addresses=FieldProperty([str])
password=FieldProperty(str)
projects=FieldProperty([S.ObjectId])
preferences=FieldProperty(dict(
results_per_page=int,
email_address=str,
email_format=str))
tool_preferences=FieldProperty({str:{str:None}}) # full mount point: prefs dict
tool_data = FieldProperty({str:{str:None}}) # entry point: prefs dict
def url(self):
return '/u/' + self.username.replace('_', '-') + '/'
@classmethod
def by_email_address(cls, addr):
ea = EmailAddress.query.get(_id=addr)
if ea is None: return None
return ea.claimed_by_user()
@classmethod
def by_username(cls, name):
return plugin.AuthenticationProvider.get(request).by_username(name)
def get_tool_data(self, tool, key, default=None):
return self.tool_data.get(tool, {}).get(key, None)
def set_tool_data(self, tool, **kw):
d = self.tool_data.setdefault(tool, {})
d.update(kw)
state(self).soil()
def address_object(self, addr):
return EmailAddress.query.get(_id=addr, claimed_by_user_id=self._id)
def openid_object(self, oid):
return OpenId.query.get(_id=oid, claimed_by_user_id=self._id)
def claim_openid(self, oid_url):
oid_obj = OpenId.upsert(oid_url, self.display_name)
oid_obj.claimed_by_user_id = self._id
if oid_url in self.open_ids: return
self.open_ids.append(oid_url)
def claim_address(self, email_address):
addr = EmailAddress.canonical(email_address)
email_addr = EmailAddress.upsert(addr)
email_addr.claimed_by_user_id = self._id
if addr in self.email_addresses: return
self.email_addresses.append(addr)
@classmethod
def register(cls, doc, make_project=True):
from allura import model as M
result = plugin.AuthenticationProvider.get(request).register_user(doc)
if result and make_project:
n = M.Neighborhood.query.get(name='Users')
n.register_project('u/' + result.username, user=result, user_project=True)
return result
def private_project(self):
from .project import Project
return Project.query.get(shortname='u/%s' % self.username, deleted=False)
@property
def script_name(self):
return '/u/' + self.username + '/'
def my_projects(self):
from .project import Project
for p in self.projects:
project = Project.query.get(_id=p, deleted=False)
if project and self._id in [role.user_id for role in project.roles] and not project.shortname.startswith('u/'):
yield project
def role_iter(self):
anon_role = ProjectRole.query.get(name='*anonymous')
auth_role = ProjectRole.query.get(name='*authenticated')
if anon_role:
yield anon_role
if self._id and auth_role:
yield auth_role
if self._id:
pr = self.project_role()
for role in pr.role_iter():
yield role
def project_role(self, project=None):
if project is None: project = c.project
with h.push_config(c, project=project, user=self):
if self._id is None:
return ProjectRole.query.get(name='*anonymous')
pr = ProjectRole.query.get(user_id=self._id)
if pr: return pr
try:
obj = ProjectRole(user_id=self._id)
session(obj).insert_now(obj, state(obj))
self.projects.append(c.project._id)
return obj
except pymongo.errors.DuplicateKeyError:
session(obj).expunge(obj)
return ProjectRole.query.get(user_id=self._id)
def set_password(self, new_password):
return plugin.AuthenticationProvider.get(request).set_password(
self, self.password, new_password)
@classmethod
def anonymous(cls):
return User.query.get(_id=None)
class ProjectRole(MappedClass):
class __mongometa__:
session = project_orm_session
name='user'
unique_indexes = [ ('name', 'user_id') ]
_id = FieldProperty(S.ObjectId)
name = FieldProperty(str)
user_id = FieldProperty(S.ObjectId, if_missing=None) # if role is a user
roles = FieldProperty([S.ObjectId])
def display(self):
if self.name: return self.name
if self.user_id:
u = self.user
if u.username: uname = u.username
elif u.display_name: uname = u.display_name
else: uname = u._id
return '*user-%s' % uname
return '**unknown name role: %s' % self._id # pragma no cover
@classmethod
def upsert(cls, **kw):
try:
obj = cls(**kw)
session(obj).insert_now(obj, state(obj))
except pymongo.errors.DuplicateKeyError:
session(obj).expunge(obj)
obj = cls.query.get(**kw)
return obj
@property
def special(self):
if self.name:
return '*' == self.name[0]
if self.user_id:
return True
return False # pragma no cover
@property
def user(self):
return User.query.get(_id=self.user_id)
def users_with_role(self):
return [pr.user for pr in ProjectRole.query.find({'roles':self._id}).all() if pr.user_id]
def role_iter(self, visited=None):
if visited is None: visited = set()
if self._id not in visited:
yield self
visited.add(self._id)
for rid in self.roles:
pr = ProjectRole.query.get(_id=rid)
if pr is None: continue
for rr in pr.role_iter(visited):
yield rr