示例#1
0
def do_login():
    """ Lo the user in user.
    """
    form = forms.LoginForm()
    next_url = flask.request.args.get('next_url')
    if not next_url or next_url == 'None':
        next_url = flask.url_for('index')

    if form.validate_on_submit():
        username = form.username.data
        password = '******' % (form.password.data,
                             APP.config.get('PASSWORD_SEED', None))
        password = hashlib.sha512(password).hexdigest()

        user_obj = pagure.lib.search_user(SESSION, username=username)
        if not user_obj or user_obj.password != password:
            flask.flash('Username or password invalid.', 'error')
            return flask.redirect(flask.url_for('auth_login'))
        elif user_obj.token:
            flask.flash(
                'Invalid user, did you confirm the creation with the url '
                'provided by email?', 'error')
            return flask.redirect(flask.url_for('auth_login'))
        else:
            visit_key = pagure.lib.login.id_generator(40)
            now = datetime.datetime.utcnow()
            expiry = now + datetime.timedelta(days=30)
            session = model.PagureUserVisit(
                user_id=user_obj.id,
                user_ip=flask.request.remote_addr,
                visit_key=visit_key,
                expiry=expiry,
            )
            SESSION.add(session)
            try:
                SESSION.commit()
                flask.g.fas_user = user_obj
                flask.g.fas_session_id = visit_key
                flask.g.fas_user.login_time = now
                flask.flash('Welcome %s' % user_obj.username)
            except SQLAlchemyError as err:  # pragma: no cover
                flask.flash(
                    'Could not set the session in the db, '
                    'please report this error to an admin', 'error')
                APP.logger.exception(err)

        return flask.redirect(next_url)
    else:
        flask.flash('Insufficient information provided', 'error')
    return flask.redirect(flask.url_for('auth_login'))
示例#2
0
文件: login.py 项目: mausvt/pagure
def do_login():
    """ Log in the user.
    """
    logout()

    form = forms.LoginForm()
    next_url = flask.request.form.get("next_url")
    if not next_url or next_url == "None":
        next_url = flask.url_for("ui_ns.index")

    if form.validate_on_submit():
        username = form.username.data
        try:
            pagure.lib.login.check_username_and_password(
                flask.g.session, username, form.password.data)
        except pagure.exceptions.PagureException as ex:
            _log.exception(ex)
            flask.flash(str(ex), "error")
            return flask.redirect(flask.url_for("auth_login"))

        user_obj = pagure.lib.query.search_user(flask.g.session,
                                                username=username)
        visit_key = pagure.lib.login.id_generator(40)
        now = datetime.datetime.utcnow()
        expiry = now + datetime.timedelta(days=30)
        session = model.PagureUserVisit(
            user_id=user_obj.id,
            user_ip=flask.request.remote_addr,
            visit_key=visit_key,
            expiry=expiry,
        )
        flask.g.session.add(session)
        try:
            flask.g.session.commit()
            flask.g.fas_user = user_obj
            flask.g.fas_session_id = visit_key
            flask.g.fas_user.login_time = now
            flask.flash("Welcome %s" % user_obj.username)
        except SQLAlchemyError as err:  # pragma: no cover
            flask.flash(
                "Could not set the session in the db, "
                "please report this error to an admin",
                "error",
            )
            _log.exception(err)

        return flask.redirect(next_url)
    else:
        flask.flash("Insufficient information provided", "error")
    return flask.redirect(flask.url_for("auth_login"))
