Esempio n. 1
0
    def __init__(self, toaddr):
        from email.MIMEMultipart import MIMEMultipart
        from email.MIMEText import MIMEText
        verification_code = str(uuid.uuid4())
        key = self.key_prefix + ":" + verification_code

        data = {
            "verification_code": verification_code,
            "email_address": toaddr,
            "timestamp": timestamp()
        }
        es = get_elasticsearch_connection()
        es_save_data(es, self.key_prefix, "local", data, verification_code)

        subject = self.subject
        body = render_template(self.template_name,
                               verification_code=verification_code)

        msg = MIMEMultipart()
        msg["To"] = toaddr
        msg["Subject"] = self.subject
        msg["From"] = self.fromaddr
        msg.attach(MIMEText(body, "plain"))

        send_email(toaddr, msg.as_string())
Esempio n. 2
0
    def num_collections(self):
        """Number of user's collections"""

        es = get_elasticsearch_connection()

        user_email = self.record['user_email_address']

        #ZS: Get user's collections if they exist
        response = es.search(
            index="users",
            doc_type="local",
            body={"query": {
                "match": {
                    "email_address": user_email
                }
            }})

        user_info = response['hits']['hits'][0]['_source']
        logger.debug("USER NUM COLL:", user_info)
        if 'collections' in user_info.keys():
            if user_info['collections'] != "[]" and len(
                    user_info['collections']) > 0:
                collections_json = json.loads(user_info['collections'])
                return len(collections_json)
            else:
                return 0
        else:
            return 0
Esempio n. 3
0
def password_reset():
    """Entry point after user clicks link in E-mail"""
    logger.debug("in password_reset request.url is:", request.url)
    # We do this mainly just to assert that it's in proper form for displaying next page
    # Really not necessary but doesn't hurt
    # user_encode = DecodeUser(ForgotPasswordEmail.key_prefix).reencode_standalone()
    verification_code = request.args.get('code')
    hmac = request.args.get('hm')
    es = get_elasticsearch_connection()
    if verification_code:
        code_details = get_item_by_unique_column(
            es, "verification_code", verification_code,
            ForgotPasswordEmail.key_prefix, "local")
        if code_details:
            user_details = get_user_by_unique_column(
                es, "email_address", code_details["email_address"])
            if user_details:
                return render_template("new_security/password_reset.html",
                                       user_encode=user_details["user_id"])
            else:
                flash("Invalid code: User no longer exists!", "error")
        else:
            flash(
                "Invalid code: Password reset code does not exist or might have expired!",
                "error")
        return redirect(
            url_for("login")
        )  #render_template("new_security/login_user.html", error=error)
Esempio n. 4
0
def password_reset_step2():
    """Handle confirmation E-mail for password reset"""
    logger.debug("in password_reset request.url is:", request.url)

    errors = []
    user_id = request.form['user_encode']

    logger.debug("locals are:", locals())

    user = Bunch()
    password = request.form['password']
    set_password(password, user)

    es = get_elasticsearch_connection()
    es.update(index="users",
              doc_type="local",
              id=user_id,
              body={"doc": {
                  "password": user.__dict__.get("password")
              }})

    flash("Password changed successfully. You can now sign in.", "alert-info")
    response = make_response(redirect(url_for('login')))

    return response
    def setUp(self):
        self.es = get_elasticsearch_connection()
        self.es_cleanup = []

        es_logger = logging.getLogger("elasticsearch")
        es_logger.setLevel(app.config.get("LOG_LEVEL"))
        es_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationInfo.log"))
        es_trace_logger = logging.getLogger("elasticsearch.trace")
        es_trace_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationTrace.log"))
    def setUp(self):
        self.es = get_elasticsearch_connection()
        self.es_cleanup = []

        es_logger = logging.getLogger("elasticsearch")
        es_logger.setLevel(app.config.get("LOG_LEVEL"))
        es_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationInfo.log"))
        es_trace_logger = logging.getLogger("elasticsearch.trace")
        es_trace_logger.addHandler(
            logging.FileHandler("/tmp/es_TestRegistrationTrace.log"))
Esempio n. 7
0
    def update_collections(self, updated_collections):
        es = get_elasticsearch_connection()

        collection_body = {
            'doc': {
                'collections': json.dumps(updated_collections)
            }
        }
        es.update(index='users',
                  doc_type='local',
                  id=self.es_user_id,
                  refresh='wait_for',
                  body=collection_body)
