auth-app.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @output wp-admin/js/auth-app.js
  3. */
  4. /* global authApp */
  5. ( function( $, authApp ) {
  6. var $appNameField = $( '#app_name' ),
  7. $approveBtn = $( '#approve' ),
  8. $rejectBtn = $( '#reject' ),
  9. $form = $appNameField.closest( 'form' ),
  10. context = {
  11. userLogin: authApp.user_login,
  12. successUrl: authApp.success,
  13. rejectUrl: authApp.reject
  14. };
  15. $approveBtn.on( 'click', function( e ) {
  16. var name = $appNameField.val(),
  17. appId = $( 'input[name="app_id"]', $form ).val();
  18. e.preventDefault();
  19. if ( $approveBtn.prop( 'aria-disabled' ) ) {
  20. return;
  21. }
  22. if ( 0 === name.length ) {
  23. $appNameField.trigger( 'focus' );
  24. return;
  25. }
  26. $approveBtn.prop( 'aria-disabled', true ).addClass( 'disabled' );
  27. var request = {
  28. name: name
  29. };
  30. if ( appId.length > 0 ) {
  31. request.app_id = appId;
  32. }
  33. /**
  34. * Filters the request data used to Authorize an Application Password request.
  35. *
  36. * @since 5.6.0
  37. *
  38. * @param {Object} request The request data.
  39. * @param {Object} context Context about the Application Password request.
  40. * @param {string} context.userLogin The user's login username.
  41. * @param {string} context.successUrl The URL the user will be redirected to after approving the request.
  42. * @param {string} context.rejectUrl The URL the user will be redirected to after rejecting the request.
  43. */
  44. request = wp.hooks.applyFilters( 'wp_application_passwords_approve_app_request', request, context );
  45. wp.apiRequest( {
  46. path: '/wp/v2/users/me/application-passwords?_locale=user',
  47. method: 'POST',
  48. data: request
  49. } ).done( function( response, textStatus, jqXHR ) {
  50. /**
  51. * Fires when an Authorize Application Password request has been successfully approved.
  52. *
  53. * In most cases, this should be used in combination with the {@see 'wp_authorize_application_password_form_approved_no_js'}
  54. * action to ensure that both the JS and no-JS variants are handled.
  55. *
  56. * @since 5.6.0
  57. *
  58. * @param {Object} response The response from the REST API.
  59. * @param {string} response.password The newly created password.
  60. * @param {string} textStatus The status of the request.
  61. * @param {jqXHR} jqXHR The underlying jqXHR object that made the request.
  62. */
  63. wp.hooks.doAction( 'wp_application_passwords_approve_app_request_success', response, textStatus, jqXHR );
  64. var raw = authApp.success,
  65. url, message, $notice;
  66. if ( raw ) {
  67. url = raw + ( -1 === raw.indexOf( '?' ) ? '?' : '&' ) +
  68. 'site_url=' + encodeURIComponent( authApp.site_url ) +
  69. '&user_login=' + encodeURIComponent( authApp.user_login ) +
  70. '&password=' + encodeURIComponent( response.password );
  71. window.location = url;
  72. } else {
  73. message = wp.i18n.sprintf(
  74. /* translators: %s: Application name. */
  75. '<label for="new-application-password-value">' + wp.i18n.__( 'Your new password for %s is:' ) + '</label>',
  76. '<strong></strong>'
  77. ) + ' <input id="new-application-password-value" type="text" class="code" readonly="readonly" value="" />';
  78. $notice = $( '<div></div>' )
  79. .attr( 'role', 'alert' )
  80. .attr( 'tabindex', -1 )
  81. .addClass( 'notice notice-success notice-alt' )
  82. .append( $( '<p></p>' ).addClass( 'application-password-display' ).html( message ) )
  83. .append( '<p>' + wp.i18n.__( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ) + '</p>' );
  84. // We're using .text() to write the variables to avoid any chance of XSS.
  85. $( 'strong', $notice ).text( response.name );
  86. $( 'input', $notice ).val( response.password );
  87. $form.replaceWith( $notice );
  88. $notice.trigger( 'focus' );
  89. }
  90. } ).fail( function( jqXHR, textStatus, errorThrown ) {
  91. var errorMessage = errorThrown,
  92. error = null;
  93. if ( jqXHR.responseJSON ) {
  94. error = jqXHR.responseJSON;
  95. if ( error.message ) {
  96. errorMessage = error.message;
  97. }
  98. }
  99. var $notice = $( '<div></div>' )
  100. .attr( 'role', 'alert' )
  101. .addClass( 'notice notice-error' )
  102. .append( $( '<p></p>' ).text( errorMessage ) );
  103. $( 'h1' ).after( $notice );
  104. $approveBtn.removeProp( 'aria-disabled', false ).removeClass( 'disabled' );
  105. /**
  106. * Fires when an Authorize Application Password request encountered an error when trying to approve the request.
  107. *
  108. * @since 5.6.0
  109. * @since 5.6.1 Corrected action name and signature.
  110. *
  111. * @param {Object|null} error The error from the REST API. May be null if the server did not send proper JSON.
  112. * @param {string} textStatus The status of the request.
  113. * @param {string} errorThrown The error message associated with the response status code.
  114. * @param {jqXHR} jqXHR The underlying jqXHR object that made the request.
  115. */
  116. wp.hooks.doAction( 'wp_application_passwords_approve_app_request_error', error, textStatus, errorThrown, jqXHR );
  117. } );
  118. } );
  119. $rejectBtn.on( 'click', function( e ) {
  120. e.preventDefault();
  121. /**
  122. * Fires when an Authorize Application Password request has been rejected by the user.
  123. *
  124. * @since 5.6.0
  125. *
  126. * @param {Object} context Context about the Application Password request.
  127. * @param {string} context.userLogin The user's login username.
  128. * @param {string} context.successUrl The URL the user will be redirected to after approving the request.
  129. * @param {string} context.rejectUrl The URL the user will be redirected to after rejecting the request.
  130. */
  131. wp.hooks.doAction( 'wp_application_passwords_reject_app', context );
  132. // @todo: Make a better way to do this so it feels like less of a semi-open redirect.
  133. window.location = authApp.reject;
  134. } );
  135. $form.on( 'submit', function( e ) {
  136. e.preventDefault();
  137. } );
  138. }( jQuery, authApp ) );