2015-07-20 15:39:59 +00:00
|
|
|
import logging
|
2016-11-04 21:57:55 +00:00
|
|
|
import features
|
2015-07-20 15:39:59 +00:00
|
|
|
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
from data import model
|
2017-09-14 19:34:41 +00:00
|
|
|
from data.users.shared import can_create_user
|
2015-07-20 15:39:59 +00:00
|
|
|
from util.validation import generate_valid_usernames
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-10-27 19:31:32 +00:00
|
|
|
UserInformation = namedtuple('UserInformation', ['username', 'email', 'id'])
|
2015-07-20 15:39:59 +00:00
|
|
|
|
2017-05-05 17:20:20 +00:00
|
|
|
DISABLED_MESSAGE = 'User creation is disabled. Please contact your adminstrator to gain access.'
|
|
|
|
|
2015-07-20 15:39:59 +00:00
|
|
|
class FederatedUsers(object):
|
|
|
|
""" Base class for all federated users systems. """
|
|
|
|
|
2016-09-08 16:24:47 +00:00
|
|
|
def __init__(self, federated_service, requires_email):
|
2015-07-20 15:39:59 +00:00
|
|
|
self._federated_service = federated_service
|
2016-09-08 16:24:47 +00:00
|
|
|
self._requires_email = requires_email
|
2015-07-20 15:39:59 +00:00
|
|
|
|
2016-10-27 19:31:32 +00:00
|
|
|
@property
|
|
|
|
def federated_service(self):
|
|
|
|
return self._federated_service
|
|
|
|
|
2017-12-12 21:00:38 +00:00
|
|
|
@property
|
|
|
|
def supports_fresh_login(self):
|
|
|
|
return True
|
|
|
|
|
2017-06-08 17:13:22 +00:00
|
|
|
@property
|
|
|
|
def supports_encrypted_credentials(self):
|
|
|
|
return True
|
|
|
|
|
2017-11-10 21:49:32 +00:00
|
|
|
@property
|
|
|
|
def requires_distinct_cli_password(self):
|
|
|
|
# Since the federated auth provides a password which works on the CLI.
|
|
|
|
return False
|
|
|
|
|
2016-10-27 19:31:32 +00:00
|
|
|
def get_user(self, username_or_email):
|
|
|
|
""" Retrieves the user with the given username or email, returning a tuple containing
|
|
|
|
a UserInformation (if success) and the error message (on failure).
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2015-07-20 15:39:59 +00:00
|
|
|
def verify_credentials(self, username_or_email, password):
|
|
|
|
""" Verifies the given credentials against the backing federated service, returning
|
2016-10-27 19:31:32 +00:00
|
|
|
a tuple containing a UserInformation (on success) and the error message (on failure).
|
|
|
|
"""
|
2015-07-20 15:39:59 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2016-10-27 19:31:32 +00:00
|
|
|
def query_users(self, query, limit=20):
|
|
|
|
""" If implemented, get_user must be implemented as well. """
|
|
|
|
return (None, 'Not supported')
|
|
|
|
|
|
|
|
def link_user(self, username_or_email):
|
2017-02-22 02:07:48 +00:00
|
|
|
(user_info, err_msg) = self.get_user(username_or_email)
|
|
|
|
if user_info is None:
|
2016-10-27 19:31:32 +00:00
|
|
|
return (None, err_msg)
|
|
|
|
|
2017-03-21 17:06:55 +00:00
|
|
|
return self.get_and_link_federated_user_info(user_info)
|
2017-02-22 02:07:48 +00:00
|
|
|
|
2017-03-21 17:06:55 +00:00
|
|
|
def get_and_link_federated_user_info(self, user_info):
|
|
|
|
return self._get_and_link_federated_user_info(user_info.username, user_info.email)
|
2016-10-27 19:31:32 +00:00
|
|
|
|
2015-07-20 15:39:59 +00:00
|
|
|
def verify_and_link_user(self, username_or_email, password):
|
|
|
|
""" Verifies the given credentials and, if valid, creates/links a database user to the
|
|
|
|
associated federated service.
|
|
|
|
"""
|
|
|
|
(credentials, err_msg) = self.verify_credentials(username_or_email, password)
|
|
|
|
if credentials is None:
|
|
|
|
return (None, err_msg)
|
|
|
|
|
2017-03-21 17:06:55 +00:00
|
|
|
return self._get_and_link_federated_user_info(credentials.username, credentials.email)
|
2015-07-20 15:39:59 +00:00
|
|
|
|
|
|
|
def confirm_existing_user(self, username, password):
|
|
|
|
""" Confirms that the given *database* username and service password are valid for the linked
|
|
|
|
service. This method is used when the federated service's username is not known.
|
|
|
|
"""
|
|
|
|
db_user = model.user.get_user(username)
|
|
|
|
if not db_user:
|
|
|
|
return (None, 'Invalid user')
|
|
|
|
|
|
|
|
federated_login = model.user.lookup_federated_login(db_user, self._federated_service)
|
|
|
|
if not federated_login:
|
|
|
|
return (None, 'Invalid user')
|
|
|
|
|
|
|
|
(credentials, err_msg) = self.verify_credentials(federated_login.service_ident, password)
|
|
|
|
if credentials is None:
|
|
|
|
return (None, err_msg)
|
|
|
|
|
|
|
|
return (db_user, None)
|
2017-02-16 20:16:47 +00:00
|
|
|
|
2017-02-17 22:10:26 +00:00
|
|
|
def service_metadata(self):
|
|
|
|
""" Returns a dictionary of extra metadata to present to *superusers* about this auth engine.
|
|
|
|
For example, LDAP returns the base DN so we can display to the user during sync setup.
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def check_group_lookup_args(self, group_lookup_args):
|
|
|
|
""" Verifies that the given group lookup args point to a valid group. Returns a tuple consisting
|
|
|
|
of a boolean status and an error message (if any).
|
|
|
|
"""
|
|
|
|
return (False, 'Not supported')
|
|
|
|
|
2017-02-16 20:16:47 +00:00
|
|
|
def iterate_group_members(self, group_lookup_args, page_size=None, disable_pagination=False):
|
|
|
|
""" Returns an iterator over all the members of the group matching the given lookup args
|
|
|
|
dictionary. The format of the lookup args dictionary is specific to the implementation.
|
|
|
|
"""
|
|
|
|
return (None, 'Not supported')
|
|
|
|
|
2017-03-21 17:06:55 +00:00
|
|
|
def _get_and_link_federated_user_info(self, username, email):
|
2017-02-16 20:16:47 +00:00
|
|
|
db_user = model.user.verify_federated_login(self._federated_service, username)
|
|
|
|
if not db_user:
|
2017-05-05 17:20:20 +00:00
|
|
|
# We must create the user in our db. Check to see if this is allowed.
|
2017-09-14 19:34:41 +00:00
|
|
|
if not can_create_user(email):
|
2017-05-05 17:20:20 +00:00
|
|
|
return (None, DISABLED_MESSAGE)
|
|
|
|
|
2017-02-16 20:16:47 +00:00
|
|
|
valid_username = None
|
|
|
|
for valid_username in generate_valid_usernames(username):
|
|
|
|
if model.user.is_username_unique(valid_username):
|
|
|
|
break
|
|
|
|
|
|
|
|
if not valid_username:
|
|
|
|
logger.error('Unable to pick a username for user: %s', username)
|
|
|
|
return (None, 'Unable to pick a username. Please report this to your administrator.')
|
|
|
|
|
|
|
|
prompts = model.user.get_default_user_prompts(features)
|
2017-04-25 21:42:35 +00:00
|
|
|
try:
|
|
|
|
db_user = model.user.create_federated_user(valid_username, email, self._federated_service,
|
|
|
|
username,
|
|
|
|
set_password_notification=False,
|
|
|
|
email_required=self._requires_email,
|
|
|
|
prompts=prompts)
|
|
|
|
except model.InvalidEmailAddressException as iae:
|
|
|
|
return (None, iae.message)
|
|
|
|
|
2017-02-16 20:16:47 +00:00
|
|
|
else:
|
|
|
|
# Update the db attributes from the federated service.
|
2017-02-22 02:07:48 +00:00
|
|
|
if email and db_user.email != email:
|
2017-02-16 20:16:47 +00:00
|
|
|
db_user.email = email
|
|
|
|
db_user.save()
|
|
|
|
|
|
|
|
return (db_user, None)
|