Ei kuvausta

class-wp-application-passwords.php 12KB

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