Skip to content

Commit

Permalink
test book update auth method
Browse files Browse the repository at this point in the history
  • Loading branch information
Landeers committed Oct 24, 2023
1 parent f80c359 commit 9f425ed
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 177 deletions.
97 changes: 97 additions & 0 deletions misc/test_book/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// LOGIN

function login() {
const email = document.getElementById("auth-email").value;
const key = document.getElementById("auth-key").value;
const organization = document.getElementById("auth-organization-id").value;
const encryptedKey = document.getElementById("auth-encrypted-key").value;
const http = new XMLHttpRequest();
http.open("POST", "http://localhost:5775/auth");
http.setRequestHeader("Content-type", "application/json");
http.send(JSON.stringify({
email,
key,
organization,
encrypted_key: encryptedKey,
}));
http.onreadystatechange = (e) => {
document.getElementById("auth-result").innerHTML = getHttpResult(http);
if (http.status === 200) {
document.getElementById("auth-info").innerHTML = `${email} | ${organization}`;
tokenSession = JSON.parse(http.responseText).token;
listWorkspaces();
}
}
}

const storageKey = "resana-secure-release-tests-key-auth";

function saveAuth() {
const auth = {};
let storage = null;
auth['email'] = document.getElementById("auth-email").value;
auth['key'] = document.getElementById("auth-key").value;
auth['organization'] = document.getElementById("auth-organization-id").value;
auth['encryptedKey'] = document.getElementById("auth-encrypted-key").value;
auth['password'] = document.getElementById("auth-password").value;
storage = localStorage.getItem(storageKey) || "[]";
storage = JSON.parse(storage);
storage.push(auth);
localStorage.setItem(storageKey, JSON.stringify(storage));
}

function listAuth() {
let saves = [];
const storage = localStorage.getItem(storageKey);
saves = JSON.parse(storage) || [];
const modal = document.getElementById("save-modal");
modal.style.display = "block";
const listElem = document.getElementById("saves-list");
listElem.innerHTML = "";
saves.forEach((save, index) => {
listElem.innerHTML += `<li onclick="loadAuth(${index})">${save.email} | ${save.key} | ${save.organization}</li>`;
});
}

function loadAuth(index) {
const storage = localStorage.getItem(storageKey);
const saves = JSON.parse(storage) || [];
document.getElementById("auth-email").value = saves[index].email;
document.getElementById("auth-key").value = saves[index].key;
document.getElementById("auth-organization-id").value = saves[index].organization;
document.getElementById("auth-encrypted-key").value = saves[index].encryptedKey;
document.getElementById("auth-password").value = saves[index].password;
const modal = document.getElementById("save-modal");
modal.style.display = "none";
}

function closeAuthModal() {
const elem = document.getElementById("save-modal");
elem.style.display = "none";
}


// LOGOUT
function deconnect(force = false) {
const http = new XMLHttpRequest();
http.open("DELETE", "http://localhost:5775/auth");
http.setRequestHeader("Authorization", `bearer ${tokenSession}`);
http.send();
http.onreadystatechange = (e) => {
document.getElementById("auth-info").innerHTML = "";
document.getElementById("logout-result").innerHTML = getHttpResult(http);
}
openAccount(force);
}

function deconnectAll(force = false) {
const http = new XMLHttpRequest();
http.open("DELETE", "http://localhost:5775/auth/all");
http.setRequestHeader("Authorization", `bearer ${tokenSession}`);
http.send();
http.onreadystatechange = (e) => {
document.getElementById("auth-info").innerHTML = "";
document.getElementById("logout-result").innerHTML = getHttpResult(http);
}
openAccount(force);
}
47 changes: 15 additions & 32 deletions misc/test_book/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,38 +183,21 @@ <h2>Auth</h2>
<li>Spammer le bouton "Se connecter" (avec mauvaise key) pour vérifier la protection anti-bruteforce</li>
</ul>

<div>
Key
<label class="switch">
<input id="switch-auth" type="checkbox" onchange="switchAuth()">
<span class="slider round"></span>
</label>
User password
</div>

