/** * Generates the actual browserfingerprint. */ function GenerateBrowserFingerprint() { let length = 255; let result = ''; let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; for ( let i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } $('#mfa_browser_fingerprint').val(result); } /** * Updates the trusted browser status. * It checks for expired fingerprints and creates a new fingerprint when necessary. * Mind that this function does not store/save the fingerprint to the browser's localStorage. */ function UpdateTrustedBrowserStatus() { var sUserName = $('#user').val(); var sSettings = localStorage.getItem('mfa_' + sUserName); // Check if the client can detect an outdated fingerprint to avoid the user needs to log in again first. if(sSettings !== null) { var oSettings = JSON.parse(sSettings); let oSetDate = oSettings.date; let oExpirationDate = new Date(new Date(oSetDate).getTime() + (14 * 24 * 60 * 60 * 1000)); let oCurrentDate = new Date(); if(oExpirationDate <= oCurrentDate) { // Destroy and generate, do not save yet localStorage.removeItem('mfa_' + sUserName); GenerateBrowserFingerprint(); } else { // Use existing print $('#mfa_browser_fingerprint').val(oSettings.fingerprint); } } // No fingerprint exists. else { GenerateBrowserFingerprint(); } } /** * Toggles the visibility of the "trusted browser" checkbox and OTP fields based on user name. */ function UpdateTrustedBrowserVisibility() { var sUserName = $('#user').val(); if(localStorage.getItem('mfa_' + sUserName) !== null) { // Fingerprint configured. If it fails, it will be have been unset upon reload (earlier). $('#jb-mfa').hide(); } else { $('#jb-mfa').show(); } } // Various events (e.g. autofilled usernames). // What about pre-authenticated users? $('#user').on('keyup change blur input', function(e) { UpdateTrustedBrowserVisibility(); }); // Initially (could be prefilled e.g. with the auth_user parameter) UpdateTrustedBrowserVisibility(); $(document).ready(function() { // Store (new) fingerprint on submission $('#login_form').on('submit', function(e) { // Only continue if fingerprint has not been set yet if($('#mfa_browser_fingerprint').val() != '') { return; } // Prevent original submission (without mfa_browser_fingerprint) e.preventDefault(); UpdateTrustedBrowserStatus(); var sUserName = $('#user').val(); var sFingerprint = $('#mfa_browser_fingerprint').val(); // Store fingerprint if new and if wanted if(localStorage.getItem('mfa_' + sUserName) === null && $('#mfa_browser_trust').prop('checked') == true) { localStorage.setItem('mfa_' + sUserName, JSON.stringify({ fingerprint: sFingerprint, date: new Date() })); } // console.log(sFingerprint); $('#mfa_browser_fingerprint').attr('data-set', '1'); $(this).submit(); }); });