Esempio n. 8
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')))
Esempio n. 9
0
    def add_collection(self, collection_name, traits):
        """Add collection into ElasticSearch"""

        collection_dict = {
            'id':
            unicode(uuid.uuid4()),
            'name':
            collection_name,
            'created_timestamp':
            datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'),
            'changed_timestamp':
            datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'),
            'num_members':
            len(traits),
            'members':
            list(traits)
        }

        es = get_elasticsearch_connection()

        user_email = self.record['user_email_address']
        response = es.search(
            index="users",
            doc_type="local",
            body={"query": {
                "match": {
                    "email_address": user_email
                }
            }})

        user_id = response['hits']['hits'][0]['_id']
        user_info = response['hits']['hits'][0]['_source']

        if 'collections' in user_info.keys():
            if user_info['collections'] != [] and user_info[
                    'collections'] != "[]":
                current_collections = json.loads(user_info['collections'])
                current_collections.append(collection_dict)
                self.update_collections(current_collections)
                #collections_json = json.dumps(current_collections)
            else:
                self.update_collections([collection_dict])
                #collections_json = json.dumps([collection_dict])
        else:
            self.update_collections([collection_dict])
            #collections_json = json.dumps([collection_dict])

        return collection_dict['id']
Esempio n. 10
0
    def es_user_id(self):
        """User id from ElasticSearch (need to check if this is the same as the id stored in self.records)"""

        es = get_elasticsearch_connection()
        user_email = self.record['user_email_address']

        #ZS: Get user's collections if they exist
        response = es.search(
            index="users",
            doc_type="local",
            body={"query": {
                "match": {
                    "email_address": user_email
                }
            }})

        user_id = response['hits']['hits'][0]['_id']
        return user_id
Esempio n. 11
0
def orcid_oauth2():
    from uuid import uuid4
    from utility.tools import ORCID_CLIENT_ID, ORCID_CLIENT_SECRET, ORCID_TOKEN_URL, ORCID_AUTH_URL
    code = request.args.get("code")
    error = request.args.get("error")
    url = "/n/login"
    if code:
        data = {
            "client_id": ORCID_CLIENT_ID,
            "client_secret": ORCID_CLIENT_SECRET,
            "grant_type": "authorization_code",
            "code": code
        }
        result = requests.post(ORCID_TOKEN_URL, data=data)
        result_dict = json.loads(result.text.encode("utf-8"))

        es = get_elasticsearch_connection()
        user_details = get_user_by_unique_column(es, "orcid",
                                                 result_dict["orcid"])
        if user_details == None:
            user_details = {
                "user_id":
                str(uuid4()),
                "name":
                result_dict["name"],
                "orcid":
                result_dict["orcid"],
                "user_url":
                "%s/%s" % ("/".join(
                    ORCID_AUTH_URL.split("/")[:-2]), result_dict["orcid"]),
                "login_type":
                "orcid",
                "organization":
                "",
                "active":
                1,
                "confirmed":
                1
            }
            save_user(es, user_details, user_details["user_id"])
        url = "/n/login?type=orcid&uid=" + user_details["user_id"]
    else:
        flash("There was an error getting code from ORCID")
    return redirect(url)
Esempio n. 12
0
def forgot_password_submit():
    """When a forgotten password form is submitted we get here"""
    params = request.form
    email_address = params['email_address']
    next_page = None
    if email_address != "":
        logger.debug("Wants to send password E-mail to ", email_address)
        es = get_elasticsearch_connection()
        user_details = get_user_by_unique_column(es, "email_address",
                                                 email_address)
        if user_details:
            ForgotPasswordEmail(user_details["email_address"])

        return render_template("new_security/forgot_password_step2.html",
                               subject=ForgotPasswordEmail.subject)

    else:
        flash("You MUST provide an email", "alert-danger")
        return redirect(url_for("forgot_password"))
Esempio n. 13
0
def get_transform_vals(dataset, trait):
    es = get_elasticsearch_connection(for_user=False)

    logger.info("DATASET NAME:", dataset.name)

    query = '{"bool": {"must": [{"match": {"name": "%s"}}, {"match": {"dataset": "%s"}}]}}' % (trait.name, dataset.name)

    es_body = {
          "query": {
            "bool": {
              "must": [
                {
                  "match": {
                    "name": "%s" % (trait.name)
                  }
                },
                {
                  "match": {
                    "dataset": "%s" % (dataset.name)
                  }
                }
              ]
            }
          }
    }

    response = es.search( index = "traits", doc_type = "trait", body = es_body )
    logger.info("THE RESPONSE:", response)
    results = response['hits']['hits']

    if len(results) > 0:
        samples = results[0]['_source']['samples']

        sample_dict = {}
        for sample in samples:
            sample_dict[sample['name']] = sample['qnorm']

        logger.info("SAMPLE DICT:", sample_dict)
        return sample_dict
    else:
        return None
Esempio n. 14
0
def get_transform_vals(dataset, trait):
    es = get_elasticsearch_connection(for_user=False)

    logger.info("DATASET NAME:", dataset.name)

    query = '{"bool": {"must": [{"match": {"name": "%s"}}, {"match": {"dataset": "%s"}}]}}' % (
        trait.name, dataset.name)

    es_body = {
        "query": {
            "bool": {
                "must": [{
                    "match": {
                        "name": "%s" % (trait.name)
                    }
                }, {
                    "match": {
                        "dataset": "%s" % (dataset.name)
                    }
                }]
            }
        }
    }

    response = es.search(index="traits", doc_type="trait", body=es_body)
    logger.info("THE RESPONSE:", response)
    results = response['hits']['hits']

    if len(results) > 0:
        samples = results[0]['_source']['samples']

        sample_dict = {}
        for sample in samples:
            sample_dict[sample['name']] = sample['qnorm']

        logger.info("SAMPLE DICT:", sample_dict)
        return sample_dict
    else:
        return None
