authorize-application.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * Authorize Application Screen
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /** WordPress Administration Bootstrap */
  9. require_once __DIR__ . '/admin.php';
  10. $error = null;
  11. $new_password = '';
  12. // This is the no-js fallback script. Generally this will all be handled by `auth-app.js`.
  13. if ( isset( $_POST['action'] ) && 'authorize_application_password' === $_POST['action'] ) {
  14. check_admin_referer( 'authorize_application_password' );
  15. $success_url = $_POST['success_url'];
  16. $reject_url = $_POST['reject_url'];
  17. $app_name = $_POST['app_name'];
  18. $app_id = $_POST['app_id'];
  19. $redirect = '';
  20. if ( isset( $_POST['reject'] ) ) {
  21. if ( $reject_url ) {
  22. $redirect = $reject_url;
  23. } else {
  24. $redirect = admin_url();
  25. }
  26. } elseif ( isset( $_POST['approve'] ) ) {
  27. $created = WP_Application_Passwords::create_new_application_password(
  28. get_current_user_id(),
  29. array(
  30. 'name' => $app_name,
  31. 'app_id' => $app_id,
  32. )
  33. );
  34. if ( is_wp_error( $created ) ) {
  35. $error = $created;
  36. } else {
  37. list( $new_password ) = $created;
  38. if ( $success_url ) {
  39. $redirect = add_query_arg(
  40. array(
  41. 'site_url' => urlencode( site_url() ),
  42. 'user_login' => urlencode( wp_get_current_user()->user_login ),
  43. 'password' => urlencode( $new_password ),
  44. ),
  45. $success_url
  46. );
  47. }
  48. }
  49. }
  50. if ( $redirect ) {
  51. // Explicitly not using wp_safe_redirect b/c sends to arbitrary domain.
  52. wp_redirect( $redirect );
  53. exit;
  54. }
  55. }
  56. // Used in the HTML title tag.
  57. $title = __( 'Authorize Application' );
  58. $app_name = ! empty( $_REQUEST['app_name'] ) ? $_REQUEST['app_name'] : '';
  59. $app_id = ! empty( $_REQUEST['app_id'] ) ? $_REQUEST['app_id'] : '';
  60. $success_url = ! empty( $_REQUEST['success_url'] ) ? $_REQUEST['success_url'] : null;
  61. if ( ! empty( $_REQUEST['reject_url'] ) ) {
  62. $reject_url = $_REQUEST['reject_url'];
  63. } elseif ( $success_url ) {
  64. $reject_url = add_query_arg( 'success', 'false', $success_url );
  65. } else {
  66. $reject_url = null;
  67. }
  68. $user = wp_get_current_user();
  69. $request = compact( 'app_name', 'app_id', 'success_url', 'reject_url' );
  70. $is_valid = wp_is_authorize_application_password_request_valid( $request, $user );
  71. if ( is_wp_error( $is_valid ) ) {
  72. wp_die(
  73. __( 'The Authorize Application request is not allowed.' ) . ' ' . implode( ' ', $is_valid->get_error_messages() ),
  74. __( 'Cannot Authorize Application' )
  75. );
  76. }
  77. if ( wp_is_site_protected_by_basic_auth( 'front' ) ) {
  78. wp_die(
  79. __( 'Your website appears to use Basic Authentication, which is not currently compatible with application passwords.' ),
  80. __( 'Cannot Authorize Application' ),
  81. array(
  82. 'response' => 501,
  83. 'link_text' => __( 'Go Back' ),
  84. 'link_url' => $reject_url ? add_query_arg( 'error', 'disabled', $reject_url ) : admin_url(),
  85. )
  86. );
  87. }
  88. if ( ! wp_is_application_passwords_available_for_user( $user ) ) {
  89. if ( wp_is_application_passwords_available() ) {
  90. $message = __( 'Application passwords are not available for your account. Please contact the site administrator for assistance.' );
  91. } else {
  92. $message = __( 'Application passwords are not available.' );
  93. }
  94. wp_die(
  95. $message,
  96. __( 'Cannot Authorize Application' ),
  97. array(
  98. 'response' => 501,
  99. 'link_text' => __( 'Go Back' ),
  100. 'link_url' => $reject_url ? add_query_arg( 'error', 'disabled', $reject_url ) : admin_url(),
  101. )
  102. );
  103. }
  104. wp_enqueue_script( 'auth-app' );
  105. wp_localize_script(
  106. 'auth-app',
  107. 'authApp',
  108. array(
  109. 'site_url' => site_url(),
  110. 'user_login' => $user->user_login,
  111. 'success' => $success_url,
  112. 'reject' => $reject_url ? $reject_url : admin_url(),
  113. )
  114. );
  115. require_once ABSPATH . 'wp-admin/admin-header.php';
  116. ?>
  117. <div class="wrap">
  118. <h1><?php echo esc_html( $title ); ?></h1>
  119. <?php if ( is_wp_error( $error ) ) : ?>
  120. <div class="notice notice-error"><p><?php echo $error->get_error_message(); ?></p></div>
  121. <?php endif; ?>
  122. <div class="card auth-app-card">
  123. <h2 class="title"><?php _e( 'An application would like to connect to your account.' ); ?></h2>
  124. <?php if ( $app_name ) : ?>
  125. <p>
  126. <?php
  127. printf(
  128. /* translators: %s: Application name. */
  129. __( 'Would you like to give the application identifying itself as %s access to your account? You should only do this if you trust the application in question.' ),
  130. '<strong>' . esc_html( $app_name ) . '</strong>'
  131. );
  132. ?>
  133. </p>
  134. <?php else : ?>
  135. <p><?php _e( 'Would you like to give this application access to your account? You should only do this if you trust the application in question.' ); ?></p>
  136. <?php endif; ?>
  137. <?php
  138. if ( is_multisite() ) {
  139. $blogs = get_blogs_of_user( $user->ID, true );
  140. $blogs_count = count( $blogs );
  141. if ( $blogs_count > 1 ) {
  142. ?>
  143. <p>
  144. <?php
  145. /* translators: 1: URL to my-sites.php, 2: Number of sites the user has. */
  146. $message = _n(
  147. 'This will grant access to <a href="%1$s">the %2$s site in this installation that you have permissions on</a>.',
  148. 'This will grant access to <a href="%1$s">all %2$s sites in this installation that you have permissions on</a>.',
  149. $blogs_count
  150. );
  151. if ( is_super_admin() ) {
  152. /* translators: 1: URL to my-sites.php, 2: Number of sites the user has. */
  153. $message = _n(
  154. 'This will grant access to <a href="%1$s">the %2$s site on the network as you have Super Admin rights</a>.',
  155. 'This will grant access to <a href="%1$s">all %2$s sites on the network as you have Super Admin rights</a>.',
  156. $blogs_count
  157. );
  158. }
  159. printf(
  160. $message,
  161. admin_url( 'my-sites.php' ),
  162. number_format_i18n( $blogs_count )
  163. );
  164. ?>
  165. </p>
  166. <?php
  167. }
  168. }
  169. ?>
  170. <?php if ( $new_password ) : ?>
  171. <div class="notice notice-success notice-alt below-h2">
  172. <p class="application-password-display">
  173. <label for="new-application-password-value">
  174. <?php
  175. printf(
  176. /* translators: %s: Application name. */
  177. esc_html__( 'Your new password for %s is:' ),
  178. '<strong>' . esc_html( $app_name ) . '</strong>'
  179. );
  180. ?>
  181. </label>
  182. <input id="new-application-password-value" type="text" class="code" readonly="readonly" value="<?php esc_attr( WP_Application_Passwords::chunk_password( $new_password ) ); ?>" />
  183. </p>
  184. <p><?php _e( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ); ?></p>
  185. </div>
  186. <?php
  187. /**
  188. * Fires in the Authorize Application Password new password section in the no-JS version.
  189. *
  190. * In most cases, this should be used in combination with the {@see 'wp_application_passwords_approve_app_request_success'}
  191. * action to ensure that both the JS and no-JS variants are handled.
  192. *
  193. * @since 5.6.0
  194. * @since 5.6.1 Corrected action name and signature.
  195. *
  196. * @param string $new_password The newly generated application password.
  197. * @param array $request The array of request data. All arguments are optional and may be empty.
  198. * @param WP_User $user The user authorizing the application.
  199. */
  200. do_action( 'wp_authorize_application_password_form_approved_no_js', $new_password, $request, $user );
  201. ?>
  202. <?php else : ?>
  203. <form action="<?php echo esc_url( admin_url( 'authorize-application.php' ) ); ?>" method="post" class="form-wrap">
  204. <?php wp_nonce_field( 'authorize_application_password' ); ?>
  205. <input type="hidden" name="action" value="authorize_application_password" />
  206. <input type="hidden" name="app_id" value="<?php echo esc_attr( $app_id ); ?>" />
  207. <input type="hidden" name="success_url" value="<?php echo esc_url( $success_url ); ?>" />
  208. <input type="hidden" name="reject_url" value="<?php echo esc_url( $reject_url ); ?>" />
  209. <div class="form-field">
  210. <label for="app_name"><?php _e( 'New Application Password Name' ); ?></label>
  211. <input type="text" id="app_name" name="app_name" value="<?php echo esc_attr( $app_name ); ?>" required />
  212. </div>
  213. <?php
  214. /**
  215. * Fires in the Authorize Application Password form before the submit buttons.
  216. *
  217. * @since 5.6.0
  218. *
  219. * @param array $request {
  220. * The array of request data. All arguments are optional and may be empty.
  221. *
  222. * @type string $app_name The suggested name of the application.
  223. * @type string $success_url The URL the user will be redirected to after approving the application.
  224. * @type string $reject_url The URL the user will be redirected to after rejecting the application.
  225. * }
  226. * @param WP_User $user The user authorizing the application.
  227. */
  228. do_action( 'wp_authorize_application_password_form', $request, $user );
  229. ?>
  230. <?php
  231. submit_button(
  232. __( 'Yes, I approve of this connection' ),
  233. 'primary',
  234. 'approve',
  235. false,
  236. array(
  237. 'aria-describedby' => 'description-approve',
  238. )
  239. );
  240. ?>
  241. <p class="description" id="description-approve">
  242. <?php
  243. if ( $success_url ) {
  244. printf(
  245. /* translators: %s: The URL the user is being redirected to. */
  246. __( 'You will be sent to %s' ),
  247. '<strong><code>' . esc_html(
  248. add_query_arg(
  249. array(
  250. 'site_url' => site_url(),
  251. 'user_login' => $user->user_login,
  252. 'password' => '[------]',
  253. ),
  254. $success_url
  255. )
  256. ) . '</code></strong>'
  257. );
  258. } else {
  259. _e( 'You will be given a password to manually enter into the application in question.' );
  260. }
  261. ?>
  262. </p>
  263. <?php
  264. submit_button(
  265. __( 'No, I do not approve of this connection' ),
  266. 'secondary',
  267. 'reject',
  268. false,
  269. array(
  270. 'aria-describedby' => 'description-reject',
  271. )
  272. );
  273. ?>
  274. <p class="description" id="description-reject">
  275. <?php
  276. if ( $reject_url ) {
  277. printf(
  278. /* translators: %s: The URL the user is being redirected to. */
  279. __( 'You will be sent to %s' ),
  280. '<strong><code>' . esc_html( $reject_url ) . '</code></strong>'
  281. );
  282. } else {
  283. _e( 'You will be returned to the WordPress Dashboard, and no changes will be made.' );
  284. }
  285. ?>
  286. </p>
  287. </form>
  288. <?php endif; ?>
  289. </div>
  290. </div>
  291. <?php
  292. require_once ABSPATH . 'wp-admin/admin-footer.php';