plugins/yubikey/yubikey.js
changeset 35 03d6287d4a8b
parent 30 294bab0bb3e2
equal deleted inserted replaced
34:6e947fa21237 35:03d6287d4a8b
    54         if ( e.keyCode == 27 )
    54         if ( e.keyCode == 27 )
    55         {
    55         {
    56           window.clearInterval(yk_interval);
    56           window.clearInterval(yk_interval);
    57           miniPromptDestroy(this);
    57           miniPromptDestroy(this);
    58         }
    58         }
    59         else if ( this.value.length == 44 && !this.submitted )
    59         // 0.3: submit only upon a keycode 13
    60         {
    60         else if ( e.keyCode == 13 )
    61           this.submitted = true;
       
    62           yk_handle_submit(this);
       
    63         }
       
    64         else if ( e.keyCode == 13 && this.value.length != 44 )
       
    65         {
    61         {
    66           this.submitted = true;
    62           this.submitted = true;
    67           yk_handle_submit(this);
    63           yk_handle_submit(this);
    68         }
    64         }
    69         else
    65         else
    95 
    91 
    96 function yk_handle_submit(ta)
    92 function yk_handle_submit(ta)
    97 {
    93 {
    98   if ( ta.value.length > 44 || !ta.value.match(/^[cbdefghijklnrtuv]+$/) )
    94   if ( ta.value.length > 44 || !ta.value.match(/^[cbdefghijklnrtuv]+$/) )
    99   {
    95   {
       
    96     // report "invalid characters"
   100     setTimeout(function()
    97     setTimeout(function()
   101       {
    98       {
   102         var parent = ta.parentNode;
    99         var parent = ta.parentNode;
   103         var tabackup = {
   100         var tabackup = {
   104           field_id: ta.yk_field_id,
   101           field_id: ta.yk_field_id,
   125   {
   122   {
   126     var field = document.getElementById(ta.yk_field_id);
   123     var field = document.getElementById(ta.yk_field_id);
   127     var status = document.getElementById(ta.yk_status_id);
   124     var status = document.getElementById(ta.yk_status_id);
   128     if ( $(status).hasClass('empty') || $(status).hasClass('rmpending') )
   125     if ( $(status).hasClass('empty') || $(status).hasClass('rmpending') )
   129     {
   126     {
   130       $(status).next('a')
   127       $(status)
       
   128       .next('span.yubikey_pubkey')
       
   129         .text(ta.value.substr(0, 12))
       
   130       .next('a.yubikey_enroll')
   131         .text($lang.get('yubiauth_ctl_btn_change_key'))
   131         .text($lang.get('yubiauth_ctl_btn_change_key'))
   132         .addClass('abutton_green')
   132         .addClass('abutton_green')
   133         .after(' <a class="abutton abutton_red yubikey_enroll" href="#yk_clear" onclick="yk_clear(\'' + ta.yk_field_id + '\', \'' + ta.yk_status_id + '\'); return false;">'
   133         .after(' <a class="abutton abutton_red yubikey_enroll" href="#yk_clear" onclick="yk_clear(\'' + ta.yk_field_id + '\', \'' + ta.yk_status_id + '\'); return false;">'
   134                + $lang.get('yubiauth_ctl_btn_clear') +
   134                + $lang.get('yubiauth_ctl_btn_clear') +
   135                '</a>');
   135                '</a>');
   136     }
   136     }
   137     $(status).removeClass('empty').removeClass('enrolled').removeClass('rmpending').addClass('savepending').html($lang.get('yubiauth_ctl_status_enrolled_pending'));
   137     $(status).removeClass('empty').removeClass('enrolled').removeClass('rmpending').addClass('savepending').html($lang.get('yubiauth_ctl_status_enrolled_pending'));
       
   138     $(status).next('span.yubikey_pubkey').text(ta.value.substr(0, 12));
   138     field.value = ta.value;
   139     field.value = ta.value;
   139     miniPromptDestroy(ta);
   140     miniPromptDestroy(ta);
   140     return true;
   141     return true;
   141   }
   142   }
   142   else if ( ta.submit_func )
   143   else if ( ta.submit_func )
   161     if ( logindata.mb_object )
   162     if ( logindata.mb_object )
   162     {
   163     {
   163       // login window is open
   164       // login window is open
   164       if ( user_level == USER_LEVEL_GUEST )
   165       if ( user_level == USER_LEVEL_GUEST )
   165       {
   166       {
   166         var show_username = window.yk_user_flags & YK_SEC_NORMAL_USERNAME;
   167         // for guests, get the user's yubikey auth flags
   167         var show_password = window.yk_user_flags & YK_SEC_NORMAL_PASSWORD;
   168         // we're still ok to submit, so make sure twofactor isn't enabled
       
   169         // as we are a guest, we have to get the flags for the user from the server
       
   170         var ajax = ajaxMakeXHR();
       
   171         var uri = makeUrlNS('Special', 'Yubikey', 'get_flags=' + ta.value.substr(0, 12));
       
   172         var flags = 0;
       
   173         try
       
   174         {
       
   175           ajax.open('GET', uri, false);
       
   176           ajax.send(null);
       
   177           
       
   178           if ( ajax.readyState == 4 && ajax.status == 200 )
       
   179           {
       
   180             // we got it
       
   181             var response = String(ajax.responseText + '');
       
   182             if ( check_json_response(response) )
       
   183             {
       
   184               response = parseJSON(response);
       
   185               flags = response.flags || 0;
       
   186             }
       
   187           }
       
   188         }
       
   189         catch ( e )
       
   190         {
       
   191           ajaxLoginSetStatus(AJAX_STATUS_ERROR);
       
   192           return false;
       
   193         }
       
   194         var show_username = flags & YK_SEC_NORMAL_USERNAME;
       
   195         var show_password = flags & YK_SEC_NORMAL_PASSWORD;
   168       }
   196       }
   169       else
   197       else
   170       {
   198       {
   171         var show_username = window.yk_user_flags & YK_SEC_ELEV_USERNAME;
   199         var show_username = window.yk_user_flags & YK_SEC_ELEV_USERNAME;
   172         var show_password = window.yk_user_flags & YK_SEC_ELEV_PASSWORD;
   200         var show_password = window.yk_user_flags & YK_SEC_ELEV_PASSWORD;
   177         $('#ajax_login_field_password').parent('td').hide().prev().hide();
   205         $('#ajax_login_field_password').parent('td').hide().prev().hide();
   178       
   206       
   179       var can_submit = true;
   207       var can_submit = true;
   180       if ( show_username && !$('#ajax_login_field_username').attr('value') )
   208       if ( show_username && !$('#ajax_login_field_username').attr('value') )
   181       {
   209       {
   182         $('#ajax_login_field_password').focus();
   210         $('#ajax_login_field_username').focus();
       
   211         
       
   212         if ( !show_password )
       
   213           $('#ajax_login_field_username').keyup(function(e)
       
   214             {
       
   215               // assign press of Enter in username field to submit
       
   216               if ( e.keyCode == 13 )
       
   217               {
       
   218                 $('#messageBoxButtons input:button:first').click();
       
   219               }
       
   220             });
       
   221         
   183         can_submit = false;
   222         can_submit = false;
   184       }
   223       }
   185       if ( show_password && !$('#ajax_login_field_password').attr('value') )
   224       if ( show_password && !$('#ajax_login_field_password').attr('value') )
   186       {
   225       {
   187         if ( can_submit )
   226         if ( can_submit )
   188         {
   227         {
       
   228           // can_submit only true if show_username false
   189           $('#ajax_login_field_password').focus();
   229           $('#ajax_login_field_password').focus();
   190         }
   230         }
   191         can_submit = false;
   231         can_submit = false;
   192       }
   232       }
   193       
   233       
   210   $(status)
   250   $(status)
   211     .removeClass('savepending')
   251     .removeClass('savepending')
   212     .removeClass('enrolled')
   252     .removeClass('enrolled')
   213     .addClass( was_pending ? 'empty' : 'rmpending' )
   253     .addClass( was_pending ? 'empty' : 'rmpending' )
   214     .text( was_pending ? $lang.get('yubiauth_ctl_status_empty') : $lang.get('yubiauth_ctl_status_remove_pending') )
   254     .text( was_pending ? $lang.get('yubiauth_ctl_status_empty') : $lang.get('yubiauth_ctl_status_remove_pending') )
       
   255     .next('span.yubikey_pubkey')
       
   256       .text('')
   215     .next('a')
   257     .next('a')
   216       .text($lang.get('yubiauth_ctl_btn_enroll'))
   258       .text($lang.get('yubiauth_ctl_btn_enroll'))
   217       .removeClass('abutton_green')
   259       .removeClass('abutton_green')
   218     .next('a')
   260     .next('a')
   219       .remove();
   261       .remove();