-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
160 lines (132 loc) · 4.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from logging import exception
import typer
import requests
import os
import random
import json
import string
# Initialize Typer App
app = typer.Typer()
# Get Environment Variables
OKTA_ORG=os.environ.get('OKTA_ORG')
OKTA_TOKEN= "SSWS "+os.environ.get('OKTA_ADMIN_TOKEN')
OKTA_USER_BULK_PASSWORD=os.environ.get('OKTA_USER_BULK_PASSWORD') # H72bbuYYumlkPqw007
@app.command()
def create_user(firstname: str, lastname: str, email: str):
url = OKTA_ORG+"api/v1/users?activate=true"
payload = json.dumps({
"profile": {
"firstName": firstname,
"lastName": lastname,
"email": email,
"login": email
}
})
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': OKTA_TOKEN
}
try:
response = requests.request("POST", url, headers=headers, data=payload)
except requests.exceptions as e:
raise SystemExit(e)
if response.status_code == 200:
print ("User {} created successfully".format(email))
else:
print ("User Creation Unsuccesful.")
@app.command()
def create_bulk_users(number_of_users: int):
if number_of_users < 1:
raise SystemExit("No. of Users cant be 0 or negative")
else:
for user in range(number_of_users):
url = OKTA_ORG+"api/v1/users?activate=true"
mydomain = "@akshay.im"
firstname = ''.join((random.choice(string.ascii_lowercase) for x in range(8)))
lastname = ''.join((random.choice(string.ascii_lowercase) for x in range(8)))
email = firstname+mydomain
payload = json.dumps({
"profile": {
"firstName": firstname,
"lastName": lastname,
"email": email,
"login": email
},
"credentials": {
"password": {
"value": OKTA_USER_BULK_PASSWORD
}
}
})
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': OKTA_TOKEN
}
try:
response = requests.request("POST", url, headers=headers, data=payload)
except requests.exceptions as e:
raise SystemExit(e)
if response.status_code == 200:
print ("User {} created successfully \n".format(email))
else:
print ("User Creation Unsuccesful.")
@app.command()
def list_users():
url = OKTA_ORG+"api/v1/users/"
payload={}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': OKTA_TOKEN
}
try:
response = requests.request("GET", url, headers=headers, data=payload)
except requests.exceptions as e:
raise SystemExit(e)
jsonRes = response.json()
for x in range(len(jsonRes)):
print("FirstName:",jsonRes[x]['profile']['firstName'],"LastName:",\
jsonRes[x]['profile']['lastName'],\
"Username:",jsonRes[x]['profile']['login'], "Status:",jsonRes[x]['status'])
@app.command()
def delete_user(email: str):
# Get UserID from email:
find_user_url = OKTA_ORG+"api/v1/users/?q="+email+"&limit=1"
payload={}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': OKTA_TOKEN
}
try:
response = requests.request("GET", find_user_url, headers=headers, data=payload)
except requests.exceptions as e:
raise SystemExit(e)
jsonRes = response.json()
userid = jsonRes[0].get('id')
if userid:
url = OKTA_ORG+"api/v1/users/"+userid
# Deprovision User
try:
del_response = requests.request("DELETE", url, headers=headers, data=payload)
except requests.exceptions as e:
raise SystemExit(e)
print("User Deprovisioned Successfully")
#Delete User
try:
user_response = requests.request("GET", find_user_url, headers=headers, data=payload)
except requests.exceptions as e:
raise SystemExit(e)
# Maybe a better way to implement this can be to use some kind of polling for user status.
if user_response.status_code != 404:
try:
del_response = requests.request("DELETE", url, headers=headers, data=payload)
except requests.exceptions as e:
raise SystemExit(e)
print("User {} deleted successfully".format(email) )
else:
print("User {} deleted successfully".format(email) )
if __name__ == "__main__":
app()