Esempio n. 15
0
def register():
    params = None
    errors = None

    params = request.form if request.form else request.args
    params = params.to_dict(flat=True)
    es = get_elasticsearch_connection()
    params["es_connection"] = es

    if params:
        logger.debug("Attempting to register the user...")
        result = RegisterUser(params)
        errors = result.errors

        if len(errors) == 0:
            flash(
                "Registration successful. You may login with your new account",
                "alert-info")
            return redirect(url_for("login"))

    return render_template("new_security/register_user.html",
                           values=params,
                           errors=errors)
Esempio n. 16
0
def github_oauth2():
    from utility.tools import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET
    code = request.args.get("code")
    data = {
        "client_id": GITHUB_CLIENT_ID,
        "client_secret": GITHUB_CLIENT_SECRET,
        "code": code
    }
    result = requests.post("https://github.com/login/oauth/access_token",
                           json=data)
    result_dict = {
        arr[0]: arr[1]
        for arr in [
            tok.split("=") for tok in
            [token.encode("utf-8") for token in result.text.split("&")]
        ]
    }

    github_user = get_github_user_details(result_dict["access_token"])
    es = get_elasticsearch_connection()
    user_details = get_user_by_unique_column(es, "github_id",
                                             github_user["id"])
    if user_details == None:
        user_details = {
            "user_id": str(uuid.uuid4()),
            "name": github_user["name"].encode("utf-8"),
            "github_id": github_user["id"],
            "user_url": github_user["html_url"].encode("utf-8"),
            "login_type": "github",
            "organization": "",
            "active": 1,
            "confirmed": 1
        }
        save_user(es, user_details, user_details["user_id"])
    url = "/n/login?type=github&uid=" + user_details["user_id"]
    return redirect(url)
Esempio n. 17
0
    def user_collections(self):
        """List of user's collections"""

        es = get_elasticsearch_connection()

        user_email = self.record['user_email_address']

        #ZS: Get user's collections if they exist
        response = es.search(
            index="users",
            doc_type="local",
            body={"query": {
                "match": {
                    "email_address": user_email
                }
            }})
        user_info = response['hits']['hits'][0]['_source']
        if 'collections' in user_info.keys():
            if len(user_info['collections']) > 0:
                return json.loads(user_info['collections'])
            else:
                return []
        else:
            return []
Esempio n. 18
0
                        "dataset_fullname": result_info[2],
                        "samples": sample_list,
                        "transform_types": "qnorm"
                    }
                }


if __name__ == '__main__':
    Conn = MySQLdb.Connect(**parse_db_uri())
    Cursor = Conn.cursor()

    #es = Elasticsearch([{
    #    "host": ELASTICSEARCH_HOST, "port": ELASTICSEARCH_PORT
    #}], timeout=60) if (ELASTICSEARCH_HOST and ELASTICSEARCH_PORT) else None

    es = get_elasticsearch_connection(for_user=False)

    #input_filename = "/home/zas1024/cfw_data/" + sys.argv[1] + ".txt"
    #input_df = create_dataframe(input_filename)
    #output_df = quantileNormalize(input_df)

    #output_df.to_csv('quant_norm.csv', sep='\t')

    #out_filename = sys.argv[1][:-4] + '_quantnorm.txt'

    success, _ = bulk(es, set_data(sys.argv[1]))

    response = es.search(
        index="traits",
        doc_type="trait",
        body={"query": {
                        "dataset": dataset_name,
                        "dataset_fullname": result_info[2],
                        "samples": sample_list,
                        "transform_types": "qnorm"
                    }
                }

if __name__ == '__main__':
    Conn = MySQLdb.Connect(**parse_db_uri())
    Cursor = Conn.cursor()

    #es = Elasticsearch([{
    #    "host": ELASTICSEARCH_HOST, "port": ELASTICSEARCH_PORT
    #}], timeout=60) if (ELASTICSEARCH_HOST and ELASTICSEARCH_PORT) else None

    es = get_elasticsearch_connection(for_user=False)

    #input_filename = "/home/zas1024/cfw_data/" + sys.argv[1] + ".txt"
    #input_df = create_dataframe(input_filename)
    #output_df = quantileNormalize(input_df)

    #output_df.to_csv('quant_norm.csv', sep='\t')

    #out_filename = sys.argv[1][:-4] + '_quantnorm.txt'

    success, _ = bulk(es, set_data(sys.argv[1]))

    response = es.search(
        index = "traits", doc_type = "trait", body = {
            "query": { "match": { "name": "ENSMUSG00000028982" } }
        }
Esempio n. 20
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)
        es = get_elasticsearch_connection()
        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
            assert (es is not None)
            return render_template("new_security/login_user.html",
                                   external_login=external_login,
                                   es_server=es.ping())
        else:
            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