Example #1
0
def set_profiles():
    if not g.notebook.conf()['openid']:
        return redirect(url_for('base.index'))
    if request.method == 'GET':
        if 'openid_response' in session:
            from sagenb.notebook.misc import valid_username_chars
            re_invalid_username_chars = re.compile('[^(%s)]' %
                                                   valid_username_chars)
            openid_resp = session['openid_response']
            if openid_resp.fullname is not None:
                openid_resp.fullname = re.sub(re_invalid_username_chars, '_',
                                              openid_resp.fullname)
            return render_template('html/accounts/openid_profile.html',
                                   resp=openid_resp)
        else:
            return redirect(url_for('base.index'))

    if request.method == 'POST':
        if 'openid_response' in session:
            parse_dict = {'resp': session['openid_response']}
        else:
            return redirect(url_for('base.index'))

        try:
            resp = session['openid_response']
            username = request.form.get('username')
            from sagenb.notebook.user import User
            from sagenb.notebook.misc import is_valid_username, is_valid_email
            if not is_valid_username(username):
                parse_dict['username_invalid'] = True
                raise ValueError
            if g.notebook.user_manager().user_exists(username):
                parse_dict['username_taken'] = True
                raise ValueError
            if not is_valid_email(request.form.get('email')):
                parse_dict['email_invalid'] = True
                raise ValueError
            try:
                new_user = User(username,
                                '',
                                email=resp.email,
                                account_type='user')
                g.notebook.user_manager().add_user_object(new_user)
            except ValueError:
                parse_dict['creation_error'] = True
                raise ValueError
            g.notebook.user_manager().create_new_openid(
                resp.identity_url, username)
            session['username'] = g.username = username
            session.modified = True
        except ValueError:
            return render_template('html/accounts/openid_profile.html',
                                   **parse_dict)
        return redirect(url_for('base.index'))
Example #2
0
def create_or_login(resp):
    username = sanitize_openid(resp.identity_url)
    if g.notebook.user_manager().user_exists(username):
        session['username'] = g.username = username
        session.modified = True
    else:
        from sagenb.notebook.user import User
        new_user = User(username, '', email = resp.email, account_type='user') 
        try: 
            g.notebook.user_manager().add_user_object(new_user)
            session['username'] = g.username = username
            session.modified = True
        except ValueError:
            #add creation_error=True to the render dict somehow 
            from authentication import login
            return login({'creation_error': True})
    return redirect(request.values.get('next', url_for('base.index')))
Example #3
0
def set_profiles():
    if not g.notebook.conf()['openid']:
        return redirect(url_for('base.index'))

    from sagenb.notebook.challenge import challenge

    show_challenge = g.notebook.conf()['challenge']
    if show_challenge:
        chal = challenge(g.notebook.conf(),
                         is_secure=g.notebook.secure,
                         remote_ip=request.environ['REMOTE_ADDR'])

    if request.method == 'GET':
        if 'openid_response' in session:
            from sagenb.notebook.misc import valid_username_chars
            re_invalid_username_chars = re.compile('[^(%s)]' %
                                                   valid_username_chars)
            openid_resp = session['openid_response']
            if openid_resp.fullname is not None:
                openid_resp.fullname = re.sub(re_invalid_username_chars, '_',
                                              openid_resp.fullname)
            template_dict = {}
            if show_challenge:
                template_dict['challenge_html'] = chal.html()

            return render_template('html/accounts/openid_profile.html',
                                   resp=openid_resp,
                                   challenge=show_challenge,
                                   **template_dict)
        else:
            return redirect(url_for('base.index'))

    if request.method == 'POST':
        if 'openid_response' in session:
            parse_dict = {'resp': session['openid_response']}
        else:
            return redirect(url_for('base.index'))

        try:
            resp = session['openid_response']
            username = request.form.get('username')
            from sagenb.notebook.user import User
            from sagenb.notebook.misc import is_valid_username, is_valid_email

            if show_challenge:
                parse_dict['challenge'] = True
                status = chal.is_valid_response(req_args=request.values)
                if status.is_valid is True:
                    pass
                elif status.is_valid is False:
                    err_code = status.error_code
                    if err_code:
                        parse_dict['challenge_html'] = chal.html(
                            error_code=err_code)
                    else:
                        parse_dict['challenge_invalid'] = True
                    raise ValueError("Invalid challenge")
                else:
                    parse_dict['challenge_missing'] = True
                    raise ValueError("Missing challenge")

            if not is_valid_username(username):
                parse_dict['username_invalid'] = True
                raise ValueError("Invalid username")
            if g.notebook.user_manager().user_exists(username):
                parse_dict['username_taken'] = True
                raise ValueError("Pre-existing username")
            if not is_valid_email(request.form.get('email')):
                parse_dict['email_invalid'] = True
                raise ValueError("Invalid email")
            try:
                new_user = User(username,
                                '',
                                email=resp.email,
                                account_type='user')
                g.notebook.user_manager().add_user_object(new_user)
            except ValueError as msg:
                parse_dict['creation_error'] = True
                raise ValueError("Error in creating user\n%s" % msg)
            g.notebook.user_manager().create_new_openid(
                resp.identity_url, username)
            session['username'] = g.username = username
            session.modified = True
        except ValueError:
            return render_template('html/accounts/openid_profile.html',
                                   **parse_dict)
        return redirect(url_for('base.index'))