Skip to content
login.html 6.26 KiB
Newer Older
<!DOCTYPE html>
<html>
    <head>
        {% for src in js %}
            <script type="text/javascript" src="{{ src }}"></script>
        {% endfor %}
        {% for src in css %}
            <link type="text/css" rel="stylesheet" href="{{ src }}">
        {% endfor %}
        
        <script type="text/javascript" src="{{ url_for( 'send_app_files', path = 'functions.js' ) }}"></script>
        <link type="text/css" rel="stylesheet" href="{{ url_for( 'send_app_files', path = 'app.css' ) }}">
        
        <script type="text/javascript">
            baseurl = "{{ baseurl }}";
        </script>
    </head>
    <body>
        <div class="icnml_login">
            <h1 style="margin-bottom: 0px">ICNML</h1>
            <h4 style="margin-top: 0px">International Close Non-Matches Library</h4>
            
            <div class="ui-widget-header ui-corner-top icnml_login_top">Please enter your login information</div>
Marco De Donno's avatar
Marco De Donno committed
            <div class="ui-widget-content ui-corner-bottom icnml_login_form">
                <div id="icnml_login_field" class="icnml_login_field">
                	<div style="text-align: right;">
                		<label for="username">Username</label>
                	</div>
                	<div>
                		<input id="username" name="username" type="text" style="width: 100%">
                    </div>
Marco De Donno's avatar
Marco De Donno committed
                	<div style="text-align: right;">
                		<label for="password">Password</label>
                	</div>
                	<div>
                		<input id="password" name="password" type="password" style="width: 100%">
                    </div>
                </div>
Marco De Donno's avatar
Marco De Donno committed
                <div id="icnml_login_error"></div>
                <div id="icnml_login_warning"></div>
Marco De Donno's avatar
Marco De Donno committed
                <div class="icnml_login_button">
                	<a class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="login_button" role="button" aria-disabled="false">
                        <span class="ui-button-text">Login</span>
                    </a>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript">
    	var login_action_password = async function()
		{
    		$( '#icnml_login_error' ).html( '' );
    		$( '#login_button > span' ).text( "Please wait..." );
    		
Marco De Donno's avatar
Marco De Donno committed
			var username = $( '#username' ).val();
    		var password = $( '#password' ).val();
    		
            if( window.crypto && window.crypto.subtle && window.TextEncoder && window.TextDecoder )
            {
            	password = await generateKey( password, "icnml_" + username, 20000 );
        		password = password.substring( 0, 128 );
        		password = "pbkdf2$sha512$icnml_" + username + "$20000$" + password;
            }
    		
        	$.ajax( {
	            url: '{{ url_for( 'do_login' ) }}',
	            dataType: 'json',
	            method: 'POST',
	            data: {
Marco De Donno's avatar
Marco De Donno committed
	            	username: username,
	            	password: password
	            },
	            success: function( data )
	            {
	                if( ! data.error )
	                {
	                	if( data.logged )
                		{
	                		location.href = "{{ url_for( 'home' ) }}";
Marco De Donno's avatar
Marco De Donno committed
                		
                		} else if( data.must_use_totp === true ) {
                			build_totp_form();
            			
                		} else {
                			if( typeof data.message !== 'undefined' )
                				var message = data.message;
                			else
                				var message = 'Invalid username/password';
                			
                			$( '#icnml_login_error' ).text( message );
Marco De Donno's avatar
Marco De Donno committed
	                	
	                	$( '#login_button > span' ).text( "Login" );
Marco De Donno's avatar
Marco De Donno committed
    	var login_action_totp = function()
		{
    		$( '#icnml_login_error' ).html( '' );
    		$( '#login_button > span' ).text( "Please wait..." );
    		
        	$.ajax( {
	            url: '{{ url_for( 'do_login' ) }}',
	            dataType: 'json',
	            method: 'POST',
	            data: {
	            	totp: $( '#totp' ).val()
	            },
	            success: function( data )
	            {
	                if( ! data.error )
	                {
	                	if( data.logged )
                		{
	                		location.href = "{{ url_for( 'home' ) }}";
                		} else {
                			if( typeof data.message !== 'undefined' )
                				$( '#icnml_login_error' ).text( data.message );
                		}
	                }
	            }
	        } );
		}
    	
    	var build_totp_form = function()
    	{
    		$( '#icnml_login_error' ).html( '' );
    		$( '#icnml_login_field' ).html( '' );
			$( '#icnml_login_field' )
				.append(
					$( '<div />' )
						.text( "TOTP" )
						.css( "text-align", "right" )
				)
				.append(
					$( '<div />' ).append(
						$( '<input/>' )
							.attr( "id", "totp" )
							.attr( "name", "totp" )
							.css( "width", "100%" )
							.on( 'keyup', function( event )
                    		{
                            	if( event.keyCode == 13 )
                            	{
                            		event.preventDefault();
                            		login_action_totp();
                            	}
                    		} )
					)
				);
			
			$( '#totp' ).focus();
    	}
    	
    	/* Events binding */
		$( '#username' ).on( 'keyup', function( event )
		{
        	if( event.keyCode == 13 )
        	{
        		event.preventDefault();
Marco De Donno's avatar
Marco De Donno committed
        		login_action_password();
        	}
		} );
        $( '#password' ).on( 'keyup', function( event )
		{
        	if( event.keyCode == 13 )
        	{
        		event.preventDefault();
Marco De Donno's avatar
Marco De Donno committed
        		login_action_password();
Marco De Donno's avatar
Marco De Donno committed
        $( '#login_button' ).on( 'click', login_action_password );
        
		$( '#username' ).focus();
		
        $( document ).ready( function()
        {
            if( !window.crypto || !window.crypto.subtle || !window.TextEncoder || !window.TextDecoder)
            {
                $( '#icnml_login_warning' )
                    .text( "Your browser does not support client-side cryptography. Please use compatible browser (Firefox, Chrome, Opera, Safari, ...) to protect your password before sending it to the ICNML server." );
            }
        } );
    </script>
</html>
Marco De Donno's avatar
Marco De Donno committed