forked from gabrielsroka/gabrielsroka.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportObjects.js
161 lines (161 loc) · 7.95 KB
/
exportObjects.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
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
161
(function () {
var total;
var objectType;
var results;
var logger;
var groups;
var lines;
var query = "";
function commatize(...fields) {
return fields.map(field => `"${field}"`).join(',');
}
if (location.pathname == "/admin/users") {
// see also Reports > Reports, Okta Password Health: https://ORG.oktapreview.com/api/v1/users?format=csv
//query = "?search=profile.secretPasscode pr";
getObjects("Users", "/api/v1/users" + query, "id,firstName,lastName,login,email,credentialType", function (user) {
return commatize(user.id, user.profile.firstName, user.profile.lastName, user.profile.login, user.profile.email, user.credentials.provider.type);
});
} else if (location.pathname == "/admin/groups") {
getObjects("Groups", "/api/v1/groups", "id,name,description,type", function (group) {
return commatize(group.id, group.profile.name, group.profile.description || "", group.type);
});
} else if (location.pathname == "/admin/apps/active") {
getObjects("Apps", "/api/v1/apps", "id,label,name", function (app) {
return commatize(app.id, app.label, app.name);
});
} else if (location.pathname == "/report/system_log_2") {
query = "?since=2018-02-01T23%3A17%3A07Z&until=2018-02-03T05%3A24%3A18Z&limit=20&" +
"filter=target.id+eq+%220oaf65hlg1DbLxkhp0x7%22+and+target.type+eq+%22AppInstance%22+and+outcome.result+eq+%22FAILURE%22+and+legacyEventType+eq+%22app.rich_client.multiple_accounts_found%22";
getObjects("Logs", "/api/v1/logs" + query, "userName", function (event) {
return event.debugContext.debugData.userName;
});
} else {
var appid = getAppId();
if (appid) {
results = createDiv("Export");
var a = results.appendChild(document.createElement("a"));
a.innerHTML = "Export App Groups";
a.onclick = function () {
document.body.removeChild(results.parentNode);
getObjects("App Groups", "/api/v1/apps/" + appid + "/groups", "id,licenses,roles", function (appgroup) {
callAPI("/api/v1/groups/" + appgroup.id, function () {
var group = JSON.parse(this.responseText);
groups.push(group.profile.name + "," + (appgroup.profile.licenses ? appgroup.profile.licenses.join(";") : "") + "," +
(appgroup.profile.roles ? appgroup.profile.roles.join(";") : ""));
if (groups.length == total) {
console.log("name,licenses,roles");
for (var g = 0; g < groups.length; g++) {
console.log("," + groups[g]);
}
}
});
return commatize(appgroup.id, appgroup.profile.licenses ? appgroup.profile.licenses.join(";") : "",
appgroup.profile.roles ? appgroup.profile.roles.join(";") : "");
});
};
results.appendChild(document.createElement("br"));
a = results.appendChild(document.createElement("a"));
a.innerHTML = "Export App Users";
a.onclick = function () {
document.body.removeChild(results.parentNode);
getObjects("App Users", "/api/v1/apps/" + appid + "/users", "id,userName,scope", function (appuser) {
return commatize(appuser.id, appuser.credentials ? appuser.credentials.userName : "", appuser.scope);
});
};
} else {
results = createDiv("Export");
results.innerHTML = "<br>Error. Go to one of these:<br><br>" +
"<a href='/admin/users'>Directory > People</a><br>" +
"<a href='/admin/groups'>Directory > Groups</a><br>" +
"<a href='/admin/people/directories'>Directory > Directory Integrations</a> and click on a Directory<br>" +
"<a href='/admin/apps/active'>Applications > Applications</a> and click on an App<br>" +
"<a href='/admin/apps/active'>Applications > Applications</a> to export Apps<br>";
}
}
function getObjects(title, path, header, logCallback) {
total = 0;
objectType = title;
results = createDiv(title);
results.innerHTML = "Loading ...";
logger = logCallback;
lines = [header];
groups = [];
callAPI(path, exportObjects);
}
function exportObjects() {
if (this.responseText) {
var objects = JSON.parse(this.responseText);
for (var i = 0; i < objects.length; i++) {
var object = logger(objects[i]);
if (object) lines.push(object);
}
total += objects.length;
results.innerHTML = total + " " + objectType + "...";
var links = getLinks(this.getResponseHeader("Link"));
if (links && links.next) {
var nextUrl = new URL(links.next); // links.next is an absolute URL; we need a relative URL.
var path = nextUrl.pathname + nextUrl.search;
if (this.getResponseHeader("X-Rate-Limit-Remaining") && this.getResponseHeader("X-Rate-Limit-Remaining") < 10) {
var interval = setInterval(() => {
results.innerHTML += "<br>Sleeping...";
if ((new Date()).getTime() / 1000 > this.getResponseHeader("X-Rate-Limit-Reset")) {
clearInterval(interval);
callAPI(path, exportObjects);
}
}, 1000);
} else {
callAPI(path, exportObjects);
}
} else {
results.innerHTML = total + " " + objectType + ". Done.";
var a = results.appendChild(document.createElement("a"));
a.href = URL.createObjectURL(new Blob([lines.join("\n")], {type: 'text/csv'}));
// a.href = "data:application/csv;charset=utf-8," + encodeURIComponent(lines.join("\n"));
var date = (new Date()).toISOString().replace(/T/, " ").replace(/:/g, "-").substr(0, 19);
a.download = "Export " + objectType + " " + date + ".csv";
a.click();
}
}
}
function callAPI(path, onload) {
var request = new XMLHttpRequest();
request.open("GET", path);
request.setRequestHeader("Content-Type", "application/json");
request.setRequestHeader("Accept", "application/json");
request.onload = onload;
request.onprogress = function (event) {
results.innerHTML = event.loaded + " bytes loaded.";
};
request.send();
}
function getLinks(headers) {
if (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 == 6 || 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><br><br>";
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"));
}
})();