Exemplo n.º 1
0
def registration_view3(request):
    if request.method == 'POST':
        form = RegistrationForm2(request.POST)
        if form.is_valid():
            fn = form.cleaned_data['firstName']
            ln = form.cleaned_data['lastName']
            email = form.cleaned_data['email']
            user = {
                "profile": {
                    "firstName": fn,
                    "lastName": ln,
                    "email": email,
                    "login": email
                }
            }
            client = UsersClient('https://' + OKTA_ORG, API_KEY)
            client.create_user(user=user, activate="false")
        try:
            print('create user {0} {1}'.format(fn, ln))
            return HttpResponseRedirect(reverse('registration_success2'))
        except Exception as e:
            print("Error: {}".format(e))
            form.add_error(field=None, error=e)
    else:
        form = RegistrationForm2()
    return render(request, 'register2.html', {'form': form})
Exemplo n.º 2
0
def add_users(request, access_token):
    conf = _get_config(request)

    response = HttpResponse()
    response.status_code = 200

    if request.method == 'POST':
        req = request.POST

        email = ''
        first_name = ''
        last_name = ''
        role = ''
        activate = False

        profile_dict = request.session['profile']
        company_name = ''
        if 'companyName' in profile_dict:
            company_name = profile_dict.get('companyName')

        if 'email' in req:
            email = req['email']
        if 'firstName' in req:
            first_name = req['firstName']
        if 'lastName' in req:
            last_name = req['lastName']
        if 'role' in req:
            role = req['role']
        if 'activate' in req:
            activate = req['activate']
        client = UsersClient('https://' + conf['org'],
                             config.get_api_key(request))

        user = {
            "profile": {
                "firstName": first_name,
                "lastName": last_name,
                "email": email,
                "login": email,
                "customer_role": role,
                "companyName": company_name
            }
        }

        if api_access_admin(conf, access_token):
            users = client.create_user(user=user, activate=activate)
        elif api_access_company_admin(conf, access_token):
            users = client.create_user(user=user, activate=activate)
        else:
            return not_authorized(request)

        response.content = users

    return response
Exemplo n.º 3
0
def add_users(request, token):
    response = HttpResponse()
    response.status_code = 200

    if request.method == 'POST':
        req = request.POST

        email = ''
        firstName = ''
        lastName = ''
        role = ''
        activate = False

        #profile_dict = json.loads(request.session['profile'])
        profile_dict = request.session['profile']
        companyName = ''
        if 'companyName' in profile_dict:
            companyName = profile_dict.get('companyName')

        if 'email' in req:
            email = req['email']
        if 'firstName' in req:
            firstName = req['firstName']
        if 'lastName' in req:
            lastName = req['lastName']
        if 'role' in req:
            role = req['role']
        if 'activate' in req:
            activate = req['activate']
        client = UsersClient('https://' + OKTA_ORG, API_KEY)

        user = {
            "profile": {
                "firstName": firstName,
                "lastName": lastName,
                "email": email,
                "login": email,
                "customer_role": role,
                "companyName": companyName
            }
        }

        if api_access_admin(token):
            users = client.create_user(user=user, activate=activate)
        elif api_access_company_admin(token):
            users = client.create_user(user=user, activate=activate)
            # users = client.create_user_scoped(user=user, activate="false", group="")
        else:
            return not_authorized(request)

        response.content = users

    return response
Exemplo n.º 4
0
def setNameId(request, token):
    post = request.POST
    print(post)

    response = HttpResponse()
    if 'nameid' in post:
        name_id = post['nameid']
        admin = request.session['profile']['preferred_username']

        version = '{}'.format(IMPERSONATION_VERSION)
        if version == "1":
            client = AppsClient('https://' + OKTA_ORG, API_KEY,
                                IMPERSONATION_SAML_APP_ID)
            response.status_code = client.set_name_id(
                request.session['id_token']['sub'], name_id)
        if version == "2":

            u_client = UsersClient('https://' + OKTA_ORG, API_KEY)
            target = json.loads(u_client.list_user(name_id))
            target_profile = target["profile"]
            target_groups = json.loads(u_client.get_user_groups(target["id"]))
            groupsIds = []
            for g in target_groups:
                if g["type"] != 'BUILT_IN':
                    groupsIds.append(g["id"])

            now = datetime.datetime.now()
            new_login = "******" + now.strftime('%Y%m%d%H%M%S') + admin.split(
                "@")[0].replace(".", "") + "AS" + target_profile["login"]
            target_profile["login"] = new_login
            target_profile["email"] = new_login
            temp_user = {"profile": target_profile, "groupIds": groupsIds}
            u_client.create_user(user=temp_user, activate=True)

            u_client = UsersClient('https://' + IMPERSONATION_V2_ORG,
                                   IMPERSONATION_V2_ORG_API_KEY)
            users = u_client.list_user(admin)
            users = json.loads(users)
            if "id" in users:
                client = AppsClient('https://' + IMPERSONATION_V2_ORG,
                                    IMPERSONATION_V2_ORG_API_KEY,
                                    IMPERSONATION_V2_SAML_APP_ID)
                response.status_code = client.set_name_id(
                    users["id"], new_login)
                for key in list(request.session.keys()):
                    del request.session[key]
    return response