<div id="key-auth">
<label for="key-email">Email</label>
<input id="key-email" value="[email protected]">
<br>
<label for="key">Key</label>
<input id="key" value="P@ssw0rd">
<br>
<label for="key-organization-id">ID organisation</label>
<input id="key-organization-id" value="test_release_">
</div>
<div id="password-auth">
<label for="password-email">Email</label>
<input id="password-email" value="[email protected]">
<br>
<label for="password">Mot de passe</label>
<input id="password" value="P@ssw0rd">
<br>
<label for="encrypted-key">Encrypted key</label>
<input id="encrypted-key" value="">
<br>
<label for="password-organization-id">ID organisation</label>
<input id="password-organization-id" value="test_release_">
</div>
<label for="auth-email">Email</label>
<input id="auth-email" value="[email protected]">
<br>
<label for="auth-key">Key</label>
<input id="auth-key" value="P@ssw0rd">
<br>
<label for="auth-organization-id">ID organisation</label>
<input id="auth-organization-id" value="test_release_">
<br>
<label for="auth-encrypted-key">Encrypted key</label>
<input id="auth-encrypted-key" value="">
<br>
<label for="auth-password">Password</label>
<input id="auth-password" value="">
<br>
<button onclick="login()">Se connecter</button>
<button onclick="saveAuth()">Sauvegarder</button>
<button onclick="listAuth()">Charger</button>
Expand Down
146 changes: 1 addition & 145 deletions misc/test_book/test_book.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,59 +78,6 @@ function boostrap() {

// AUTH
let tokenSession = null;
let authMethod = "password";

function switchAuth() {
authMethod = authMethod == "key" ? "password" : "key";
const keyAuth = document.getElementById("key-auth");
const passwordAuth = document.getElementById("password-auth");
if (authMethod === "key") {
keyAuth.style.display = "block";
passwordAuth.style.display = "none";
} else {
keyAuth.style.display = "none";
passwordAuth.style.display = "block";
}
}

function login() {
const keyEmail = document.getElementById("key-email").value;
const key = document.getElementById("key").value;
const keyOrganization = document.getElementById("key-organization-id").value;
const PasswordEmail = document.getElementById("password-email").value;
const password = document.getElementById("password").value;
const encryptedKey = document.getElementById("encrypted-key").value;
const PasswordOrganization = document.getElementById("password-organization-id").value;
const http = new XMLHttpRequest();
http.open("POST", "http://localhost:5775/auth");
http.setRequestHeader("Content-type", "application/json");
if (authMethod === "key") {
http.send(JSON.stringify({
email: keyEmail,
key,
organization: keyOrganization
}));
} else {
http.send(JSON.stringify({
email: PasswordEmail,
user_password: password,
encrypted_key: encryptedKey,
organization: PasswordOrganization
}));
}
http.onreadystatechange = (e) => {
document.getElementById("auth-result").innerHTML = getHttpResult(http);
if (http.status === 200) {
if (authMethod === "key") {
document.getElementById("auth-info").innerHTML = `${keyEmail} | ${keyOrganization}`;
} else {
document.getElementById("auth-info").innerHTML = `${PasswordEmail} | ${PasswordOrganization}`;
}
tokenSession = JSON.parse(http.responseText).token;
listWorkspaces();
}
}
}

const saltDerive2 = new Uint8Array("122,205,180,252,110,57,134,101,147,170,189,150,191,228,84,206".split(","));

Expand Down Expand Up @@ -962,97 +909,6 @@ function shamirGetOthers() {
}


// LOGOUT
function deconnect(force = false) {
const http = new XMLHttpRequest();
http.open("DELETE", "http://localhost:5775/auth");
http.setRequestHeader("Authorization", `bearer ${tokenSession}`);
http.send();
http.onreadystatechange = (e) => {
document.getElementById("auth-info").innerHTML = "";
document.getElementById("logout-result").innerHTML = getHttpResult(http);
}
openAccount(force);
}

function deconnectAll(force = false) {
const http = new XMLHttpRequest();
http.open("DELETE", "http://localhost:5775/auth/all");
http.setRequestHeader("Authorization", `bearer ${tokenSession}`);
http.send();
http.onreadystatechange = (e) => {
document.getElementById("auth-info").innerHTML = "";
document.getElementById("logout-result").innerHTML = getHttpResult(http);
}
openAccount(force);
}

const keyAuthStorageKey = "resana-secure-release-tests-key-auth";
const passwordAuthStorageKey = "resana-secure-release-tests-password-auth";

function saveAuth() {
const auth = {};
let storage = null;
if (authMethod === "key") {
auth['email'] = document.getElementById("key-email").value;
auth['key'] = document.getElementById("key").value;
auth['organization'] = document.getElementById("key-organization-id").value;
} else {
auth['email'] = document.getElementById("password-email").value;
auth['password'] = document.getElementById("password").value;
auth['encryptedKey'] = document.getElementById("encrypted-key").value;
auth['organization'] = document.getElementById("password-organization-id").value;
}
const storageKey = authMethod === "key" ? keyAuthStorageKey : passwordAuthStorageKey;
storage = localStorage.getItem(storageKey) || "[]";
storage = JSON.parse(storage);
storage.push(auth);
localStorage.setItem(storageKey, JSON.stringify(storage));
}

function listAuth() {
const storageKey = authMethod === "key" ? keyAuthStorageKey : passwordAuthStorageKey;
let saves = [];
const storage = localStorage.getItem(storageKey);
saves = JSON.parse(storage) || [];
const modal = document.getElementById("save-modal");
modal.style.display = "block";
const listElem = document.getElementById("saves-list");
listElem.innerHTML = "";
saves.forEach((save, index) => {
if (authMethod === "key") {
listElem.innerHTML += `<li onclick="loadAuth(${index})">${save.email} | ${save.key} | ${save.organization}</li>`;
} else {
listElem.innerHTML += `<li onclick="loadAuth(${index})">${save.email} | ${save.password} | ${save.organization}</li>`;
}
});
}

function loadAuth(index) {
const storageKey = authMethod === "key" ? keyAuthStorageKey : passwordAuthStorageKey;
const storage = localStorage.getItem(storageKey);
const saves = JSON.parse(storage) || [];
if (authMethod === "key") {
document.getElementById("key-email").value = saves[index].email;
document.getElementById("key").value = saves[index].key;
document.getElementById("key-organization-id").value = saves[index].organization;
} else {
document.getElementById("password-email").value = saves[index].email;
document.getElementById("password").value = saves[index].password;
document.getElementById("encrypted-key").value = saves[index].encryptedKey;
document.getElementById("password-organization-id").value = saves[index].organization;
}
const modal = document.getElementById("save-modal");
modal.style.display = "none";
}

function closeAuthModal() {
const elem = document.getElementById("save-modal");
elem.style.display = "none";
}

// RESULT

(function() {
switchAuth();
})();
(function() {})();

0 comments on commit 9f425ed

Please sign in to comment.