示例#3
0
def do_login():
    """ Log in the user.
    """
    logout()

    form = forms.LoginForm()
    next_url = flask.request.form.get("next_url")
    if not next_url or next_url == "None":
        next_url = flask.url_for("ui_ns.index")

    if form.validate_on_submit():
        username = form.username.data
        user_obj = pagure.lib.query.search_user(flask.g.session,
                                                username=username)
        if not user_obj:
            flask.flash("Username or password invalid.", "error")
            return flask.redirect(flask.url_for("auth_login"))

        try:
            password_checks = check_password(
                form.password.data,
                user_obj.password,
                seed=pagure.config.config.get("PASSWORD_SEED", None),
            )
        except pagure.exceptions.PagureException as err:
            _log.exception(err)
            flask.flash("Username or password of invalid format.", "error")
            return flask.redirect(flask.url_for("auth_login"))

        if not password_checks:
            flask.flash("Username or password invalid.", "error")
            return flask.redirect(flask.url_for("auth_login"))

        elif user_obj.token:
            flask.flash(
                "Invalid user, did you confirm the creation with the url "
                "provided by email?",
                "error",
            )
            return flask.redirect(flask.url_for("auth_login"))

        else:
            password = user_obj.password
            if not isinstance(password, six.text_type):
                password = password.decode("utf-8")
            if not password.startswith("$2$"):
                user_obj.password = generate_hashed_value(form.password.data)
                flask.g.session.add(user_obj)
                flask.g.session.flush()

            visit_key = pagure.lib.login.id_generator(40)
            now = datetime.datetime.utcnow()
            expiry = now + datetime.timedelta(days=30)
            session = model.PagureUserVisit(
                user_id=user_obj.id,
                user_ip=flask.request.remote_addr,
                visit_key=visit_key,
                expiry=expiry,
            )
            flask.g.session.add(session)
            try:
                flask.g.session.commit()
                flask.g.fas_user = user_obj
                flask.g.fas_session_id = visit_key
                flask.g.fas_user.login_time = now
                flask.flash("Welcome %s" % user_obj.username)
            except SQLAlchemyError as err:  # pragma: no cover
                flask.flash(
                    "Could not set the session in the db, "
                    "please report this error to an admin",
                    "error",
                )
                _log.exception(err)

        return flask.redirect(next_url)
    else:
        flask.flash("Insufficient information provided", "error")
    return flask.redirect(flask.url_for("auth_login"))
示例#4
0
def do_login():
    """ Log in the user.
    """
    form = forms.LoginForm()
    next_url = flask.request.form.get('next_url')
    if not next_url or next_url == 'None':
        next_url = flask.url_for('index')

    if form.validate_on_submit():
        username = form.username.data
        user_obj = pagure.lib.search_user(SESSION, username=username)
        if not user_obj:
            flask.flash('Username or password invalid.', 'error')
            return flask.redirect(flask.url_for('auth_login'))

        try:
            password_checks = check_password(form.password.data,
                                             user_obj.password,
                                             seed=APP.config.get(
                                                 'PASSWORD_SEED', None))
        except pagure.exceptions.PagureException as err:
            _log.exception(err)
            flask.flash('Username or password of invalid format.', 'error')
            return flask.redirect(flask.url_for('auth_login'))

        if not password_checks:
            flask.flash('Username or password invalid.', 'error')
            return flask.redirect(flask.url_for('auth_login'))

        elif user_obj.token:
            flask.flash(
                'Invalid user, did you confirm the creation with the url '
                'provided by email?', 'error')
            return flask.redirect(flask.url_for('auth_login'))

        else:

            if not user_obj.password.startswith('$2$'):
                user_obj.password = generate_hashed_value(form.password.data)
                SESSION.add(user_obj)

            visit_key = pagure.lib.login.id_generator(40)
            now = datetime.datetime.utcnow()
            expiry = now + datetime.timedelta(days=30)
            session = model.PagureUserVisit(
                user_id=user_obj.id,
                user_ip=flask.request.remote_addr,
                visit_key=visit_key,
                expiry=expiry,
            )
            SESSION.add(session)
            try:
                SESSION.commit()
                flask.g.fas_user = user_obj
                flask.g.fas_session_id = visit_key
                flask.g.fas_user.login_time = now
                flask.flash('Welcome %s' % user_obj.username)
            except SQLAlchemyError as err:  # pragma: no cover
                flask.flash(
                    'Could not set the session in the db, '
                    'please report this error to an admin', 'error')
                _log.exception(err)

        return flask.redirect(next_url)
    else:
        flask.flash('Insufficient information provided', 'error')
    return flask.redirect(flask.url_for('auth_login'))