chore: reconfig scripts to handle openid auth

This commit is contained in:
Jesús Pérez 2023-09-19 02:20:29 +01:00
parent a298f5ff4c
commit 8332d8ed14
4 changed files with 93 additions and 66 deletions

View File

@ -186,6 +186,9 @@ const otp_in = async () => {
return false;
}
};
const on_openid = (item) => {
location.href=`/openid/${item}`;
};
const restart = () => {
const main_form = document.getElementById('main-form');
const main_btns = document.getElementById('main-btns');

View File

@ -35,41 +35,41 @@ const get_otp_info = () => {
}
}
const check_value = async (name,value,elem) => {
if (login_messages.innerHTML!='') { login_messages.innerHTML=''; }
if (login_messages.innerHTML !== '') { login_messages.innerHTML=''; }
const elem_error = document.getElementById(`${name}-error`);
let res = await check_item(name,value);
if (elem_error) { elem_error.innerHTML = res}
if (res != '') {
if (res !== '') {
elem.focus();
return false;
}
return true;
};
const set_name = async (elem) => {
if (elem.value == '' ) {
if (elem.value === ' ' ) {
login_messages.innerHTML='Please include a valid name';
return false;
}
return await check_value('name',elem.value,elem);
};
const set_email = async (elem) => {
if (elem.value == '' || !elem.value.includes('@')) {
if (elem.value === '' || !elem.value.includes('@')) {
login_messages.innerHTML='Please include a valid email';
return false;
}
return await check_value('email',elem.value,elem);
};
const set_totp = (elem) => {
if (login_messages.innerHTML!='') { login_messages.innerHTML=''; }
if (login_messages.innerHTML !== '') { login_messages.innerHTML=''; }
};
const make_role_chips = (elem) => {
const arr_items = elem.value == '' ? [] : elem.value.split(',');
const roles_chips = document.getElementById('roles-chips');
if (roles_chips) {
roles_chips.innerHTML='';
if (arr_items.length == 0) { return; }
const make_chips = (elem, keys) => {
const arr_items = elem.value === '' ? [] : elem.value.split(',');
const elem_chips = document.getElementById(`${keys}-chips`);
if (elem_chips) {
elem_chips.innerHTML='';
if (arr_items.length === 0) { return; }
arr_items.forEach(name => {
if (name.replaceAll(' ','') !== '') {
if (name.replaceAll(' ','') !== '') {
const chip = document.createElement("span");
chip.id = `badge-${name}`;
const arr_classes = "inline-flex items-center px-2 py-1 mr-2 text-sm font-medium text-blue-800 bg-blue-100 rounded dark:bg-blue-900 dark:text-blue-300".split(" ");
@ -79,14 +79,14 @@ const make_role_chips = (elem) => {
} else {
chip.innerHTML = `${name} <button type="button" class="inline-flex items-center p-0.5 ml-2 text-sm text-blue-400 bg-transparent rounded-sm hover:bg-blue-200 hover:text-blue-900 dark:hover:bg-blue-800 dark:hover:text-blue-300" data-dismiss-target="#badge-${name}" aria-label="Remove">
<svg aria-hidden="true" class="w-3.5 h-3.5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
<span class="sr-only">Remove role</span></button> `;
<span class="sr-only">Remove ${keys}</span></button> `;
}
roles_chips.append(chip);
elem_chips.append(chip);
if (!elem.disabled) {
document.getElementById(`badge-${name}`).addEventListener('click', (ev) => {
ev.preventDefault();
chip.remove();
elem.value = arr_items.filter(it => it != name).join(',');
elem.value = arr_items.filter(it => it !== name).join(',');
});
}
}
@ -94,11 +94,15 @@ const make_role_chips = (elem) => {
}
};
const set_role = (elem) => {
if (login_messages.innerHTML!='') { login_messages.innerHTML=''; }
make_role_chips(elem);
if (login_messages.innerHTML !== '') { login_messages.innerHTML=''; }
make_chips(elem, 'role');
};
const set_openids = (elem) => {
if (login_messages.innerHTML !== '') { login_messages.innerHTML = ''; }
make_chips(elem, 'openids');
};
const set_items = (elem) => {
if (login_messages.innerHTML!='') { login_messages.innerHTML=''; }
if (login_messages.innerHTML !== '') { login_messages.innerHTML=''; }
};
const clean_password_messages = () => {
if (password_secure_bar) { password_secure_bar.style.width = '0%'; }
@ -107,16 +111,16 @@ const clean_password_messages = () => {
if (password_warning) { password_warning.innerHTML=''; }
if (password_error) { password_error.innerHTML=''; }
};
const set_password = async (elem) => {
if (login_messages.innerHTML!='') { login_messages.innerHTML=''; }
clean_password_messages();
const set_password = async (elem) => {
if (login_messages.innerHTML !== '') { login_messages.innerHTML=''; }
clean_password_messages();
const feedback = await check_item('password',elem.value);
if (password_secure_bar) { password_secure_bar.style.width = `${parseInt(feedback.score)*10}%`; }
if (password_secure_val) { password_secure_val.innerHTML=feedback.score; }
if (password_suggestions) { password_suggestions.innerHTML=feedback.suggestions; }
if (password_warning) { password_warning.innerHTML=feedback.warning; }
if (PASSWORD_SCORE && feedback.score < PASSWORD_SCORE) {
elem.focus();
if (PASSWORD_SCORE && feedback.score < PASSWORD_SCORE) {
elem.focus();
if (password_error) {
password_error.innerHTML = `Password under score value: ${PASSWORD_SCORE}`;
}
@ -136,7 +140,7 @@ const change_use_totp = (elem) => {
} else {
const otp_auth = document.getElementById('user-totp-inpt');
const otp_code = document.getElementById('user-totp-code')
if (otp_auth && otp_code && otp_code.value != 0 ) {
if (otp_auth && otp_code && otp_code.value !== 0 ) {
otp_auth.parentElement.classList.remove('hidden');
}
}
@ -155,7 +159,7 @@ const reset_totp = (hide_parent) => {
const totp_recovery_message = document.getElementById('totp-recovery-message');
if (totp_recovery_message) { totp_recovery_message.classList.remove('hidden')}
if (copy_code) { copy_code.classList.add('hidden') }
if (otp_auth) {
if (otp_auth) {
otp_auth.value = '';
if (hide_parent) { otp_auth.parentElement.classList.add('hidden') }
}
@ -170,8 +174,8 @@ const set_theme = (elem) => {
const sel_theme = elem.value;
const element = document.getElementsByTagName('html')[0];
const curr_theme = element.classList.contains('dark') ? 'dark': 'light';
if (curr_theme != sel_theme) {
if (sel_theme != 'dark' && element.classList.contains('dark')) {
if (curr_theme !== sel_theme) {
if (sel_theme !== 'dark' && element.classList.contains('dark')) {
element.classList.remove('dark');
} else {
element.classList.add('dark');
@ -180,9 +184,9 @@ const set_theme = (elem) => {
}
};
const copy_otp_code = (elem) => {
const content = document.getElementById('user-totp-code').value || '';
if (content == '') { return }
const toast_target = document.getElementById('toast-copy-totp-code');
const content = document.getElementById('user-totp-code').value || '';
if (content === '') { return }
const toast_target = document.getElementById('toast-copy-totp-code');
navigator.clipboard.writeText(content).then(
() => {
if (toast_target) {
@ -194,7 +198,7 @@ const copy_otp_code = (elem) => {
},
);
};
const check_item = async (name, value) => {
const check_item = async (name, value) => {
if (CHECK_URL) {
const data = {
name,
@ -209,14 +213,14 @@ const check_item = async (name, value) => {
});
login_messages.innerHTML='';
if (response.ok && response.status === 200 ) {
if (name == 'password') {
if (name === 'password') {
const text = await response.text();
const arr_text = text.split('|');
return {
score: arr_text[0] || 0,
suggestions: arr_text[1].replace('\n','<br>') || '',
warning: arr_text[2] || '',
};
};
} else {
return '';
}
@ -225,21 +229,21 @@ const check_item = async (name, value) => {
}
}
};
const log_in = async () => {
const log_in = async () => {
login_messages.innerHTML='';
const elem_name = document.getElementById('user-name-inpt');
if (elem_name) {
if (elem_name.value == '') {
if (elem_name.value === '') {
login_messages.innerHTML='Enter a name';
return;
}
const data_name = elem_name.dataset.val;
if (data_name != elem_name.value) { if (! await set_name(elem_name)) { return; } }
if (data_name !== elem_name.value) { if (! await set_name(elem_name)) { return; } }
}
const name = elem_name ? elem_name.value : '';
const elem_password = document.getElementById('user-password-inpt');
if (elem_password) {
if (elem_password.value == '') {
if (elem_password.value === '') {
login_messages.innerHTML='Please include a password';
return;
}
@ -254,14 +258,14 @@ const log_in = async () => {
otp_code = elem_otp_code.value;
otp_url = document.getElementById('user-totp-url').value || ''
otp_auth = document.getElementById('user-totp-inpt').value || '';
if (otp_code != '') {
if (otp_auth == '' && totp_mode == "mandatory") {
if (otp_code !== '') {
if (otp_auth === '' && totp_mode === "mandatory") {
login_messages.innerHTML='Please enter a valid TOTP code';
return;
}
if (totp_mode == 'mandatory' || document.getElementById('use-totp').checked) {
if (totp_mode === 'mandatory' || document.getElementById('use-totp').checked) {
const numbers = /^[0-9]+$/;
if(! otp_auth.match(numbers) || otp_auth.length != totp_digits ) {
if(! otp_auth.match(numbers) || otp_auth.length !== totp_digits ) {
login_messages.innerHTML='Please enter a valid TOTP code';
return;
}
@ -271,6 +275,9 @@ const log_in = async () => {
const elem_roles = document.getElementById('user-roles-inpt');
var roles = '';
if (elem_roles) { roles = elem_roles.value; }
const elem_openids = document.getElementById('user-openids-inpt');
var openids = '';
if (elem_openids) { openids = elem_openids.value; }
const elem_fullname= document.getElementById('user-fullname-inpt');
var fullname = '';
if (elem_fullname) { fullname = elem_fullname.value; }
@ -280,20 +287,20 @@ const log_in = async () => {
const elem_email = document.getElementById('user-email-inpt');
if (elem_email) {
const data_email = elem_email.dataset.val;
if (data_email != elem_email.value) { if (! await set_email(elem_email)) { return; } }
if (data_email !== elem_email.value) { if (! await set_email(elem_email)) { return; } }
}
const email = elem_email ? elem_email.value : '';
const elem_theme = document.getElementById('theme-select');
var theme='';
var items={};
if (elem_theme){
if (elem_theme){
theme= elem_theme.value;
items = {'theme': theme };
}
if (INVITE_KEY && INVITE_KEY != '') {
if (INVITE_KEY && INVITE_KEY !== '') {
items.invite_key = INVITE_KEY;
}
if (INVITE_ID && INVITE_ID != '') {
if (INVITE_ID && INVITE_ID !== '') {
items.invite_id = INVITE_ID;
}
const data = {
@ -308,6 +315,7 @@ const log_in = async () => {
otp_auth,
roles,
items,
openids,
//timestamp: new Date().toLocaleString(),
};
const response = await fetch(SIGNIN_URL, {
@ -334,13 +342,14 @@ window.addEventListener('load', () => {
const section_title = document.getElementById('section-title')
const back_btn = document.getElementById('back-btn')
const user_roles = document.getElementById('user-roles-inpt');
const user_openids = document.getElementById('user-openids-inpt');
if (show_password) {
show_password.addEventListener('click', (ev) => {
ev.preventDefault();
[...show_password.children].forEach(it => {
it.classList.toggle('hidden');
});
if (input_password.type == 'password') {
if (input_password.type === 'password') {
input_password.type = 'text';
} else {
input_password.type = 'password';
@ -353,12 +362,12 @@ window.addEventListener('load', () => {
[...show_repeat_password.children].forEach(it => {
it.classList.toggle('hidden');
});
if (input_repeat_password.type == 'password') {
if (input_repeat_password.type === 'password') {
input_repeat_password.type = 'text';
} else {
input_repeat_password.type = 'password';
}
});
});
}
if (otp_code_text) {
otp_code_text.addEventListener('click', (ev) => {
@ -379,6 +388,9 @@ window.addEventListener('load', () => {
});
}
if (user_roles) {
make_role_chips(user_roles);
make_chips(user_roles, 'roles');
}
if (user_openids) {
make_chips(user_openids, 'openids');
}
});

View File

@ -1,8 +1,8 @@
const make_role_chips = (elem) => {
const make_chips = (elem,keys) => {
const arr_items = elem.value == '' ? [] : elem.value.split(',');
const roles_chips = document.getElementById('roles-chips');
if (roles_chips) {
roles_chips.innerHTML='';
const elem_chips = document.getElementById(`${keys}-chips`);
if (elem_chips) {
elem_chips.innerHTML='';
if (arr_items.length == 0) { return; }
arr_items.forEach(name => {
if (name.replaceAll(' ','') !== '') {
@ -11,7 +11,7 @@ const make_role_chips = (elem) => {
const arr_classes = "inline-flex items-center px-2 py-1 mr-2 text-sm font-medium text-blue-800 bg-blue-100 rounded dark:bg-blue-900 dark:text-blue-300".split(" ");
chip.classList.add(...arr_classes);
chip.innerHTML = `${name}`;
roles_chips.append(chip);
elem_chips.append(chip);
}
});
}
@ -19,6 +19,10 @@ const make_role_chips = (elem) => {
window.addEventListener('load', () => {
const user_roles = document.getElementById('user-roles-inpt');
if (user_roles) {
make_role_chips(user_roles);
make_chips(user_roles, 'roles');
}
const user_openids = document.getElementById('user-openids-inpt');
if (user_openids) {
make_chips(user_openids, 'openids');
}
});

View File

@ -22,6 +22,7 @@ const load_elem = () => {
email: document.getElementById('usr-email'),
description: document.getElementById('usr-description'),
roles: document.getElementById('usr-roles'),
openids: document.getElementById('usr-openids'),
items: document.getElementById('usr-items'),
status: document.getElementById('usr-status'),
current_status: document.getElementById('current-usr-status'),
@ -57,6 +58,7 @@ const fill_form = () => {
usrs_elem.form.email.value = usrs_env.user.email;
usrs_elem.form.description.value = usrs_env.user.description;
usrs_elem.form.roles.value = usrs_env.user.roles;
usrs_elem.form.openids.value = usrs_env.user.openids;
usrs_elem.form.items.value = usrs_env.user.items;
usrs_elem.form.status.value = usrs_env.user.status;
usrs_env.user.isadmin == 1 ?
@ -86,6 +88,7 @@ const fill_form = () => {
default:
color='yellow';
}
usrs_elem.form.openids.value = usrs_env.openids;
set_user_status(usrs_elem.form.status.value,color);
};
const collect_form = () => {
@ -94,6 +97,7 @@ const collect_form = () => {
usrs_env.user.email = usrs_elem.form.email.value;
usrs_env.user.description = usrs_elem.form.description.value;
usrs_env.user.roles = usrs_elem.form.roles.value;
usrs_env.user.openids = usrs_elem.form.openids.value;
usrs_env.user.items = usrs_elem.form.items.value;
usrs_env.user.status = usrs_elem.form.status.value;
usrs_elem.form.isadmin.checked ?
@ -111,7 +115,8 @@ const edit_usr = async (id) => {
if (usrs_env.modal.show) {
const res = await get_user('id',id);
if (res.status && res.data.id == id) {
usrs_env.user = res.data;
usrs_env.user = res.data;
usrs_env.openids = res.openids;
fill_form();
usrs_env.modal.show();
}
@ -140,7 +145,7 @@ const on_search = (el) => {
}
});
if (usrs_elem.count_select) {
if (count_select == total) {
if (count_select === total) {
usrs_elem.count_select.innerHTML='';
} else {
usrs_elem.count_select.innerHTML=`${count_select} of`;
@ -215,9 +220,12 @@ const table_sort = (col) => {
case 'isadmin':
col_rows.push({ky: data[6], tr:it})
break;
case 'openids':
col_rows.push({ky: data[7], tr:it})
break;
}
})
if (col_rows.length == 0) { return; }
if (col_rows.length === 0) { return; }
const is_asc = !usrs_env.cols[col].asc;
usrs_env.cols[col].asc = is_asc;
if (is_asc) {
@ -272,7 +280,7 @@ const alert_info = (task,message) => {
}
};
const post_fetch = async (source,url = '',source_data) => {
if (url == '') { return }
if (url === '') { return }
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(source_data),
@ -283,19 +291,19 @@ const post_fetch = async (source,url = '',source_data) => {
alert_danger('hide','');
alert_info('hide','');
if (response.ok && response.status === 200 ) {
if (source == 'get_user') {
if (source === 'get_user') {
const name = source_data.name;
const value = source_data.value;
const value = `${source_data.value}`;
const data = await response.json();
if (data[name] && data[name] == value){
return {status: true, data }
if (data.user && data.user[name] && `${data.user[name]}` === value) {
return {status: true, data: data.user, openids: data.openids ? data.openids : '' }
} else {
alert_danger('show',`No data found for ${name}`)
return {status: false, data: {}};
return {status: false, data: {}, openids: ''};
}
} else {
const data = await response.text();
if (data != '') { alert_info('show',data); }
if (data !== '') { alert_info('show',data); }
return {status: true, data }
}
} else if ( response.status === 401 ) {
@ -404,7 +412,7 @@ window.addEventListener('load', () => {
if (thead[0]) {
[...thead[0].getElementsByTagName('th')].forEach(col => {
const th = col.dataset.th;
if (th != 'check') {
if (th !== 'check') {
usrs_env.cols[th]={asc: false};
col.addEventListener('click', (ev) => {
ev.preventDefault();