class-wp-application-passwords.php 12 KB

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