Skip to content
/ kvmd Public

Commit b25f491

Browse files
committed
web: add basic UI for OAuth
1 parent 4c920fd commit b25f491

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

web/login/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<div id="login-box">
5252
<div id="login">
5353
<table>
54+
<tbody>
5455
<tr>
5556
<td><img class="svg-gray" id="login-logo" src="../share/svg/logo.svg" alt="&amp;pi;-kvm"></td>
5657
<td></td>
@@ -104,9 +105,11 @@
104105
<tr>
105106
<td></td>
106107
<td>
107-
<button class="key" id="login-button">Login</button>
108+
<button class="key login-button" id="login-button">Login</button>
108109
</td>
109110
</tr>
111+
</tbody>
112+
<tbody id="oauth-tbody"></tbody>
110113
</table>
111114
</div>
112115
</div>
@@ -118,4 +121,4 @@
118121
</li>
119122
</ul>
120123
</body>
121-
</html>
124+
</html>

web/share/css/login/login.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ img#login-logo {
4646
height: 30px;
4747
}
4848

49-
button#login-button {
50-
width: 100%;
49+
button.login-button {
50+
width: 100%;
51+
}
52+
53+
button.login-button-secondary {
54+
width: 100%;
5155
}
5256

5357
input[type="text"]#user-input,

web/share/js/login/main.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export function main() {
3232
if (checkBrowser(null, null)) {
3333
initWindowManager();
3434

35+
__oauthInit();
36+
3537
// Radio is a string container
3638
tools.radio.clickValue("expire-radio", tools.storage.get("login.expire", 0));
3739
tools.radio.setOnClick("expire-radio", function() {
@@ -51,6 +53,71 @@ export function main() {
5153
}
5254
}
5355

56+
function __oauthInit() {
57+
// tools.httpRequest("GET", "/api/auth/flow/oauth/check", null, function(http) {
58+
// if (http.status === 200) {
59+
// // add overall plugin health check (perhaps flesh out the /check endpoint itself first)
60+
// }
61+
// })
62+
63+
tools.httpRequest("GET", "/api/auth/flow/oauth/providers", null, function(http) {
64+
if (http.status === 200) {
65+
let oauthInfo = JSON.parse(http.responseText).result;
66+
if (!oauthInfo.enabled) {
67+
return;
68+
}
69+
if (Object.entries(oauthInfo.providers).length === 0) {
70+
// perhaps also draw a "fake button" (inactive + greyed-out) to indicate we have no providers available
71+
return;
72+
}
73+
74+
let buttons = (`
75+
<tr>
76+
<td colspan="2">
77+
<hr>
78+
</td>
79+
</tr>
80+
`);
81+
const buttonIds = [];
82+
83+
for (const [shortName, longName] of Object.entries(oauthInfo.providers)) {
84+
const { html, buttonId } = __oauthButton(shortName, longName);
85+
// if (oauthInfo.default === shortName) {
86+
// $(buttonId).classList.add("default");
87+
// } else {
88+
// $(buttonId).classList.add("secondary");
89+
// }
90+
buttons += html;
91+
buttonIds.push({ shortName, buttonId });
92+
}
93+
$("oauth-tbody").innerHTML = buttons;
94+
for (const { shortName, buttonId } of buttonIds) {
95+
tools.el.setOnClick($(buttonId), () => __oauthLogin(shortName));
96+
}
97+
}
98+
// else {
99+
// // add error handling, draw an error "fake button" (like inactive + red-tinted) in place of the oauth buttons
100+
// }
101+
});
102+
}
103+
104+
function __oauthButton(shortName, longName) {
105+
const html = (`
106+
<tr>
107+
<td></td>
108+
<td>
109+
<button class="key login-button-secondary" id="oauth-button-${shortName}">Login with ${longName}</button>
110+
</td>
111+
</tr>
112+
`);
113+
const buttonId = `oauth-button-${shortName}`;
114+
return { html, buttonId };
115+
}
116+
117+
function __oauthLogin(shortName) {
118+
tools.currentOpen(`/api/auth/flow/oauth/login/${shortName}`);
119+
}
120+
54121
function __login() {
55122
let e_user = encodeURIComponent($("user-input").value);
56123
if (e_user.length === 0) {

0 commit comments

Comments
 (0)