diff --git a/activate_staged_users.py b/activate_staged_users.py new file mode 100644 index 0000000..ba995d2 --- /dev/null +++ b/activate_staged_users.py @@ -0,0 +1,127 @@ +import requests +import json +import re +import sys +import csv + +orgName = "" +apiKey = "" + +api_token = "SSWS "+ apiKey + +headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token} + +def GetPaginatedResponse(url): + + response = requests.request("GET", url, headers=headers) + + returnResponseList = [] + + responseJSON = json.dumps(response.json()) + + responseList = json.loads(responseJSON) + + returnResponseList = returnResponseList + responseList + + + if "errorCode" in responseJSON: + + print "\nYou encountered following Error: \n" + print responseJSON + print "\n" + + return "Error" + + else: + + headerLink= response.headers["Link"] + + count = 1 + + while str(headerLink).find("rel=\"next\"") > -1: + + linkItems = str(headerLink).split(",") + + nextCursorLink = "" + for link in linkItems: + + if str(link).find("rel=\"next\"") > -1: + nextCursorLink = str(link) + + + nextLink = str(nextCursorLink.split(";")[0]).strip() + nextLink = nextLink[1:] + nextLink = nextLink[:-1] + + url = nextLink + + print "\nCalling Paginated Url " + str(url) + " " + str(count) + " \n" + response = requests.request("GET", url, headers=headers) + + responseJSON = json.dumps(response.json()) + + responseList = json.loads(responseJSON) + + returnResponseList = returnResponseList + responseList + + headerLink= response.headers["Link"] + + count += 1 + + + returnJSON = json.dumps(returnResponseList) + + return returnResponseList + +def ActivateUser(url): + + response = requests.post(url, headers=headers) + + responseJSON = response.json() + + if "errorCode" in responseJSON: + print "\nYou encountered following Error: \n" + print responseJSON + print "\n" + + return "Error" + + else: + + return responseJSON + + + +def UpdateAssignment(): + + + getStagedUsersUrl = "https://"+orgName+".com/api/v1/users?filter=status eq \"STAGED\"" + + stagedUsers = GetPaginatedResponse(getStagedUsersUrl) + + count = 1 + + for stagedUser in stagedUsers: + + userId = str(stagedUser[u"id"]) + + print "Activating " + stagedUser[u"profile"][u"firstName"] + " " + stagedUser[u"profile"][u"lastName"] + stagedUser[u"profile"][u"login"] + " " + str(count) + + acticvateUserUrl = "https://"+orgName+".com/api/v1/users/" + userId + "/lifecycle/activate?sendEmail=false" + + + activationResponse = ActivateUser(acticvateUserUrl) + + if "Error" != activationResponse: + + print stagedUser[u"profile"][u"firstName"] + " " + stagedUser[u"profile"][u"lastName"] + stagedUser[u"profile"][u"login"] + " ACTIVATED\n" + + count += 1 + + + + + +if __name__ == "__main__": + + UpdateAssignment() \ No newline at end of file diff --git a/create_random_users.py b/create_random_users.py new file mode 100755 index 0000000..4645d58 --- /dev/null +++ b/create_random_users.py @@ -0,0 +1,100 @@ +import requests +import json +import re +import sys +import csv + +orgName = "" +apiKey = "" + +def CreateUsers(): + + N = 3 + + firstName = "OktaSupp" + lastName = "TestUser" + + ## CSV file before user is created + + beforeCreation = open("User-Info-Before-Creation.csv", "wb") + beforeWriter = csv.writer(beforeCreation) + + beforeWriter.writerow(["firstName", "lastName", "email", "login"]) + + afterCreation = open("User-Info-After-Creation.csv", "wb") + afterWriter = csv.writer(afterCreation) + afterWriter.writerow(["id", "status", "email","Okta-Request-Id"]) + + for userNum in range(1, N): + + try: + + email = firstName.lower()+str(userNum)+lastName.lower()+str(userNum)+"@mailinator.com" + + ## First name of the user + print "\n Creating User " + email + "\n" + + #Write to CSV before creating + beforeWriter.writerow([firstName,lastName,email,email]) + + + user_info = {} + + user_info['profile'] = {} + user_info['credentials'] = {} + + user_info['profile'] ['firstName'] = firstName+str(userNum) + user_info['profile'] ['lastName'] = lastName+str(userNum) + user_info['profile'] ['email'] = email + user_info['profile'] ['login'] = email + + user_info['credentials'] ['password'] = {} + user_info['credentials'] ['recovery_question'] = {} + + user_info['credentials'] ['password']['value'] = "TestPass123$" + user_info['credentials']['recovery_question'] ['question'] = "Who's a major player" + user_info['credentials']['recovery_question'] ['answer'] = "Ev3ry1" + + user_info_json = json.dumps(user_info) + + url = "https://"+orgName+".com/api/v1/users" + + api_token = "SSWS "+ apiKey + headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token} + + print user_info + response = requests.post(url, data = user_info_json, headers = headers) + + responseJSON = response.json() + + ##Read headers + oktaRequestId= response.headers["X-Okta-Request-Id"] + + if "errorCode" in responseJSON: + + + print responseJSON + + else: + + userId = responseJSON["id"] + status = responseJSON["status"] + userEmail = responseJSON["profile"]["email"] + + + + #Write to CSV after creating + afterWriter.writerow([userId,status,email,oktaRequestId]) + + print "\n User " + email + " Created Successfully" + + except: + + print "Unexpected error:", sys.exc_info() + print "\n" + pass + +if __name__ == "__main__": + CreateUsers() + + diff --git a/get_all_active_users.py b/get_all_active_users.py new file mode 100644 index 0000000..7516620 --- /dev/null +++ b/get_all_active_users.py @@ -0,0 +1,104 @@ +import requests +import json +import re +import sys +import csv + + +orgName = "" + +apiKey = "" + +api_token = "SSWS "+ apiKey + +headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token} + +def GetPaginatedResponse(url): + + response = requests.request("GET", url, headers=headers) + + returnResponseList = [] + + responseJSON = json.dumps(response.json()) + + responseList = json.loads(responseJSON) + + returnResponseList = returnResponseList + responseList + + + if "errorCode" in responseJSON: + + print "\nYou encountered following Error: \n" + print responseJSON + print "\n" + + return "Error" + + else: + + headerLink= response.headers["Link"] + + while str(headerLink).find("rel=\"next\"") > -1: + + linkItems = str(headerLink).split(",") + + nextCursorLink = "" + for link in linkItems: + + if str(link).find("rel=\"next\"") > -1: + nextCursorLink = str(link) + + + nextLink = str(nextCursorLink.split(";")[0]).strip() + nextLink = nextLink[1:] + nextLink = nextLink[:-1] + + url = nextLink + + response = requests.request("GET", url, headers=headers) + + responseJSON = json.dumps(response.json()) + + responseList = json.loads(responseJSON) + + returnResponseList = returnResponseList + responseList + + headerLink= response.headers["Link"] + + + returnJSON = json.dumps(returnResponseList) + + return returnResponseList + + + +def DownloadSFUsers(): + + url = "https://"+orgName+".com/api/v1/users" + + responseJSON = GetPaginatedResponse(url) + + if responseJSON != "Error": + + + userFile = open("All-Users-In-Okta.csv", "wb") + + writer = csv.writer(userFile) + + writer.writerow(["firstName", "lastName", "email", "login"]) + + for user in responseJSON: + + firstName = user[u"profile"][u"firstName"] + lastName = user[u"profile"][u"lastName"] + email = user[u"profile"][u"email"] + login = user[u"profile"][u"login"] + + row = firstName+","+lastName+","+email+","+login + + writer.writerow([firstName,lastName,email,login]) + + +if __name__ == "__main__": + + DownloadSFUsers() \ No newline at end of file diff --git a/get_app_assignment_events.py b/get_app_assignment_events.py new file mode 100644 index 0000000..8fb1277 --- /dev/null +++ b/get_app_assignment_events.py @@ -0,0 +1,152 @@ +import requests +import json +import re +import sys +import csv + + +orgName = "" +apiKey = "" + + +api_token = "SSWS "+ apiKey +headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token} + +def GetPaginatedResponse(url): + + response = requests.request("GET", url, headers=headers) + + returnResponseList = [] + + responseJSON = json.dumps(response.json()) + + responseList = json.loads(responseJSON) + + returnResponseList = returnResponseList + responseList + + + if "errorCode" in responseJSON: + + print "\nYou encountered following Error: \n" + print responseJSON + print "\n" + + return "Error" + + else: + + headerLink= response.headers["Link"] + count = 1 + + while str(headerLink).find("rel=\"next\"") > -1: + + linkItems = str(headerLink).split(",") + + nextCursorLink = "" + for link in linkItems: + + if str(link).find("rel=\"next\"") > -1: + nextCursorLink = str(link) + + + nextLink = str(nextCursorLink.split(";")[0]).strip() + nextLink = nextLink[1:] + nextLink = nextLink[:-1] + + url = nextLink + + print "\nCalling Paginated Url " + str(url) + " " + str(count) + " \n" + response = requests.request("GET", url, headers=headers) + + responseJSON = json.dumps(response.json()) + + responseList = json.loads(responseJSON) + + returnResponseList = returnResponseList + responseList + + headerLink= response.headers["Link"] + + count += 1 + + + returnJSON = json.dumps(returnResponseList) + + return returnResponseList + + + + +def GetEvents(): + + eventsUrl = "https://"+orgName+".com/api/v1/events?filter=published gt \"2017-01-27T00:00:00.000Z\"" + + responseJSON = GetPaginatedResponse(eventsUrl) + + userFile = open("App-Access-Events.csv", "wb") + + print "\nAll Events Retrieved. Generating CSV...\n" + writer = csv.writer(userFile) + + writer.writerow(["Category", "Message", "RequestUri", "Admin", "AppUser", "App","Timestamp" "EVENT_JSON"]) + + if responseJSON != "Error": + + count = 0 + for event in responseJSON: + + categories = [] + categories = event[u"action"][u"categories"] + + if "Application Assignment" in categories: + + admin = "" + appUser = "" + app = "" + + + category = categories[categories.index("Application Assignment")] + + message = event[u"action"][u"message"] + + requestUri = event[u"action"][u"requestUri"] + + actors = event[u"actors"] + + targets = event[u"targets"] + + timestamp = event[u"published"] + + + for actor in actors: + + if actor[u"objectType"] == "User": + + if u"login" in actor: + + admin = actor[u"login"] + + + for target in targets: + + if target[u"objectType"] == "User": + + if u"login" in target: + + appUser = target[u"login"] + + + if target[u"objectType"] == "AppInstance": + + if u"displayName" in target: + + app = target[u"displayName"] + + + + if admin != "": + writer.writerow([category, message, requestUri, admin, appUser, app, timestamp, event]) + + +if __name__ == "__main__": + + GetEvents() \ No newline at end of file