Exemplo n.º 1
0
def send_group_invites(group_id, user_email_list=[], user_type="members"):
    for user_email in user_email_list:
        user_details = get_user_by_unique_column("email_address", user_email)
        if user_details:
            group_info = get_group_info(group_id)
            #ZS: Probably not necessary since the group should normally always exist if group_id is being passed here,
            #    but it's technically possible to hit it if Redis is cleared out before submitting the new users or something
            if group_info:
                #ZS: Don't add user if they're already an admin or if they're being added a regular user and are already a regular user,
                #    but do add them if they're a regular user and are added as an admin
                if (user_details['user_id'] in group_info['admins']) or \
                   ((user_type == "members") and (user_details['user_id'] in group_info['members'])):
                    continue
                else:
                    send_verification_email(
                        user_details,
                        template_name="email/group_verification.txt",
                        key_prefix="verification_code",
                        subject=
                        "You've been invited to join a GeneNetwork user group")
        else:
            temp_password = ''.join(
                random.choice(string.ascii_uppercase + string.digits)
                for _ in range(6))
            user_details = {
                'user_id': str(uuid.uuid4()),
                'email_address': user_email,
                'registration_info': basic_info(),
                'password': set_password(temp_password),
                'confirmed': 0
            }
            save_user(user_details, user_details['user_id'])
            send_invitation_email(user_email, temp_password)
Exemplo n.º 2
0
    def __init__(self, kw):
        self.thank_you_mode = False
        self.errors = []
        self.user = Bunch()

        self.user.email_address = kw.get('email_address',
                                         '').encode("utf-8").strip()
        if not (5 <= len(self.user.email_address) <= 50):
            self.errors.append(
                'Email Address needs to be between 5 and 50 characters.')
        else:
            email_exists = get_user_by_unique_column("email_address",
                                                     self.user.email_address)
            #email_exists = get_user_by_unique_column(es, "email_address", self.user.email_address)
            if email_exists:
                self.errors.append('User already exists with that email')

        self.user.full_name = kw.get('full_name', '').encode("utf-8").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',
                                        '').encode("utf-8").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.user_id = str(uuid.uuid4())
        self.user.confirmed = 1

        self.user.registration_info = json.dumps(basic_info(), sort_keys=True)
        save_user(self.user.__dict__, self.user.user_id)
Exemplo n.º 3
0
def github_oauth2():
    from utility.tools import GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, GITHUB_AUTH_URL
    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"])

    user_details = get_user_by_unique_column("github_id", github_user["id"])
    if user_details == None:
        user_details = {
            "user_id":
            str(uuid.uuid4()),
            "name":
            github_user["name"].encode("utf-8")
            if github_user["name"] else "None",
            "github_id":
            github_user["id"],
            "user_url":
            github_user["html_url"].encode("utf-8"),
            "login_type":
            "github",
            "organization":
            "",
            "active":
            1,
            "confirmed":
            1
        }
        save_user(user_details, user_details["user_id"])

    url = "/n/login?type=github&uid=" + user_details["user_id"]
    return redirect(url)
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",
            "redirect_uri": GN2_BRANCH_URL + "n/login/orcid_oauth2",
            "code": code
        }

        result = requests.post(ORCID_TOKEN_URL, data=data)
        result_dict = json.loads(result.text.encode("utf-8"))

        user_details = get_user_by_unique_column("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(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)
def register_user(params):
    thank_you_mode = False
    errors = []
    user_details = {}

    user_details['email_address'] = params.get('email_address',
                                               '').encode("utf-8").strip()
    if not (5 <= len(user_details['email_address']) <= 50):
        errors.append('Email Address needs to be between 5 and 50 characters.')
    else:
        email_exists = get_user_by_unique_column("email_address",
                                                 user_details['email_address'])
        if email_exists:
            errors.append('User already exists with that email')

    user_details['full_name'] = params.get('full_name',
                                           '').encode("utf-8").strip()
    if not (5 <= len(user_details['full_name']) <= 50):
        errors.append('Full Name needs to be between 5 and 50 characters.')

    user_details['organization'] = params.get('organization',
                                              '').encode("utf-8").strip()
    if user_details['organization'] and not (5 <= len(
            user_details['organization']) <= 50):
        errors.append(
            'Organization needs to be empty or between 5 and 50 characters.')

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

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

    user_details['password'] = set_password(password)
    user_details['user_id'] = str(uuid.uuid4())
    user_details['confirmed'] = 1

    user_details['registration_info'] = basic_info()

    if len(errors) == 0:
        save_user(user_details, user_details['user_id'])

    return errors
def get_signed_session_id(user):
    session_id = str(uuid.uuid4())

    session_id_signature = hmac.hmac_creation(session_id)
    session_id_signed = session_id + ":" + session_id_signature

    #ZS: Need to check if this is ever actually used or exists
    if 'user_id' not in user:
        user['user_id'] = str(uuid.uuid4())
        save_user(user, user['user_id'])

    if 'github_id' in user:
        session = dict(login_time=time.time(),
                       user_type="github",
                       user_id=user['user_id'],
                       github_id=user['github_id'],
                       user_name=user['name'],
                       user_url=user['user_url'])
    elif 'orcid' in user:
        session = dict(login_time=time.time(),
                       user_type="orcid",
                       user_id=user['user_id'],
                       github_id=user['orcid'],
                       user_name=user['name'],
                       user_url=user['user_url'])
    else:
        session = dict(login_time=time.time(),
                       user_type="gn2",
                       user_id=user['user_id'],
                       user_name=user['full_name'],
                       user_email_address=user['email_address'])

    key = UserSession.user_cookie_name + ":" + session_id
    Redis.hmset(key, session)
    Redis.expire(key, THREE_DAYS)

    return session_id_signed