forked from gabrielsroka/gabrielsroka.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddSwaApp.js
66 lines (63 loc) · 2.61 KB
/
addSwaApp.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
/*
Add a Custom SWA app and assign it to me.
Setup:
Drag this to the bookmark toolbar:
javascript:(function(){document.body.appendChild(document.createElement("script")).src="https://gabrielsroka.github.io/addSwaApp.js";})();
Usage:
1. Login to Okta Admin.
2. Click the bookmark from your toolbar.
*/
(function () {
var popup = createPopup("Add SWA App");
var form = $("<form><table><tr><td>Label<td><input class=label style='width: 300px'>" +
"<tr><td>Login URL<td><input class=loginUrl value='https://LOGIN.oktapreview.com' style='width: 300px'></table>" +
"<button type=submit>Add</button></form>").appendTo(popup);
form.submit(async (event) => {
event.preventDefault();
popup.html("Getting user...");
var me = await $.get("/api/v1/users/me");
var app = {
label: form.find("input.label").val(),
settings: {
signOn: {
loginUrl: form.find("input.loginUrl").val()
}
},
signOnMode: "AUTO_LOGIN",
visibility: {
autoSubmitToolbar: false
},
credentials: {
revealPassword: true
}
};
popup.html("Adding app...");
// https://developer.okta.com/docs/reference/api/apps/#add-custom-swa-application
app = await postJson({
url: "/api/v1/apps",
data: app
});
var appUser = {
id: me.id,
scope: "USER"
};
popup.html("Assigning user to app...");
// https://developer.okta.com/docs/reference/api/apps/#assign-user-to-application-for-sso
await postJson({
url: "/api/v1/apps/" + app.id + "/users",
data: appUser
});
popup.html(`Added app <a href='/admin/app/${app.name}/instance/${app.id}/'>${app.label}</a>.`);
});
function postJson(settings) {
settings.contentType = "application/json";
settings.data = JSON.stringify(settings.data);
return $.post(settings);
}
function createPopup(title) {
var popup = $(`<div style='position: absolute; z-index: 1000; left: 4px; top: 4px; background-color: white; padding: 8px; border: 1px solid #ddd;'>` +
`${title}<div style='display: block; float: right;'><a href='https://gabrielsroka.github.io' target='_blank' rel='noopener'> ? </a> ` +
`<a onclick='document.body.removeChild(this.parentNode.parentNode)' style='cursor: pointer'> X </a></div><br><br></div>`).appendTo(document.body);
return $("<div></div>").appendTo(popup);
}
})();