/usr/lib/python2.7/dist-packages/sagenb/notebook/user_manager.py is in python-sagenb 1.0.1+ds1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 | from __future__ import absolute_import
from . import user
import crypt
import hashlib
SALT = 'aa'
class UserManager(object):
def __init__(self, accounts=False):
"""
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U == loads(dumps(U))
True
"""
self._users = {}
self._accounts = accounts
def __eq__(self, other):
"""
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U1 = SimpleUserManager()
sage: U2 = SimpleUserManager(accounts=False)
sage: U1 == U2
False
sage: U2.set_accounts(True)
sage: U1 == U2
True
sage: U1.create_default_users('password')
sage: U1 == U2
False
sage: U2.create_default_users('password')
sage: U1 == U2
True
"""
if other.__class__ is not self.__class__:
return False
if self._users != other._users:
return False
if self._accounts != other._accounts:
return False
return True
def user_list(self):
"""
Returns a sorted list of the users that have logged into the notebook.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user_list()
[_sage_, admin, guest, pub]
"""
user_list = list(self.users().itervalues())
user_list.sort(key=lambda x: str(x))
return user_list
def users(self):
"""
Returns a dictionary whose keys are the usernames and whose values are the
corresponding users.
Note that these are just the users that have logged into the notebook and are
note necessarily all of the valid users.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: list(sorted(U.users().items()))
[('_sage_', _sage_), ('admin', admin), ('guest', guest), ('pub', pub)]
"""
return self._users
def user(self, username):
"""
Returns a user object for the user username.
This first checks to see if a user with username has been seen before and is in
the users dictionary. If such a user is found, then that object is returned.
Otherwise, the underscore _user method is tried. This is the method that subclasses
should override to provide custom user functionality.
EXAMPLES::
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user('pub')
pub
TESTS::
sage: U.user('william')
Traceback (most recent call last):
...
LookupError: no user 'william'
sage: U.user('hello/')
Traceback (most recent call last):
...
ValueError: no user 'hello/'
"""
if not isinstance(username, (str, unicode)) or '/' in username:
raise ValueError("no user '%s'" % username)
if username in self.users():
return self.users()[username]
try:
return self._user(username)
except AttributeError:
pass
raise LookupError("no user '{}'".format(username))
def valid_login_names(self):
"""
Return a list of users that can log in.
"""
return [x for x in self.usernames() if not x in ['guest', '_sage_', 'pub']]
def user_exists(self, username):
"""
Returns True if and only if the user \emph{username} has signed in before.
Note that this should not be used to check to see if a username is valid since
there are UserManager backends (such as LDAP) where we could have many valid usernames, but
not all of them will have actually logged into the notebook.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user_exists('admin')
True
"""
return username in self.users()
def usernames(self):
"""
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: u = U.usernames(); u.sort(); u
['_sage_', 'admin', 'guest', 'pub']
"""
return self.users().keys()
def user_is_admin(self, username):
"""
Returns True if the user username is an admin user.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user_is_admin('admin')
True
sage: U.user_is_admin('pub')
False
"""
try:
return self.user(username).is_admin()
except KeyError:
return False
def user_is_guest(self, username):
"""
Returns True if the user username is an gues user.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user_is_guest('guest')
True
sage: U.user_is_guest('admin')
False
"""
try:
return self.user(username).is_guest()
except KeyError:
return False
def create_default_users(self, passwd, verbose=False):
"""
Creates the default users (pub, _sage_, guest, and admin) in the current
notebook.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user_list()
[_sage_, admin, guest, pub]
"""
if verbose:
print("Creating default users.")
self.add_user('pub', '', '', account_type='user', force=True)
self.add_user('_sage_', '', '', account_type='user', force=True)
self.add_user('guest', '', '', account_type='guest', force=True)
self.add_user('admin', passwd, '', account_type='admin', force=True)
def delete_user(self, username):
"""
Deletes the user username from the users dictionary.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user_list()
[_sage_, admin, guest, pub]
sage: U.delete_user('pub')
sage: U.user_list()
[_sage_, admin, guest]
"""
us = self.users()
if username in us:
del us[username]
def user_conf(self, username):
"""
Returns the configuration dictionary for the user username.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.user('admin').conf()
Configuration: {}
"""
return self.user(username).conf()
def set_accounts(self, value):
"""
Set whether or not accounts can be created for this notebook.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.get_accounts()
True
sage: U.set_accounts(False)
sage: U.get_accounts()
False
"""
if value not in [True, False]:
raise ValueError("accounts must be True or False")
self._accounts = value
def get_accounts(self):
"""
Get whether or not accounts can be created for this notebook.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('password')
sage: U.get_accounts()
True
sage: U.set_accounts(False)
sage: U.get_accounts()
False
"""
return self._accounts
def add_user(self, username, password, email, account_type="user", external_auth=None, force=False):
"""
Adds a new user to the user dictionary.
INPUT:
username -- the username
password -- the password
email -- the email address
account_type -- one of 'user', 'admin', or 'guest'
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.add_user('william', 'password', 'email@address.com', account_type='admin')
sage: U.set_accounts(True)
sage: U.add_user('william', 'password', 'email@address.com', account_type='admin')
WARNING: User 'william' already exists -- and is now being replaced.
sage: U.user('william')
william
"""
if not self.get_accounts() and not force:
raise ValueError("creating new accounts disabled.")
us = self.users()
if username in us:
print("WARNING: User '%s' already exists -- and is now being replaced." % username)
U = user.User(username, password, email, account_type, external_auth)
us[username] = U
self.set_password(username, password)
def add_user_object(self, user, force=False):
"""
Adds a new user to the user dictionary.
INPUT:
user -- a User object
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: from sagenb.notebook.user import User
sage: U = SimpleUserManager()
sage: user = User('william', 'password', 'email@address.com', account_type='admin')
sage: U.add_user_object(user)
sage: U.set_accounts(True)
sage: U.add_user_object(user)
WARNING: User 'william' already exists -- and is now being replaced.
sage: U.user('william')
william
"""
if not self.get_accounts() and not force:
raise ValueError("creating new accounts disabled.")
us = self.users()
if user.username() in us:
print("WARNING: User '%s' already exists -- and is now being replaced." % user.username())
self._users[user.username()] = user
class SimpleUserManager(UserManager):
def __init__(self, accounts=True, conf=None):
"""
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U == loads(dumps(U))
True
"""
self._passwords = {}
UserManager.__init__(self, accounts=accounts)
self._conf = {'accounts': accounts} if conf is None else conf
def copy_password(self, username, other_username):
"""
Sets the password of user to be the password of other_user.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: UM = SimpleUserManager(accounts=True)
sage: UM.create_default_users('passpass')
sage: UM.add_user('william', 'password', 'email@address.com')
sage: UM.check_password('admin','passpass')
True
sage: UM.check_password('william','password')
True
sage: UM.copy_password('william', 'admin')
sage: UM.check_password('william','passpass')
True
"""
O = self.user(other_username)
passwd = O.password()
self.set_password(username, passwd, encrypt=False)
def _user(self, username):
"""
Returns a User object with username username.
This method is called by UserManager.user if it did not find a user
with username username in the user dictionary. This method will
automatically create users for the usernames 'pub', '_sage_',
'admin', and 'guest'.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.user('guest')
guest
sage: U.user('pub')
pub
sage: U.user('admin')
admin
TESTS::
sage: U.user('william')
Traceback (most recent call last):
...
LookupError: no user 'william'
sage: U._user('william')
Traceback (most recent call last):
...
LookupError: no user 'william'
sage: U.user('hello/')
Traceback (most recent call last):
...
ValueError: no user 'hello/'
sage: U._user('hello/')
Traceback (most recent call last):
...
LookupError: no user 'hello/'
"""
if username in ['pub', '_sage_']:
self.add_user(username, '', '', account_type='user', force=True)
return self.users()[username]
elif username == 'admin':
self.add_user(username, '', '', account_type='admin', force=True)
return self.users()[username]
elif username == 'guest':
self.add_user('guest', '', '', account_type='guest', force=True)
return self.users()[username]
raise LookupError("no user '{}'".format(username))
def set_password(self, username, new_password, encrypt = True):
"""
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('passpass')
sage: U.check_password('admin','passpass')
True
sage: U.set_password('admin', 'password')
sage: U.check_password('admin','password')
True
sage: U.set_password('admin', 'test'); U.check_password('admin','test')
True
sage: U.set_password('admin', 'test', encrypt=False); U.password('admin')
'test'
"""
if encrypt:
salt = user.generate_salt()
new_password = 'sha256${0}${1}'.format(salt,
hashlib.sha256(salt + new_password).hexdigest())
self._passwords[username] = new_password
# need to make sure password in the user object is synced
# for compatibility only the user object data is stored in the 'users.pickle'
self.user(username).set_password(new_password, encrypt = False)
def passwords(self):
"""
Return a dictionary whose keys are the usernames and whose values are
the encrypted passwords associated to the user.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('passpass')
sage: list(sorted(U.passwords().items())) #random
[('_sage_', ''),
('admin', ''),
('guest', ''),
('pub', '')]
sage: len(list(sorted(U.passwords().items())))
4
"""
return dict([(user.username(), self.password(user.username())) for user in self.user_list()])
def password(self, username):
"""
Return the stored password for username. Might be encrypted.
EXAMPLES:
sage: from sagenb.notebook.user_manager import SimpleUserManager
sage: U = SimpleUserManager()
sage: U.create_default_users('passpass')
sage: U.check_password('admin','passpass')
True
"""
return self._passwords.get(username, None)
def check_password(self, username, password):
# the empty password is always false
if username == "pub" or password == '':
return False
user_password = self.password(username)
if user_password is None and not self.user(username).is_external():
print("User %s has None password" % username)
return False
if user_password.find('$') == -1:
if user_password == crypt.crypt(password, user.SALT):
self.set_password(username, password)
return True
else:
return False
else:
salt, user_password = user_password.split('$')[1:]
if hashlib.sha256(salt + password).hexdigest() == user_password:
return True
try:
return self._check_password(username, password)
except AttributeError:
return False
def get_accounts(self):
# need to use notebook's conf because those are already serialized
# fix when user_manager is serialized
return self._conf['accounts']
def set_accounts(self, value):
if value not in [True, False]:
raise ValueError("accounts must be True or False")
self._accounts = value
self._conf['accounts'] = value
class ExtAuthUserManager(SimpleUserManager):
def __init__(self, accounts=None, conf=None):
SimpleUserManager.__init__(self, accounts=accounts, conf=conf)
from .auth import LdapAuth
# keys must match to a T_BOOL option in server_config.py
# so we can turn this auth method on/off
self._auth_methods = {
'auth_ldap': LdapAuth(self._conf),
}
def _user(self, username):
"""
Check all auth methods that are enabled in the notebook's config.
If a valid username is found, a new User object will be created.
"""
for a in self._auth_methods:
if self._conf[a]:
u = self._auth_methods[a].check_user(username)
if u:
try:
email = self._auth_methods[a].get_attrib(username, 'email')
except KeyError:
email = None
self.add_user(username, password='', email=email, account_type='user', external_auth=a, force=True)
return self.users()[username]
raise LookupError("no user '{}'".format(username))
def _check_password(self, username, password):
"""
Find auth method for user 'username' and
use that auth method to check username/password combination.
"""
u = self.users()[username]
if u.is_external():
a = u.external_auth()
else:
return False
if self._conf[a]:
return self._auth_methods[a].check_password(username, password)
return False
class OpenIDUserManager(ExtAuthUserManager):
def __init__(self, accounts=True, conf=None):
"""
Creates an user_manager that supports OpenID identities
EXAMPLES:
sage: from sagenb.notebook.user_manager import OpenIDUserManager
sage: UM = OpenIDUserManager()
sage: UM.create_default_users('passpass')
sage: UM.check_password('admin','passpass')
True
"""
ExtAuthUserManager.__init__(self, accounts=accounts, conf=conf)
self._openid = {}
def load(self, datastore):
"""
Loads required data from a given datastore.
"""
self._openid = datastore.load_openid()
def save(self, datastore):
"""
Saves persistent data to a given datastore.
"""
datastore.save_openid(self._openid)
def get_username_from_openid(self, identity_url):
"""
Return the username corresponding ot a given identity_url
EXAMPLES:
sage: from sagenb.notebook.user_manager import OpenIDUserManager
sage: UM = OpenIDUserManager()
sage: UM.create_default_users('passpass')
sage: UM.create_new_openid('https://www.google.com/accounts/o8/id?id=AItdaWgzjV1HJTa552549o1csTDdfeH6_bPxF14', 'thedude')
sage: UM.get_username_from_openid('https://www.google.com/accounts/o8/id?id=AItdaWgzjV1HJTa552549o1csTDdfeH6_bPxF14')
'thedude'
"""
try:
return self._openid[identity_url]
except KeyError:
raise LookupError("no openID identity '{}'".format(identity_url))
def create_new_openid(self, identity_url, username):
"""
Create a new identity_url -- username pairing
EXAMPLES:
sage: from sagenb.notebook.user_manager import OpenIDUserManager
sage: UM = OpenIDUserManager()
sage: UM.create_default_users('passpass')
sage: UM.create_new_openid('https://www.google.com/accounts/o8/id?id=AItdaWgzjV1HJTa552549o1csTDdfeH6_bPxF14', 'thedude')
sage: UM.get_username_from_openid('https://www.google.com/accounts/o8/id?id=AItdaWgzjV1HJTa552549o1csTDdfeH6_bPxF14')
'thedude'
"""
self._openid[identity_url] = username
def get_user_from_openid(self, identity_url):
"""
Return the user object corresponding ot a given identity_url
"""
return self.user(self.get_username_from_openid(identity_url))
|