예제 #1
0
def one_tap_login(request):
    received_json_data = json.loads(request.body)
    credential = received_json_data['credential']
    decoded = jwt.decode(credential, verify=False)
    user_data = {
        "name": decoded['name'],
        "email": decoded['email'],
        "given_name": decoded['given_name'],
        "family_name": decoded['family_name']
    }
    
    json_user_data = json.dumps(user_data)
    request.session['user_data'] = json_user_data
    # run this in command shell:
    # python manage.py migrate

    client = OneLoginClient(
        mysecrets.ONELOGIN_CLIENT_ID, 
        mysecrets.ONELOGIN_CLIENT_SECRET,
        'us'
    )

    # 1. Make sure the user you want to create does not exist yet
    users = client.get_users({
        "email": decoded["email"]
    })

    # 2. Create the new user (explain the most interesting user parameters)
    if len(users) == 0:
        new_user_params = {
            "email": decoded["email"],
            "firstname": decoded["name"],
            "lastname": decoded["given_name"],
            "username": decoded["family_name"]
        }
        created_user = client.create_user(new_user_params)

        if created_user is not None:

            # 3. Assign the Default role to the user
            roles = client.get_roles({
                "name": "Default"
            })

            if  len(roles) == 1:
                role_ids = [
                    roles[0].id
                ]
                client.assign_role_to_user(created_user.id, role_ids)

            # 4. Set the user state
            USER_STATE_APPROVED = 1
            client.set_state_to_user(created_user.id, USER_STATE_APPROVED)

    return HttpResponse(json_user_data, content_type="application/json")
예제 #2
0
class OneLogin:
    def __init__(self):
        CLIENT_ID = os.environ["ONELOGIN_CLIENT_ID"]
        CLIENT_SECRET = os.environ["ONELOGIN_CLIENT_SECRET"]
        REGION = os.environ.get("ONELOGIN_REGION", "US").upper()
        self.client = OneLoginClient(CLIENT_ID, CLIENT_SECRET, REGION)

    def get_group_members(self, group_name=None):
        """
        This is technically not named well, since we're getting users assigned to a role, but
        because of the existing framework, the matching the function name keeps it reusable
        :param group_name:
        :return:
        """
        member_list = []
        role = self.client.get_roles(query_parameters={"name": group_name})
        users = self.client.get_users(query_parameters={"role_id": role[0].id})
        for user in users:
            member_list.append({"username": user.username, "email": user.email})

        return member_list