forked from gabrielsroka/gabrielsroka.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportAppUsers.js
79 lines (76 loc) · 2.89 KB
/
exportAppUsers.js
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
/*
This bookmarklet exports app users.
Setup:
1. Drag this to the bookmark toolbar:
javascript:(function(){document.body.appendChild(document.createElement("script")).src="https://gabrielsroka.github.io/exportAppUsers.js";})();
Usage:
1. In Okta Admin, go to Applications > Applications.
2. Click on an app.
3. Open the dev console (F12).
4. Click the bookmark from your toolbar.
5. When the export is done, the users are in the console. Save the results to a CSV file.
*/
(function () {
var appid = getAppId();
if (appid) {
console.clear();
var total = 0;
var results = createDiv("App Users");
callAPI("/apps/" + appid + "/users", showAppUsers);
} else {
alert("Error. Go to Applications > Applications and click on an app.");
}
function showAppUsers() {
if (this.responseText) {
var appusers = JSON.parse(this.responseText);
for (var i = 0; i < appusers.length; i++) {
console.log(appusers[i].credentials.userName);
}
total += appusers.length;
results.innerHTML = total + " app users.";
var links = getLinks(this.getResponseHeader("Link"));
if (links.next) {
var path = links.next.replace(/.*api.v1/, ""); // links.next is an absolute URL; we need a relative URL.
callAPI(path, showAppUsers);
} else {
results.innerHTML += " Done -- check the console for results.";
}
}
}
function callAPI(path, onload) {
var request = new XMLHttpRequest();
request.open("GET", "/api/v1" + path);
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Accept", "application/json");
request.onload = onload;
request.send();
}
function getLinks(headers) {
headers = headers.split(", ");
var links = {};
for (var i = 0; i < headers.length; i++) {
var matches = headers[i].match(/<(.*)>; rel="(.*)"/);
links[matches[2]] = matches[1];
}
return links;
}
function getAppId() {
var path = location.pathname;
var pathparts = path.split(/\//);
if (path.match(/admin\/app/) && pathparts.length == 7) {
return pathparts[5];
}
}
function createDiv(title) {
var div = document.body.appendChild(document.createElement("div"));
div.innerHTML = "<a onclick='document.body.removeChild(this.parentNode)'>" + title + " - close</a> " +
"<a href='https://gabrielsroka.github.io/' target='_blank'>?</a>";
div.style.position = "absolute";
div.style.zIndex = "1000";
div.style.left = "4px";
div.style.top = "4px";
div.style.backgroundColor = "white";
div.style.padding = "8px";
return div.appendChild(document.createElement("div"));
}
})();