暫無描述

class-wp-application-passwords.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /**
  3. * WP_Application_Passwords class
  4. *
  5. * @package WordPress
  6. * @since 5.6.0
  7. */
  8. /**
  9. * Class for displaying, modifying, and sanitizing application passwords.
  10. *
  11. * @package WordPress
  12. */
  13. class WP_Application_Passwords {
  14. /**
  15. * The application passwords user meta key.
  16. *
  17. * @since 5.6.0
  18. *
  19. * @var string
  20. */
  21. const USERMETA_KEY_APPLICATION_PASSWORDS = '_application_passwords';
  22. /**
  23. * The option name used to store whether application passwords is in use.
  24. *
  25. * @since 5.6.0
  26. *
  27. * @var string
  28. */
  29. const OPTION_KEY_IN_USE = 'using_application_passwords';
  30. /**
  31. * The generated application password length.
  32. *
  33. * @since 5.6.0
  34. *
  35. * @var int
  36. */
  37. const PW_LENGTH = 24;
  38. /**
  39. * Checks if Application Passwords are being used by the site.
  40. *
  41. * This returns true if at least one App Password has ever been created.
  42. *
  43. * @since 5.6.0
  44. *
  45. * @return bool
  46. */
  47. public static function is_in_use() {
  48. $network_id = get_main_network_id();
  49. return (bool) get_network_option( $network_id, self::OPTION_KEY_IN_USE );
  50. }
  51. /**
  52. * Creates a new application password.
  53. *
  54. * @since 5.6.0
  55. * @since 5.7.0 Returns WP_Error if application name already exists.
  56. *
  57. * @param int $user_id User ID.
  58. * @param array $args Information about the application password.
  59. * @return array|WP_Error The first key in the array is the new password, the second is its detailed information.
  60. * A WP_Error instance is returned on error.
  61. */
  62. public static function create_new_application_password( $user_id, $args = array() ) {
  63. if ( ! empty( $args['name'] ) ) {
  64. $args['name'] = sanitize_text_field( $args['name'] );
  65. }
  66. if ( empty( $args['name'] ) ) {
  67. return new WP_Error( 'application_password_empty_name', __( 'An application name is required to create an application password.' ), array( 'status' => 400 ) );
  68. }
  69. if ( self::application_name_exists_for_user( $user_id, $args['name'] ) ) {
  70. return new WP_Error( 'application_password_duplicate_name', __( 'Each application name should be unique.' ), array( 'status' => 409 ) );
  71. }
  72. $new_password = wp_generate_password( static::PW_LENGTH, false );
  73. $hashed_password = wp_hash_password( $new_password );
  74. $new_item = array(
  75. 'uuid' => wp_generate_uuid4(),
  76. 'app_id' => empty( $args['app_id'] ) ? '' : $args['app_id'],
  77. 'name' => $args['name'],
  78. 'password' => $hashed_password,
  79. 'created' => time(),
  80. 'last_used' => null,
  81. 'last_ip' => null,
  82. );
  83. $passwords = static::get_user_application_passwords( $user_id );
  84. $passwords[] = $new_item;
  85. $saved = static::set_user_application_passwords( $user_id, $passwords );
  86. if ( ! $saved ) {
  87. return new WP_Error( 'db_error', __( 'Could not save application password.' ) );
  88. }
  89. $network_id = get_main_network_id();
  90. if ( ! get_network_option( $network_id, self::OPTION_KEY_IN_USE ) ) {
  91. update_network_option( $network_id, self::OPTION_KEY_IN_USE, true );
  92. }
  93. /**
  94. * Fires when an application password is created.
  95. *
  96. * @since 5.6.0
  97. *
  98. * @param int $user_id The user ID.
  99. * @param array $new_item The details about the created password.
  100. * @param string $new_password The unhashed generated app password.
  101. * @param array $args Information used to create the application password.
  102. */
  103. do_action( 'wp_create_application_password', $user_id, $new_item, $new_password, $args );
  104. return array( $new_password, $new_item );
  105. }
  106. /**
  107. * Gets a user's application passwords.
  108. *
  109. * @since 5.6.0
  110. *
  111. * @param int $user_id User ID.
  112. * @return array The list of app passwords.
  113. */
  114. public static function get_user_application_passwords( $user_id ) {
  115. $passwords = get_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, true );
  116. if ( ! is_array( $passwords ) ) {
  117. return array();
  118. }
  119. $save = false;
  120. foreach ( $passwords as $i => $password ) {
  121. if ( ! isset( $password['uuid'] ) ) {
  122. $passwords[ $i ]['uuid'] = wp_generate_uuid4();
  123. $save = true;
  124. }
  125. }
  126. if ( $save ) {
  127. static::set_user_application_passwords( $user_id, $passwords );
  128. }
  129. return $passwords;
  130. }
  131. /**
  132. * Gets a user's application password with the given uuid.
  133. *
  134. * @since 5.6.0
  135. *
  136. * @param int $user_id User ID.
  137. * @param string $uuid The password's uuid.
  138. * @return array|null The application password if found, null otherwise.
  139. */
  140. public static function get_user_application_password( $user_id, $uuid ) {
  141. $passwords = static::get_user_application_passwords( $user_id );
  142. foreach ( $passwords as $password ) {
  143. if ( $password['uuid'] === $uuid ) {
  144. return $password;
  145. }
  146. }
  147. return null;
  148. }
  149. /**
  150. * Checks if application name exists for this user.
  151. *
  152. * @since 5.7.0
  153. *
  154. * @param int $user_id User ID.
  155. * @param string $name Application name.
  156. * @return bool Whether provided application name exists or not.
  157. */
  158. public static function application_name_exists_for_user( $user_id, $name ) {
  159. $passwords = static::get_user_application_passwords( $user_id );
  160. foreach ( $passwords as $password ) {
  161. if ( strtolower( $password['name'] ) === strtolower( $name ) ) {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. /**
  168. * Updates an application password.
  169. *
  170. * @since 5.6.0
  171. *
  172. * @param int $user_id User ID.
  173. * @param string $uuid The password's uuid.
  174. * @param array $update Information about the application password to update.
  175. * @return true|WP_Error True if successful, otherwise a WP_Error instance is returned on error.
  176. */
  177. public static function update_application_password( $user_id, $uuid, $update = array() ) {
  178. $passwords = static::get_user_application_passwords( $user_id );
  179. foreach ( $passwords as &$item ) {
  180. if ( $item['uuid'] !== $uuid ) {
  181. continue;
  182. }
  183. if ( ! empty( $update['name'] ) ) {
  184. $update['name'] = sanitize_text_field( $update['name'] );
  185. }
  186. $save = false;
  187. if ( ! empty( $update['name'] ) && $item['name'] !== $update['name'] ) {
  188. $item['name'] = $update['name'];
  189. $save = true;
  190. }
  191. if ( $save ) {
  192. $saved = static::set_user_application_passwords( $user_id, $passwords );
  193. if ( ! $saved ) {
  194. return new WP_Error( 'db_error', __( 'Could not save application password.' ) );
  195. }
  196. }
  197. /**
  198. * Fires when an application password is updated.
  199. *
  200. * @since 5.6.0
  201. *
  202. * @param int $user_id The user ID.
  203. * @param array $item The updated app password details.
  204. * @param array $update The information to update.
  205. */
  206. do_action( 'wp_update_application_password', $user_id, $item, $update );
  207. return true;
  208. }
  209. return new WP_Error( 'application_password_not_found', __( 'Could not find an application password with that id.' ) );
  210. }
  211. /**
  212. * Records that an application password has been used.
  213. *
  214. * @since 5.6.0
  215. *
  216. * @param int $user_id User ID.
  217. * @param string $uuid The password's uuid.
  218. * @return true|WP_Error True if the usage was recorded, a WP_Error if an error occurs.
  219. */
  220. public static function record_application_password_usage( $user_id, $uuid ) {
  221. $passwords = static::get_user_application_passwords( $user_id );
  222. foreach ( $passwords as &$password ) {
  223. if ( $password['uuid'] !== $uuid ) {
  224. continue;
  225. }
  226. // Only record activity once a day.
  227. if ( $password['last_used'] + DAY_IN_SECONDS > time() ) {
  228. return true;
  229. }
  230. $password['last_used'] = time();
  231. $password['last_ip'] = $_SERVER['REMOTE_ADDR'];
  232. $saved = static::set_user_application_passwords( $user_id, $passwords );
  233. if ( ! $saved ) {
  234. return new WP_Error( 'db_error', __( 'Could not save application password.' ) );
  235. }
  236. return true;
  237. }
  238. // Specified Application Password not found!
  239. return new WP_Error( 'application_password_not_found', __( 'Could not find an application password with that id.' ) );
  240. }
  241. /**
  242. * Deletes an application password.
  243. *
  244. * @since 5.6.0
  245. *
  246. * @param int $user_id User ID.
  247. * @param string $uuid The password's uuid.
  248. * @return true|WP_Error Whether the password was successfully found and deleted, a WP_Error otherwise.
  249. */
  250. public static function delete_application_password( $user_id, $uuid ) {
  251. $passwords = static::get_user_application_passwords( $user_id );
  252. foreach ( $passwords as $key => $item ) {
  253. if ( $item['uuid'] === $uuid ) {
  254. unset( $passwords[ $key ] );
  255. $saved = static::set_user_application_passwords( $user_id, $passwords );
  256. if ( ! $saved ) {
  257. return new WP_Error( 'db_error', __( 'Could not delete application password.' ) );
  258. }
  259. /**
  260. * Fires when an application password is deleted.
  261. *
  262. * @since 5.6.0
  263. *
  264. * @param int $user_id The user ID.
  265. * @param array $item The data about the application password.
  266. */
  267. do_action( 'wp_delete_application_password', $user_id, $item );
  268. return true;
  269. }
  270. }
  271. return new WP_Error( 'application_password_not_found', __( 'Could not find an application password with that id.' ) );
  272. }
  273. /**
  274. * Deletes all application passwords for the given user.
  275. *
  276. * @since 5.6.0
  277. *
  278. * @param int $user_id User ID.
  279. * @return int|WP_Error The number of passwords that were deleted or a WP_Error on failure.
  280. */
  281. public static function delete_all_application_passwords( $user_id ) {
  282. $passwords = static::get_user_application_passwords( $user_id );
  283. if ( $passwords ) {
  284. $saved = static::set_user_application_passwords( $user_id, array() );
  285. if ( ! $saved ) {
  286. return new WP_Error( 'db_error', __( 'Could not delete application passwords.' ) );
  287. }
  288. foreach ( $passwords as $item ) {
  289. /** This action is documented in wp-includes/class-wp-application-passwords.php */
  290. do_action( 'wp_delete_application_password', $user_id, $item );
  291. }
  292. return count( $passwords );
  293. }
  294. return 0;
  295. }
  296. /**
  297. * Sets a users application passwords.
  298. *
  299. * @since 5.6.0
  300. *
  301. * @param int $user_id User ID.
  302. * @param array $passwords Application passwords.
  303. *
  304. * @return bool
  305. */
  306. protected static function set_user_application_passwords( $user_id, $passwords ) {
  307. return update_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, $passwords );
  308. }
  309. /**
  310. * Sanitizes and then splits a password into smaller chunks.
  311. *
  312. * @since 5.6.0
  313. *
  314. * @param string $raw_password The raw application password.
  315. * @return string The chunked password.
  316. */
  317. public static function chunk_password( $raw_password ) {
  318. $raw_password = preg_replace( '/[^a-z\d]/i', '', $raw_password );
  319. return trim( chunk_split( $raw_password, 4, ' ' ) );
  320. }
  321. }