import re import os import random import string import urllib import base64 import Cookie import lasso from quixote import get_request, get_response, get_session, get_session_manager, get_publisher, redirect, get_field from quixote.html import htmltext from quixote.directory import Directory from quixote.util import StaticDirectory from authentic.form import * from qommon import get_cfg, get_logger from qommon import errors from qommon import emails from qommon.admin.emails import EmailsDirectory from qommon.admin.texts import TextsDirectory from qommon import template from qommon.tokens import Token import authentic.identities as identities import authentic.misc as misc import authentic.liberty.root as liberty_root import authentic.liberty.saml2 as saml2_root import authentic.liberty.idwsf2 as idwsf2_root import authentic.admin.root as root import admin.configuration as configuration try: import cas except ImportError: cas = None import datetime import login_token class LoginError(Exception): pass class RegistrationError(Exception): pass class ForgotPassword(RuntimeError): pass class CookieSetterDirectory(Directory): _q_exports = ['', 'idpintro'] def _q_index [html] (self): template.html_top() _('This domain is not for humans, it is only used to set identity ' 'provider discovery cookie.') def idpintro(self): tok = get_request().form.get('tok') token = Token.get(tok) session = get_session_manager().get(token.session_id) request = get_request() try: intro_cookie = request.cookies['_saml_idp'] except KeyError: intro_cookie = '' intro_cookie_q = urllib.unquote(intro_cookie) splitted_cookie = [x for x in intro_cookie_q.split(' ') if x] succinct_id = base64.encodestring(token.provider_id).strip() if succinct_id in splitted_cookie: splitted_cookie.remove(succinct_id) splitted_cookie.append(succinct_id) new_cookie = urllib.quote(' '.join(splitted_cookie)) if new_cookie != intro_cookie: response = get_response() response.set_cookie('_saml_idp', new_cookie, domain = '.' + token.common_domain, path = '/', expires = Cookie._getdate(3*365*86400)) token.remove_self() return redirect(token.next_url) class RootDirectory(Directory): _q_exports = ['', 'admin', 'liberty', 'login', 'logout', 'change_password', 'register', 'forgot_password', 'update_info', 'saml', 'singleLogout', 'federations', 'login_local', 'login_ssl', 'associate_certificate', 'themes', 'disco', 'cas', 'forgot_identifier'] admin = root.RootDirectory() disco = idwsf2_root.DiscoveryServiceDirectory() saml = saml2_root.RootDirectory() liberty = liberty_root.RootDirectory() themes = template.ThemesDirectory() if cas: cas = cas.CASDirectory() def _q_traverse(self, path): if get_request().environ.get('HTTPS') and not get_request().session.ssl and get_request().session.id: get_session_manager().expire_session() get_request().session = get_session_manager().get_session() fn = os.path.join(get_publisher().app_dir, 'common_cookie') if os.path.exists(fn): # on special domain to set cookie, nothing else, let's change root get_publisher().app_dir = open(fn).read() return CookieSetterDirectory()._q_traverse(path) session = get_session() if session: get_request().user = session.get_user() else: get_request().user = None response = get_response() response.filter = {} if not hasattr(response, 'breadcrumb'): response.breadcrumb = [ ('', _('Home')) ] return Directory._q_traverse(self, path) def _q_lookup(self, component): if component in ('css','images'): return StaticDirectory(os.path.join(get_publisher().data_dir, 'web', component), follow_symlinks = True) if component == 'qo': dirname = os.path.join(get_publisher().data_dir, 'qommon') return StaticDirectory(dirname, follow_symlinks = True) if get_publisher().WEBROOT_DIR: dirname = os.path.abspath(os.path.join(get_publisher().WEBROOT_DIR, component)) if os.path.exists(dirname): return StaticDirectory(dirname, follow_symlinks = True) raise errors.TraversalError() def _q_index [html] (self): session = get_session() if not session or not session.user: return self.login() try: identities.get_store().load_identities() except identities.IdentityStoreException: return template.error_page(_('Failed to connect to identities storage.')) identities.get_store().connect(session) try: identity = identities.get_store().get_identity(session.user) except KeyError: # identity no longer available; perhaps identity store changed ? return self.logout() alternate_homepage_url = configuration.get_configuration(str('homepage')).get(str('alternate_homepage_url')) if alternate_homepage_url: return redirect(alternate_homepage_url) identities_cfg = get_cfg('identities', {}) branding_cfg = get_cfg('branding', {}) passwords_cfg = get_cfg('passwords', {}) ssl_cfg = get_cfg('ssl', {}) template.html_top(_('Account Management')) get_response().breadcrumb.append( ('', _('Account Management')) ) allow_certificate_federation = ssl_cfg.get('allow_certificate_federation', False) vars = { 'can_change_password': str(passwords_cfg.get('can_change', False)), 'creation_mode': identities_cfg.get('creation'), 'identity_label': str(identity), 'idp_sso_list': str(self.get_idp_sso_list()), 'federations_list': str(self.get_idp_federations_list(identity)), 'admin': identity.is_admin(), 'show_federations': get_cfg('idp', {}).get('defederation', False), } if allow_certificate_federation: vars['allow_certificate_federation_url'] = htmltext('https://' + get_request().environ['HTTP_HOST'] + get_request().environ['SCRIPT_NAME'] + '/associate_certificate') certificates = [ x for x in identity.accounts if isinstance(x, identities.CertificateAccount) ] certificate_list = '
' + _('Certificates federated:') + '\n
%s
' % _('A password will be mailed to you.')) if passwords_cfg.get('lost_password_behaviour', 'nothing') == 'dumb_question': dumb_questions_options = [(str(x), _(identities.dumb_questions[x])) \ for x in identities.available_dumb_questions] form.add(HtmlWidget, '%s
' % _('If you forget your password...')) form.add(SingleSelectWidget, 'dumb_question', title = _('Security question'), required=True, options = [(None, _('[Select a question]'))] + dumb_questions_options) form.add(StringWidget, 'smart_answer', title = _('Your answer'), required=True) form.add_submit("submit", _("Submit")) form.add_submit("cancel", _("Cancel")) if form.get_submit() == 'cancel': return redirect('.') if form.is_submitted() and not form.has_errors(): try: return self.register_submit(form) except RegistrationError: pass template.html_top(_('Registration')) get_response().breadcrumb.append( ('register', _('Registration')) ) vars = { 'register_form': str(form.render()) } return template.process_template( str(TextsDirectory.get_html_text('register')), vars) def register_submit(self, form): identities_cfg = get_cfg('identities', {}) pwd_cfg = configuration.get_configuration('passwords') store = identities.get_store() account = identities.PasswordAccount() has_email = 'email' in [x.key for x in identities.get_store_class().fields] if identities_cfg.get('email-as-username', False): account.username = form.get_widget('email').parse() else: account.username = form.get_widget('username').parse() if pwd_cfg.get('lost_password_behaviour') == 'dumb_question': account.dumb_question = form.get_widget('dumb_question').parse() account.smart_answer = form.get_widget('smart_answer').parse() if store.has_identity_with_username(account.username): if identities_cfg.get('email-as-username', False): form.get_widget('email').set_error(_('That address is already in use')) else: form.get_widget('username').set_error(_('That username is already in use')) raise RegistrationError() if pwd_cfg.get('can_change') and not pwd_cfg.get('generate'): password = form.get_widget('password').parse() if len(account.password) < min_pw_length: form.set_error('password', _('Password is too short. It must be at least %d characters.') % \ min_pw_length) raise RegistrationError() if max_pw_length and len(account.password) > max_pw_length: form.set_error('password', _('Password is too long. It must be at most %d characters.') % \ max_pw_length) raise RegistrationError() else: password = store.create_password(for_account=account.username) account.password = store.hash_password(password) identity = store.get_identity_class()() if not identity.id: identity.id = account.username # or sequence ? for field in identities.get_store_class().fields: if form.get_widget(field.key): setattr(identity, field.key, form.get_widget(field.key).parse()) identity.accounts = [account] self.pre_registration_callback(identity) try: store.add(identity) except identities.AlreadyExists, e: if e.args: keys = e.args[0] for key in keys: form.set_error(key, _('This value must be unique but it already exists for another user')) raise RegistrationError() if identities_cfg.get('creation') == 'moderated': identity.disabled = True else: try: self.email_password(identity, welcome = True, password = password) except errors.EmailError: store.remove(identity) get_logger().error('Error emailing password to user (%s)' % identity.email) return template.error_page( _('An error occured and your password could not be send. ' 'Is your email address correct?')) except ForgotPassword, e: message = e.args[0] get_logger().error('Registration password sending failed for %s: %s' % (account.username, message)) return template.error_page(message) if identities_cfg.get('notify-on-register', False): self.notify_registration(identity) if store.count() == 0: # first user created gets admin role identity.roles = [identities.ROLE_ADMIN] store.save(identity) get_logger().info('User created new identity (%s)' % identity) self.registration_callback(identity) if identities_cfg.get('creation') == 'self' and not pwd_cfg.get('generate'): return redirect(get_request().environ['SCRIPT_NAME'] + '/login') elif identities_cfg.get('creation') == 'moderated': return self.moderated_answer() else: return self.register_done_password_sent() def pre_registration_callback(self, identity): pass # for derivatives to override def registration_callback(self, identity): pass # for derivatives to override def moderated_answer [html] (self): template.html_top(_('Registration')) 'Registration is moderated.' def notify_registration(self, identity): identities_cfg = get_cfg('identities', {}) admins = identities.get_store().administrators() admin_emails = [x.email for x in admins if x.email] if not admin_emails: return data = { 'hostname': get_request().get_server(), 'identity': str(identity), 'email': identity.email, 'email_as_username': str(identities_cfg.get('email-as-username', False)), 'username': identity.accounts[0].username, 'service': get_session().service, } emails.custom_ezt_email('new-registration-admin-notification', data, admin_emails, fire_and_forget = True) def register_done_password_sent [html] (self): template.html_top(_('Registration Completed')) get_response().breadcrumb.append( ('registration', _('Registration Completed')) ) branding_cfg = get_cfg('branding', {}) return template.process_template( str(TextsDirectory.get_html_text('register_completed')), {}) def email_password(self, identity, welcome = False, password = None): store = identities.get_store() for account in identity.accounts: if hasattr(account, 'username'): break else: account = None if not account: raise ForgotPassword(_('Your account has no password, it \ certainly uses another kind of authentication, contact an administrator.')) if not identity.email: raise ForgotPassword(_('Your account has no email, contact an \ administrator')) identities_cfg = configuration.get_configuration('identities') pwd_cfg = configuration.get_configuration('passwords') if not password: password = account.password if password is None or identities.get_pwd_hashing_scheme() != 'clear' or \ pwd_cfg.get('generate_on_remind') or \ identities.get_hash_format(password) is not None: password = store.create_password(for_account=account.username) account.password = store.hash_password(password) store.save(identity) data = { 'hostname': get_request().get_server(), 'identity': str(identity), 'email': identity.email, 'email_as_username': str(identities_cfg.get('email-as-username')), 'username': identity.accounts[0].username, 'password': password, 'service': get_session().service, } if welcome: emails.custom_ezt_email('welcome-email', data, identity.email) else: emails.custom_ezt_email('password-email', data, identity.email) def forgot_password_message [html] (self): template.html_top(_('Lost Password')) return template.process_template( str(TextsDirectory.get_html_text('lost_password_mailed')), vars) def forgot_password_dumb_question [html] (self, form): identities_cfg = get_cfg('identities', {}) username = form.get_widget('username').parse() identity = identities.get_store().get_identity_for_username(username) if identity: for account in identity.accounts: if hasattr(account, str('username')): break else: raise 'XXX' else: account = identities.PasswordAccount() if not get_session().question_key: get_session().question_key = random.choice(identities.available_dumb_questions) account.dumb_question = get_session().question_key get_session().question_key = account.dumb_question form = Form(enctype="multipart/form-data", use_tokens = False) form.add_hidden("username", username) if not hasattr(account, str('dumb_question')): return template.error_page('No password question for this identity') if not identities.dumb_questions.has_key(account.dumb_question): return template.error_page('No password question for this identity') if identities_cfg.get('email-as-username', False): form.add(HtmlWidget, '%s: %s
' % (_('Email'), username)) else: form.add(HtmlWidget, '%s: %s
' % (_('Username'), username)) form.add(StringWidget, "answer", title = _(identities.dumb_questions[account.dumb_question]), required = True, size = 30) answer = form.get_widget('answer').parse() user_answer = account.smart_answer or '' if answer and str(answer).lower().strip() != str(user_answer).lower().strip(): return template.error_page(_('Wrong answer or inexistant user')) form.set_error('answer', _('Wrong answer')) if not get_request().form.has_key('answer'): form.set_error('answer', None) if not form.has_errors() and get_request().form.has_key('answer'): return self.forgot_password_submit(form) or \ self.forgot_password_message() form.add_submit('submit', _('Submit')) form.add_submit('cancel', _('Cancel')) vars = { 'lost_password_question_form': str(form.render()) } return template.process_template( str(TextsDirectory.get_html_text('lost_password_question')), vars) def forgot_password [html] (self): get_response().breadcrumb.append( ('forgot_password', _('Lost Password')) ) passwords_cfg = get_cfg('passwords', {}) behaviour = passwords_cfg.get('lost_password_behaviour', 'nothing') if behaviour == 'nothing': raise errors.AccessForbiddenError() form = Form(enctype="multipart/form-data", use_tokens = False) identities_cfg = get_cfg('identities', {}) if identities_cfg.get('email-as-username', False): form.add(EmailWidget, "username", title= _('Email'), size=30, required=True) else: form.add(StringWidget, "username", title=_("Username"), size=30, required=True) form.add_submit("submit", _("Submit")) form.add_submit("cancel", _("Cancel")) if form.get_submit() == 'cancel': return redirect('.') if form.is_submitted() and not form.has_errors(): if behaviour == 'dumb_question': return self.forgot_password_dumb_question(form) else: return self.forgot_password_submit(form) or \ self.forgot_password_message() else: vars = { 'lost_password_behaviour': str(behaviour), 'lost_password_form': str(form.render()), 'generate_on_remind': passwords_cfg.get('generate_on_remind', False) } template.html_top(_('Lost Password')) return template.process_template( str(TextsDirectory.get_html_text('lost_password')), vars) def forgot_password_submit(self, form): username = form.get_widget('username').parse() try: identity = identities.get_store().get_identity_for_username( username,throw=True) except identities.TooMuchAccounts: get_logger().warning('Forgot password: more than one account for identifier %r' % username) return template.error_page(htmltext(_('There is more than one accounts for the identifier %s, try to \ ask for all your accounts')) % username) if not identity: get_logger().warning('Forgot password: no account for username %r' % username) return template.error_page(_('Your identity %r is unknown.') % username) try: self.email_password(identity) except errors.EmailError: get_logger().error('lost password -> email password (%s) (failure)' % username) return template.error_page(_('An error occured and your password could not be send.')) except ForgotPassword, e: message = e.args[0] get_logger().error('lost password -> %s: %s' % (username, message)) return template.error_page(message) get_logger().info('lost password -> email password of %s to %s' % (username, identity.email)) @misc.protect_form_from_get_parameters def update_info [html] (self): identities_cfg = get_cfg('identities', {}) if identities_cfg.get('creation') != 'self': raise errors.TraversalError() session = get_session() if not session or not session.user: raise errors.AccessForbiddenError() form = Form(enctype="multipart/form-data") form.keep_referer() skip_email = identities_cfg.get('email-as-username', False) identity = identities.get_store().get_identity(session.user) for field in identities.get_store_class().fields: if field.key == 'email' and skip_email: continue field.add_to_form(form, identity = identity) and None form.add_submit("submit", _("Submit")) form.add_submit("cancel", _("Cancel")) if get_request().get_method() == 'GET': get_request().form = {} if form.get_submit() == 'cancel': return misc.redirect_to_return_url() or \ misc.redirect_to_referer(form) or \ misc.redirect_home() if form.is_submitted() and not form.has_errors(): return self.update_info_submit(form, identity) template.html_top(_('Updating Personal Information')) get_response().breadcrumb.append( ('', _('Account Management')) ) get_response().breadcrumb.append( ('update_info', _('Updating Personal Information')) ) vars = { 'info_form': str(form.render()) } return template.process_template( str(TextsDirectory.get_html_text('update_info')), vars) def update_info_submit(self, form, identity): identities_cfg = get_cfg('identities', {}) skip_email = identities_cfg.get('email-as-username', False) for field in identities.get_store_class().fields: if field.key == 'email' and skip_email: continue if form.get_widget(field.key): setattr(identity, field.key, form.get_widget(field.key).parse()) identities.get_store().save(identity) get_logger().info('Updated identity %r' % identity.id) return misc.redirect_to_return_url() or \ misc.redirect_to_referer(form) or \ misc.redirect_home() def forgot_identifier(self): get_response().breadcrumb.append( ('forgot_identifier', _('Lost Account Name')) ) form = Form(enctype="multipart/form-data", use_tokens = False) form.add(EmailWidget, "email", title= _('Email'), size=30, required=True) form.add_submit("submit", _("Submit")) form.add_submit("cancel", _("Cancel")) if form.get_submit() == 'cancel': return redirect('.') if form.is_submitted() and not form.has_errors(): res, error = self.forgot_identifier_submit(form) if not error: return res form.set_error('email', error) vars = { 'lost_identifier_form': str(form.render()) } template.html_top(_('Lost Account Name')) return template.process_template( str(TextsDirectory.get_html_text('lost_identifier')), vars) def forgot_identifier_submit(self, form): email = form.get_widget('email').parse() idents = identities.get_store().get_identities_by_attributes({ 'email': email }) usernames = [] for ident in idents: for account in ident.accounts: if hasattr(account, 'username'): usernames.append(account.username) if not usernames: return None, _('There is no account with this email') data = { 'hostname': get_request().get_server(), 'email': email, 'usernames': usernames, 'service': get_session().service, } get_logger().info('Forgot identifier: reminded email %r of its accounts' % email) try: emails.custom_ezt_email('identifier-email', data, email) except: return None, _('Error when sending the mail') get_session().message = ('info', _('Your identifiers have been send to %s')) return misc.redirect_home(), None TextsDirectory.register('account', N_('Account Management'), hint = N_('Available variables: identity_label, idp_sso_list, show_federations, federations_list, certificate_list, allow_certificate_federation'), default = N_('''\Associate a certificate to this account
[if-any certificate_list] [certificate_list] [end] [end] [end] [is admin "True"] [end] ''')) TextsDirectory.register('register', N_('Registration'), hint = N_('Available variable: register_form'), default = N_('''\ [register_form] ''')) TextsDirectory.register('register_completed', N_('Registration Completed'), default = N_('''\Your password has been mailed to you.
''')) TextsDirectory.register('change_password', N_('Changing Password'), hint = N_('Available variable: change_password_form'), default = N_('''\Fill the form to get a new password mailed back to you.
[else]Fill the form to get your password mailed back to you.
[end] [end] [is lost_password_behaviour "email_reminder"] [is generate_on_remind "True"]A new password will be mailed back to you.
[else]Your password will be mailed back to you.
[end] [end] [lost_password_form] ''')) TextsDirectory.register('lost_identifier', N_('Lost Account Name'), hint = N_('Available variables: lost_identifier_form'), default = N_('''\Give your email to get back the list of your accounts.
[lost_identifier_form] ''')) TextsDirectory.register('lost_password_question', N_('Lost Password Question'), hint = N_('Available variable: lost_password_question_form'), default = N_('''\ [lost_password_question_form] ''')) TextsDirectory.register('lost_password_mailed', N_('Lost Password (mailed)'), default = N_('''\Your password has been mailed back to you.
Login ''')) TextsDirectory.register('update_info', N_('Updating Personal Information'), hint = N_('Available variable: info_form'), default = N_('''\ [info_form] ''')) TextsDirectory.register('login', N_('Login'), hint = N_('Available variables: login_form, authentication_failure'), default = N_('''\