1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00

makes authentication work .. again

This commit is contained in:
Falk Werner 2019-04-14 18:44:55 +02:00
parent 2d70ba71c5
commit 043a0f18bc
2 changed files with 84 additions and 33 deletions

View File

@ -29,6 +29,14 @@ export class ConnectionView {
const authenticateBox = document.createElement("div"); const authenticateBox = document.createElement("div");
this.element.appendChild(authenticateBox); this.element.appendChild(authenticateBox);
const authLabel = document.createElement("span");
authLabel.textContent = "use authentication:";
authenticateBox.appendChild(authLabel);
this.authenticateCheckbox = document.createElement("input");
this.authenticateCheckbox.type = "checkbox";
authenticateBox.appendChild(this.authenticateCheckbox);
const usernameLabel = document.createElement("span"); const usernameLabel = document.createElement("span");
usernameLabel.textContent = "user:"; usernameLabel.textContent = "user:";
authenticateBox.appendChild(usernameLabel); authenticateBox.appendChild(usernameLabel);
@ -46,12 +54,6 @@ export class ConnectionView {
this.passwordTextbox.type = "password"; this.passwordTextbox.type = "password";
this.passwordTextbox.value = "secret"; this.passwordTextbox.value = "secret";
authenticateBox.appendChild(this.passwordTextbox); authenticateBox.appendChild(this.passwordTextbox);
this.authenticateButton = document.createElement("input");
this.authenticateButton.type = "button";
this.authenticateButton.value = "authenticate";
this.authenticateButton.addEventListener("click", () => { this._onAuthenticateButtonClicked(); });
authenticateBox.appendChild(this.authenticateButton);
} }
_onConnectButtonClicked() { _onConnectButtonClicked() {
@ -66,15 +68,22 @@ export class ConnectionView {
_onAuthenticateButtonClicked() { _onAuthenticateButtonClicked() {
if (this._client.isConnected()) { if (this._client.isConnected()) {
const username = this.usernameTextbox.value;
const password = this.passwordTextbox.value;
this._client.authenticate("username", { username, password });
} }
} }
_onConnectionOpened() { _onConnectionOpened() {
if (this.authenticateCheckbox.checked) {
console.log("authenticate");
const username = this.usernameTextbox.value;
const password = this.passwordTextbox.value;
const promise = this._client.authenticate("username", { username, password });
promise.then(() => { this._client.addProvider("test", this._provider) });
} else {
this._client.addProvider("test", this._provider); this._client.addProvider("test", this._provider);
}
this.connectButton.value = "disconnect"; this.connectButton.value = "disconnect";
} }

View File

@ -5,6 +5,8 @@ export class Client {
constructor(provider) { constructor(provider) {
this._provider = { }; this._provider = { };
this._pendingRequests = {};
this._id = 0;
this._ws = null; this._ws = null;
this.onopen = () => { }; this.onopen = () => { };
this.onclose = () => { }; this.onclose = () => { };
@ -24,14 +26,20 @@ export class Client {
}; };
} }
authenticate(type, credentials) { _invokeRequest(method, params) {
const request = { const id = ++this._id;
"method": "authenticate", const request = {method, params, id};
"params": [type, credentials],
"id": 42
};
return new Promise((resolve, reject) => {
this._pendingRequests[id] = {resolve, reject};
this._ws.send(JSON.stringify(request)); this._ws.send(JSON.stringify(request));
})
}
authenticate(type, credentials) {
return this._invokeRequest("authenticate", [type, credentials]);
} }
addProvider(name, provider) { addProvider(name, provider) {
@ -56,28 +64,62 @@ export class Client {
return ((this._ws) && (this._ws.readyState === WebSocket.OPEN)); return ((this._ws) && (this._ws.readyState === WebSocket.OPEN));
} }
_isRequest(request) {
const method = request.method;
return (("string" === typeof(method)) && (request.hasOwnProperty("params")));
}
_isResponse(response) {
const id = response.id;
return (("number" === typeof(id)) && (response.hasOwnProperty("result") || response.hasOwnProperty("error")));
}
_removePendingRequest(id) {
let result = null;
if (this._pendingRequests.hasOwnProperty(id)) {
result = this._pendingRequests[id];
delete this._pendingRequests[id];
}
return result;
}
_onmessage(message) { _onmessage(message) {
try { try {
const request = JSON.parse(message.data); const data = JSON.parse(message.data);
const method = request.method;
const id = request.id;
const params = request.params;
if ("string" !== typeof(method)) { if (this._isRequest(data)) {
throw new Error("parse error: missing field: \"method\""); const method = data.method;
} const id = data.id;
const params = data.params;
if (!params) { if ("number" === typeof(id)) {
throw new Error("parse error: missing field: \"params\"");
}
if ("number" === typeof(request.id)) {
this._invoke(method, params, id); this._invoke(method, params, id);
} }
else { else {
this._notify(method, params); this._notify(method, params);
} }
} }
else if (this._isResponse(data)) {
const id = data.id;
const result = data.result;
const error = data.error;
const request = this._removePendingRequest(id);
if (request) {
if (result) {
request.resolve(result);
}
else {
request.reject(error);
}
}
}
}
catch (ex) { catch (ex) {
// swallow // swallow
} }