lenticular_cloud2/browser_app/index.js

152 lines
4.0 KiB
JavaScript

import 'jquery';
import 'bootstrap';
import 'jquery-form'
import {ConfirmDialog, Dialog} from './confirm-modal.js';
jQuery = window.$ = window.jQuery = require('jquery');
var cbor = require('cbor-web');
var forge = require('node-forge');
var QRCode = require("qrcode-svg");
var pki = require('node-forge/lib/pki');
var asn1 = require('node-forge/lib/asn1');
var pkcs12 = require('node-forge/lib/pkcs12');
var util = require('node-forge/lib/util');
import SimpleFormSubmit from "simple-form-submit";
import {startRegistration, startAuthentication} from '@simplewebauthn/browser';
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
window.CBOR = cbor;
/*
Convert an ArrayBuffer into a string
from https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
*/
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function randBase32() {
// src: https://en.wikipedia.org/wiki/Base32 RFC4648
const alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z','2', '3', '4', '5', '6', '7'];
var result = '';
var buf = new Uint8Array(1);
for ( var i = 0; i < 16; i++ ) {
window.crypto.getRandomValues(buf);
var rand_val = buf[0] & 31;
result += alphabet[rand_val];
}
return result;
}
window.ConfirmDialog = ConfirmDialog;
window.Dialog = Dialog;
window.$(document).ready(function () {
$('#sidebarCollapse').onclick = function () {
$('nav.sidebar').classList.toggle('d-none');
};
});
window.admin = {
registration: {
delete: function(href, username) {
var dialog = new ConfirmDialog('Reject user registration', `Are you sure to reject the registration request from "${username}"?`);
dialog.show().then(()=>{
fetch(href, {
method: 'DELETE'
});
});
return false;
},
accept: function(href, username) {
var dialog = new ConfirmDialog('Accept user registration', `Are you sure to accept the registration request from "${username}"?`);
dialog.show().then(()=>{
fetch(href, {
method: 'PUT'
}).then(()=>{
location.reload();
});
});
return false;
}
}
};
window.auth = {
sign_up: {
submit: function(form) {
SimpleFormSubmit.submitForm(form.action, form)
.then(response =>{
response.json().then(data => {
if (data.errors) {
var msg ='<ul>';
for( var field in data.errors) {
msg += `<li>${field}: ${data.errors[field]}</li>`;
}
msg += '</ul>';
new Dialog('Registration Error', `Error Happend: ${msg}`).show()
} else {
new Dialog('Registration successfully', 'Wait until an administrator has aproved your account').show();
}
});
});
return false;
}
}
};
window.auth_passkey = {
sign_up: async function(options) {
const resp = await startRegistration(options);
return resp;
},
sign_in: async function(options) {
const resp = await startAuthentication(options);
return resp;
},
}
window.password_change= {
init: function(){
var form = $('form');
form.onsubmit = function () {
SimpleFormSubmit.submitForm(form.action, form)
.then(response =>{
response.json().then(function(data) {
if (data.errors) {
var msg ='<ul>';
for( var field in data.errors) {
msg += `<li>${field}: ${data.errors[field]}</li>`;
}
msg += '</ul>';
new Dialog('Password change Error', `Error Happend: ${msg}`).show()
} else {
new Dialog('Password changed', 'Password changed successfully!').show();
}
});
}).error(error =>{
new Dialog('Password change Error', `Error Happend: ${msg}`).show()
});
return false;
}
}
}
window.oauth2_token = {
revoke: function(href, id){
var dialog = new ConfirmDialog('Revoke client tokens', `Are you sure to revoke all tokens from client "${id}"?`);
dialog.show().then(()=>{
fetch(href, {
method: 'DELETE'
});
});
return false;
}
}