Exemple #1
0
    def __init__(self, kw):
        self.thank_you_mode = False
        self.errors = []
        self.user = Bunch()

        self.user.email_address = kw.get('email_address', '').strip()
        if not (5 <= len(self.user.email_address) <= 50):
            self.errors.append(
                'Email Address needs to be between 5 and 50 characters.')

        self.user.full_name = kw.get('full_name', '').strip()
        if not (5 <= len(self.user.full_name) <= 50):
            self.errors.append(
                'Full Name needs to be between 5 and 50 characters.')

        self.user.organization = kw.get('organization', '').strip()
        if self.user.organization and not (5 <= len(self.user.organization) <=
                                           50):
            self.errors.append(
                'Organization needs to be empty or between 5 and 50 characters.'
            )

        password = str(kw.get('password', ''))
        if not (6 <= len(password)):
            self.errors.append('Password needs to be at least 6 characters.')

        if kw.get('password_confirm') != password:
            self.errors.append("Passwords don't match.")

        if self.errors:
            return

        logger.debug("No errors!")

        set_password(password, self.user)

        self.user.registration_info = json.dumps(basic_info(), sort_keys=True)

        self.new_user = model.User(**self.user.__dict__)
        db_session.add(self.new_user)

        try:
            db_session.commit()
        except sqlalchemy.exc.IntegrityError:
            # This exception is thrown if the email address is already in the database
            # To do: Perhaps put a link to sign in using an existing account here
            self.errors.append(
                "An account with this email address already exists. "
                "Click the button above to sign in using an existing account.")
            return

        logger.debug("Adding verification email to queue")
        #self.send_email_verification()
        VerificationEmail(self.new_user)
        logger.debug("Added verification email to queue")

        self.thank_you_mode = True
Exemple #2
0
 def oauth2_login(self, login_type, user_id):
     """Login via an OAuth2 provider"""
     es = get_elasticsearch_connection()
     user_details = get_user_by_unique_column(es, "user_id", user_id)
     if user_details:
         user = model.User()
         user.id = user_details["user_id"]
         user.full_name = user_details["name"]
         user.login_type = user_details["login_type"]
         return self.actual_login(user)
     else:
         flash("Error logging in via OAuth2")
         return make_response(redirect(url_for('login')))
Exemple #3
0
    def standard_login(self):
        """Login through the normal form"""
        params = request.form if request.form else request.args
        logger.debug("in login params are:", params)

        if not params:
            from utility.tools import GITHUB_AUTH_URL, GITHUB_CLIENT_ID, ORCID_AUTH_URL, ORCID_CLIENT_ID
            external_login = {}
            if GITHUB_AUTH_URL and GITHUB_CLIENT_ID != 'UNKNOWN':
                external_login["github"] = GITHUB_AUTH_URL
            if ORCID_AUTH_URL and ORCID_CLIENT_ID != 'UNKNOWN':
                external_login["orcid"] = ORCID_AUTH_URL

            return render_template("new_security/login_user.html",
                                   external_login=external_login,
                                   redis_is_available=is_redis_available())
        else:
            user_details = get_user_by_unique_column("email_address",
                                                     params["email_address"])
            #user_details = get_user_by_unique_column(es, "email_address", params["email_address"])
            user = None
            valid = None
            if user_details:
                user = model.User()
                for key in user_details:
                    user.__dict__[key] = user_details[key]
                valid = False

                submitted_password = params['password']
                pwfields = Struct(json.loads(user.password))
                encrypted = Password(submitted_password, pwfields.salt,
                                     pwfields.iterations, pwfields.keylength,
                                     pwfields.hashfunc)
                logger.debug("\n\nComparing:\n{}\n{}\n".format(
                    encrypted.password, pwfields.password))
                valid = pbkdf2.safe_str_cmp(encrypted.password,
                                            pwfields.password)
                logger.debug("valid is:", valid)

        if valid and not user.confirmed:
            VerificationEmail(user)
            return render_template(
                "new_security/verification_still_needed.html",
                subject=VerificationEmail.subject)
        if valid:
            if params.get('remember'):
                logger.debug("I will remember you")
                self.remember_me = True

            if 'import_collections' in params:
                import_col = "true"
            else:
                import_col = "false"

            #g.cookie_session.import_traits_to_user()

            self.logged_in = True

            return self.actual_login(user, import_collections=import_col)

        else:
            if user:
                self.unsuccessful_login(user)
            flash("Invalid email-address or password. Please try again.",
                  "alert-danger")
            response = make_response(redirect(url_for('login')))

            return response