From f2b741bc3687ab394f2511c9f770c976b8e50346 Mon Sep 17 00:00:00 2001 From: Lakshyajeet Singh Goyal Date: Wed, 29 Apr 2026 14:27:22 +0530 Subject: [PATCH 01/10] feat: add expiry rest param for application password --- src/wp-includes/class-wp-application-passwords.php | 9 +++++++++ ...lass-wp-rest-application-passwords-controller.php | 12 ++++++++++++ src/wp-includes/user.php | 7 +++++++ 3 files changed, 28 insertions(+) diff --git a/src/wp-includes/class-wp-application-passwords.php b/src/wp-includes/class-wp-application-passwords.php index 6e84e0a2b2d0b..c7db73ae4f2c8 100644 --- a/src/wp-includes/class-wp-application-passwords.php +++ b/src/wp-includes/class-wp-application-passwords.php @@ -106,6 +106,7 @@ public static function create_new_application_password( $user_id, $args = array( 'created' => time(), 'last_used' => null, 'last_ip' => null, + 'expires' => isset( $args['expires'] ) ? (int) $args['expires'] : null, ); $passwords = static::get_user_application_passwords( $user_id ); @@ -282,6 +283,14 @@ public static function update_application_password( $user_id, $uuid, $update = a $save = false; + if ( array_key_exists( 'expires', $update ) ) { + $expires = null === $update['expires'] ? null : (int) $update['expires']; + if ( ! array_key_exists( 'expires', $item ) || $item['expires'] !== $expires ) { + $item['expires'] = $expires; + $save = true; + } + } + if ( ! empty( $update['name'] ) && $item['name'] !== $update['name'] ) { $item['name'] = $update['name']; $save = true; diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php index 767917d6f6fd0..bf08d4aa52a29 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php @@ -583,6 +583,11 @@ protected function prepare_item_for_database( $request ) { $prepared->app_id = $request['app_id']; } + $prepared->expires = null; + if ( isset( $request['expires'] ) && null !== $request['expires'] ) { + $prepared->expires = rest_parse_date( $request['expires'] ); + } + /** * Filters an application password before it is inserted via the REST API. * @@ -619,6 +624,7 @@ public function prepare_item_for_response( $item, $request ) { 'created' => gmdate( 'Y-m-d\TH:i:s', $item['created'] ), 'last_used' => $item['last_used'] ? gmdate( 'Y-m-d\TH:i:s', $item['last_used'] ) : null, 'last_ip' => $item['last_ip'] ? $item['last_ip'] : null, + 'expires' => ! empty( $item['expires'] ) ? gmdate( 'Y-m-d\TH:i:s', $item['expires'] ) : null, ); if ( isset( $item['new_password'] ) ) { @@ -849,6 +855,12 @@ public function get_item_schema() { 'context' => array( 'view', 'edit' ), 'readonly' => true, ), + 'expires' => array( + 'description' => __( 'The GMT date the application password expires.' ), + 'type' => array( 'string', 'null' ), + 'format' => 'date-time', + 'context' => array( 'view', 'edit' ), + ), ), ); diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 9c635f63d288a..ec9347dd6eaa1 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -463,6 +463,13 @@ function wp_authenticate_application_password( $error = new WP_Error(); + if ( ! empty( $item['expires'] ) && time() > $item['expires'] ) { + $error->add( + 'application_password_expired', + __( 'The provided application password has expired.' ) + ); + } + /** * Fires when an application password has been successfully checked as valid. * From 2b9d1490d1681fc4f74a9dc998058253d3089197 Mon Sep 17 00:00:00 2001 From: Lakshyajeet Singh Goyal Date: Wed, 29 Apr 2026 15:03:37 +0530 Subject: [PATCH 02/10] feat: display application password expiry on the frontend --- .../_enqueues/admin/application-passwords.js | 12 ++++- ...ss-wp-application-passwords-list-table.php | 49 +++++++++++++++++++ src/wp-admin/user-edit.php | 6 +++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/js/_enqueues/admin/application-passwords.js b/src/js/_enqueues/admin/application-passwords.js index c79cdb8b4037f..5a951fd4e4003 100644 --- a/src/js/_enqueues/admin/application-passwords.js +++ b/src/js/_enqueues/admin/application-passwords.js @@ -5,7 +5,8 @@ ( function( $ ) { var $appPassSection = $( '#application-passwords-section' ), $newAppPassForm = $appPassSection.find( '.create-application-password' ), - $newAppPassField = $newAppPassForm.find( '.input' ), + $newAppPassField = $newAppPassForm.find( '#new_application_password_name' ), + $newAppPassExpiresField = $newAppPassForm.find( '#new_application_password_expires' ), $newAppPassButton = $newAppPassForm.find( '.button' ), $appPassTwrapper = $appPassSection.find( '.application-passwords-list-table-wrapper' ), $appPassTbody = $appPassSection.find( 'tbody' ), @@ -36,6 +37,15 @@ name: name }; + var expires = $newAppPassExpiresField.val(); + if ( expires ) { + var expiresDate = new Date( expires ); + + if ( ! isNaN( expiresDate.getTime() ) ) { + request.expires = expiresDate.toISOString(); + } + } + /** * Filters the request data used to create a new Application Password. * diff --git a/src/wp-admin/includes/class-wp-application-passwords-list-table.php b/src/wp-admin/includes/class-wp-application-passwords-list-table.php index 9a60853016fc5..a15495e881009 100644 --- a/src/wp-admin/includes/class-wp-application-passwords-list-table.php +++ b/src/wp-admin/includes/class-wp-application-passwords-list-table.php @@ -29,6 +29,7 @@ public function get_columns() { 'created' => __( 'Created' ), 'last_used' => __( 'Last Used' ), 'last_ip' => __( 'Last IP' ), + 'expires' => __( 'Expires' ), 'revoke' => __( 'Revoke' ), ); } @@ -101,6 +102,35 @@ public function column_last_ip( $item ) { } } + /** + * Handles the expires column output. + * + * @since 7.1.0 + * + * @param array $item The current application password item. + */ + public function column_expires( $item ) { + if ( empty( $item['expires'] ) ) { + echo '—'; + } else { + $date = date_i18n( __( 'F j, Y' ), $item['expires'] ); + if ( time() > $item['expires'] ) { + printf( + '%s', + /* translators: %s: Expiration date for the Application Password. */ + sprintf( __( 'Expired on %s' ), $date ) + ); + } else { + echo $date; + } + } + printf( + '
', + esc_attr__( 'Edit Application Password Expiration Date' ), + esc_html__( 'Edit Expiry' ) + ); + } + /** * Handles the revoke column output. * @@ -230,6 +260,25 @@ public function print_js_template_row() { case 'last_ip': echo "{{ data.last_ip || '—' }}"; break; + case 'expires': + ?> + <# if ( data.expires ) { #> + <# var isExpired = new Date().getTime() > new Date( data.expires ).getTime(); #> + <# var formattedDate = wp.date.dateI18n( , data.expires ); #> + <# if ( isExpired ) { #> + + <# } else { #> + {{ formattedDate }} + <# } #> + <# } else { #> + — + <# } #> +
+ %2$s', diff --git a/src/wp-admin/user-edit.php b/src/wp-admin/user-edit.php index c25380a93ee91..84fe33f698013 100644 --- a/src/wp-admin/user-edit.php +++ b/src/wp-admin/user-edit.php @@ -837,6 +837,12 @@

+
+ + +

+
+ Date: Wed, 29 Apr 2026 18:27:31 +0530 Subject: [PATCH 03/10] feat: allow application password expiry editing from the frontend --- .../_enqueues/admin/application-passwords.js | 80 +++++++++++++++++++ src/wp-admin/css/forms.css | 22 +++++ 2 files changed, 102 insertions(+) diff --git a/src/js/_enqueues/admin/application-passwords.js b/src/js/_enqueues/admin/application-passwords.js index 5a951fd4e4003..e59402c3e3071 100644 --- a/src/js/_enqueues/admin/application-passwords.js +++ b/src/js/_enqueues/admin/application-passwords.js @@ -64,6 +64,7 @@ $newAppPassButton.removeProp( 'aria-disabled' ).removeClass( 'disabled' ); } ).done( function( response ) { $newAppPassField.val( '' ); + $newAppPassExpiresField.val( '' ); $newAppPassButton.prop( 'disabled', false ); $newAppPassForm.after( tmplNewAppPass( { @@ -87,6 +88,85 @@ */ wp.hooks.doAction( 'wp_application_passwords_created_password', response, request ); } ).fail( handleErrorResponse ); + }); + + /** + * Handles the inline editing of an application password expiration date. + * * @since 7.1.0 + */ + $appPassTbody.on( 'click', '.edit-expires', function( e ) { + e.preventDefault(); + + var $button = $( this ), + $tr = $button.closest( 'tr' ), + uuid = $tr.data( 'uuid' ), + currentExpires = $tr.data( 'expires' ), + $td = $button.closest( 'td' ); + + if ( $td.find( '.edit-expires-form' ).length ) { + return; + } + + var $form = $( '
' ); + var $input = $( '' ); + + if ( currentExpires ) { + $input.val( currentExpires.split( 'T' )[0] ); + } + + var $buttonContainer = $( '
' ); + var $saveBtn = $( '' ); + var $cancelBtn = $( '' ); + + $buttonContainer.append( $saveBtn ).append( $cancelBtn ); + $form.append( $input ).append( $buttonContainer ); + + $td.append( $form ); + $button.hide(); + $input.trigger( 'focus' ); + + // Close form on Escape key. + $input.on( 'keydown', function( e ) { + if ( 27 === e.which ) { + $cancelBtn.trigger( 'click' ); + } + if ( 13 === e.which ) { + e.preventDefault(); + $saveBtn.trigger( 'click' ); + } + }); + + $cancelBtn.on( 'click', function() { + $form.remove(); + $button.show().trigger( 'focus' ); + } ); + + $saveBtn.on( 'click', function() { + var newExpires = $input.val(); + var expiresDate = newExpires ? new Date( newExpires ) : null; + var requestData = { + expires: ( expiresDate && ! isNaN( expiresDate.getTime() ) ) ? expiresDate.toISOString() : null + }; + + clearNotices(); + $saveBtn.prop( 'disabled', true ); + $cancelBtn.prop( 'disabled', true ); + + wp.apiRequest( { + path: '/wp/v2/users/' + userId + '/application-passwords/' + uuid + '?_locale=user', + method: 'PUT', + data: JSON.stringify( requestData ), + contentType: 'application/json' + } ).always( function() { + $saveBtn.prop( 'disabled', false ); + $cancelBtn.prop( 'disabled', false ); + } ).done( function( response ) { + var $newRow = $( tmplAppPassRow( response ) ); + $tr.replaceWith( $newRow ); + $newRow.find( '.edit-expires' ).trigger( 'focus' ); + addNotice( wp.i18n.__( 'Application password expiration updated.' ), 'success' ); + } ).fail( handleErrorResponse ); + } ); } ); $appPassTbody.on( 'click', '.delete', function( e ) { diff --git a/src/wp-admin/css/forms.css b/src/wp-admin/css/forms.css index b48825a1ef5a3..187215c9c0cfb 100644 --- a/src/wp-admin/css/forms.css +++ b/src/wp-admin/css/forms.css @@ -1077,6 +1077,28 @@ table.form-table td .updated p { max-width: 20em; } +.edit-expires-form { + margin-top: 8px; + width: 100%; +} + +.edit-expires-input { + display: block; + width: 100%; + max-width: 150px; + margin-bottom: 5px; +} + +.edit-expires-button-group { + display: flex; + gap: 4px; + justify-content: flex-start; +} + +.column-expires { + vertical-align: top; +} + .auth-app-card.card { max-width: 768px; } From 3d31c0da955757a3ebb3d9a0816ce09a328e0681 Mon Sep 17 00:00:00 2001 From: Lakshyajeet Singh Goyal Date: Thu, 30 Apr 2026 12:27:33 +0530 Subject: [PATCH 04/10] test: add unit tests for appication password expiry --- tests/phpunit/tests/auth.php | 20 ++++++++++++++++ .../tests/rest-api/application-passwords.php | 23 ++++++++++++++++--- .../rest-application-passwords-controller.php | 10 +++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php index a290d11e118e6..1fdb846c707ac 100644 --- a/tests/phpunit/tests/auth.php +++ b/tests/phpunit/tests/auth.php @@ -1767,6 +1767,26 @@ public function test_authenticate_application_password_chunked() { $this->assertSame( self::$user_id, $user->ID ); } + /** + * @ticket 53995 + */ + public function test_authenticate_application_password_expired() { + add_filter( 'application_password_is_api_request', '__return_true' ); + add_filter( 'wp_is_application_passwords_available', '__return_true' ); + + list( $password ) = WP_Application_Passwords::create_new_application_password( + self::$user_id, + array( + 'name' => 'phpunit', + 'expires' => time() - DAY_IN_SECONDS, + ) + ); + + $error = wp_authenticate_application_password( null, self::$_user->user_login, $password ); + $this->assertWPError( $error ); + $this->assertSame( 'application_password_expired', $error->get_error_code() ); + } + /** * @ticket 51939 */ diff --git a/tests/phpunit/tests/rest-api/application-passwords.php b/tests/phpunit/tests/rest-api/application-passwords.php index 65e3bf222d85f..a831407075bb5 100644 --- a/tests/phpunit/tests/rest-api/application-passwords.php +++ b/tests/phpunit/tests/rest-api/application-passwords.php @@ -95,7 +95,7 @@ public function test_create_new_application_password( array $args, array $names $this->assertNotEmpty( $new_password ); $this->assertSame( - array( 'uuid', 'app_id', 'name', 'password', 'created', 'last_used', 'last_ip' ), + array( 'uuid', 'app_id', 'name', 'password', 'created', 'last_used', 'last_ip', 'expires' ), array_keys( $new_item ) ); $this->assertSame( $args['name'], $new_item['name'] ); @@ -106,10 +106,16 @@ public function data_create_new_application_password() { 'should create new password when no passwords exists' => array( 'args' => array( 'name' => 'test3' ), ), - 'should create new password when name is unique' => array( + 'should create new password when name is unique' => array( 'args' => array( 'name' => 'test3' ), 'names' => array( 'test1', 'test2' ), ), + 'should create new password with expiration' => array( + 'args' => array( + 'name' => 'test_expire', + 'expires' => time() + DAY_IN_SECONDS, + ), + ), ); } @@ -154,7 +160,7 @@ public function test_update_application_password( array $update, array $existing // Check updated only given values. $updated_item = WP_Application_Passwords::get_user_application_password( self::$user_id, $uuid ); foreach ( $updated_item as $key => $update_value ) { - $expected_value = $update[ $key ] ?? $original_item[ $key ]; + $expected_value = array_key_exists( $key, $update ) ? $update[ $key ] : $original_item[ $key ]; $this->assertSame( $expected_value, $update_value ); } } @@ -186,6 +192,17 @@ public function data_update_application_password() { 'update' => array( 'name' => 'Test Updated' ), 'existing' => array( 'name' => 'Test' ), ), + 'should update expires' => array( + 'update' => array( 'expires' => time() + DAY_IN_SECONDS ), + 'existing' => array( 'name' => 'Test' ), + ), + 'should clear expires' => array( + 'update' => array( 'expires' => null ), + 'existing' => array( + 'name' => 'Test', + 'expires' => time() + DAY_IN_SECONDS, + ), + ), ); } diff --git a/tests/phpunit/tests/rest-api/rest-application-passwords-controller.php b/tests/phpunit/tests/rest-api/rest-application-passwords-controller.php index 060a5c0912a94..287d649e2ba19 100644 --- a/tests/phpunit/tests/rest-api/rest-application-passwords-controller.php +++ b/tests/phpunit/tests/rest-api/rest-application-passwords-controller.php @@ -906,6 +906,7 @@ protected function check_response( $response, $item, $password = false ) { $this->assertArrayHasKey( 'created', $response ); $this->assertArrayHasKey( 'last_used', $response ); $this->assertArrayHasKey( 'last_ip', $response ); + $this->assertArrayHasKey( 'expires', $response ); $this->assertSame( $item['uuid'], $response['uuid'] ); $this->assertSame( $item['app_id'], $response['app_id'] ); @@ -924,6 +925,12 @@ protected function check_response( $response, $item, $password = false ) { $this->assertNull( $response['last_ip'] ); } + if ( ! empty( $item['expires'] ) ) { + $this->assertSame( gmdate( 'Y-m-d\TH:i:s', $item['expires'] ), $response['expires'] ); + } else { + $this->assertNull( $response['expires'] ); + } + if ( $password ) { $this->assertArrayHasKey( 'password', $response ); } else { @@ -947,7 +954,8 @@ public function test_get_item_schema() { $this->assertArrayHasKey( 'created', $properties ); $this->assertArrayHasKey( 'last_used', $properties ); $this->assertArrayHasKey( 'last_ip', $properties ); - $this->assertCount( 7, $properties ); + $this->assertArrayHasKey( 'expires', $properties ); + $this->assertCount( 8, $properties ); } /** From 1a5623aba33580a1250df40896344b2b7094c8d4 Mon Sep 17 00:00:00 2001 From: Lakshyajeet Singh Goyal Date: Thu, 30 Apr 2026 12:56:03 +0530 Subject: [PATCH 05/10] test: add e2e tests to add and edit application password expiry --- .../profile/applications-passwords.test.js | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/e2e/specs/profile/applications-passwords.test.js b/tests/e2e/specs/profile/applications-passwords.test.js index f6c5cf7a781b7..27ee00feb8afc 100644 --- a/tests/e2e/specs/profile/applications-passwords.test.js +++ b/tests/e2e/specs/profile/applications-passwords.test.js @@ -40,6 +40,56 @@ test.describe( 'Manage applications passwords', () => { ); } ); + test('should correctly create a new application password with expiration', async ( { + page, + applicationPasswords + } ) => { + const expiresDate = new Date(); + expiresDate.setDate( expiresDate.getDate() + 7 ); + const expiresString = expiresDate.toISOString().split( 'T' )[ 0 ]; + + await applicationPasswords.create( TEST_APPLICATION_NAME, expiresString ); + + const [ app ] = await applicationPasswords.get(); + expect( app['name'] ).toBe( TEST_APPLICATION_NAME ); + expect( app['expires'] ).not.toBeNull(); + expect( app['expires'].startsWith( expiresString ) ).toBe( true ); + + const successMessage = page.getByRole( 'alert' ); + await expect( successMessage ).toHaveClass( /notice-success/ ); + } ); + + test('should correctly update an application password expiration date', async ( { + page, + applicationPasswords + } ) => { + await applicationPasswords.create(); + + const [ app ] = await applicationPasswords.get(); + expect( app['expires'] ).toBeNull(); + + const editButton = page.getByRole( 'button', { name: 'Edit Expiration Date' } ); + await expect( editButton ).toBeVisible(); + await editButton.click(); + + const expiresInput = page.locator( '.edit-expires-input' ); + await expect( expiresInput ).toBeVisible(); + + const expiresDate = new Date(); + expiresDate.setDate( expiresDate.getDate() + 10 ); + const expiresString = expiresDate.toISOString().split( 'T' )[ 0 ]; + await expiresInput.fill( expiresString ); + + const saveButton = page.getByRole( 'button', { name: 'Save' } ); + await saveButton.click(); + + await expect( page.getByRole( 'alert' ) ).toContainText( 'Application password expiration updated.' ); + + const [ updatedApp ] = await applicationPasswords.get(); + expect( updatedApp['expires'] ).not.toBeNull(); + expect( updatedApp['expires'].startsWith( expiresString ) ).toBe( true ); + } ); + test( 'should correctly revoke a single application password', async ( { page, applicationPasswords @@ -94,13 +144,19 @@ class ApplicationPasswords { this.admin = admin; } - async create(applicationName = TEST_APPLICATION_NAME) { + async create(applicationName = TEST_APPLICATION_NAME, expires = null) { await this.admin.visitAdminPage( '/profile.php' ); const newPasswordField = this.page.getByRole( 'textbox', { name: 'New Application Password Name' } ); await expect( newPasswordField ).toBeVisible(); await newPasswordField.fill( applicationName ); + if ( expires ) { + const newPasswordExpiresField = this.page.getByLabel( 'Expires on' ); + await expect( newPasswordExpiresField ).toBeVisible(); + await newPasswordExpiresField.fill( expires ); + } + await this.page.getByRole( 'button', { name: 'Add Application Password' } ).click(); await expect( this.page.getByRole( 'alert' ) ).toBeVisible(); } From 92e376ac2f8ec8c1588ffc52e32c23bf477ca16c Mon Sep 17 00:00:00 2001 From: Lakshyajeet Singh Goyal Date: Thu, 30 Apr 2026 14:01:07 +0530 Subject: [PATCH 06/10] fix: update qunit fixtures --- tests/qunit/fixtures/wp-api-generated.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 003dc397ae305..6dc369c366f5c 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -10200,6 +10200,15 @@ mockedApiResponse.Schema = { "minLength": 1, "pattern": ".*\\S.*", "required": true + }, + "expires": { + "description": "The GMT date the application password expires.", + "type": [ + "string", + "null" + ], + "format": "date-time", + "required": false } } }, @@ -10295,6 +10304,15 @@ mockedApiResponse.Schema = { "minLength": 1, "pattern": ".*\\S.*", "required": false + }, + "expires": { + "description": "The GMT date the application password expires.", + "type": [ + "string", + "null" + ], + "format": "date-time", + "required": false } } }, From 2023cdf6eea9050aa9f522d9971d4484d2914da2 Mon Sep 17 00:00:00 2001 From: shewa <30765517+shewa12@users.noreply.github.com> Date: Wed, 7 Oct 2026 16:17:14 +0600 Subject: [PATCH 07/10] Application Password: Expire date setup functionality improved Expiration preset added, past date selection issue fixed Expiration edit option removed --- .../_enqueues/admin/application-passwords.js | 170 +++++++++--------- src/wp-admin/css/forms.css | 18 -- ...ss-wp-application-passwords-list-table.php | 12 +- src/wp-admin/user-edit.php | 30 +++- .../class-wp-application-passwords.php | 43 ++++- tests/phpunit/tests/auth.php | 33 +++- .../tests/rest-api/application-passwords.php | 30 ++++ 7 files changed, 216 insertions(+), 120 deletions(-) diff --git a/src/js/_enqueues/admin/application-passwords.js b/src/js/_enqueues/admin/application-passwords.js index 5d1a759873c02..5e35aff299f8f 100644 --- a/src/js/_enqueues/admin/application-passwords.js +++ b/src/js/_enqueues/admin/application-passwords.js @@ -11,6 +11,8 @@ var $appPassSection = $( '#application-passwords-section' ), $newAppPassForm = $appPassSection.find( '.create-application-password' ), $newAppPassField = $newAppPassForm.find( '#new_application_password_name' ), + $newAppPassPresetField = $newAppPassForm.find( '#new_application_password_expiration_preset' ), + $newAppPassCustomWrap = $newAppPassForm.find( '#new_application_password_custom_expires_wrap' ), $newAppPassExpiresField = $newAppPassForm.find( '#new_application_password_expires' ), $newAppPassButton = $newAppPassForm.find( '.button' ), $appPassTwrapper = $appPassSection.find( '.application-passwords-list-table-wrapper' ), @@ -19,7 +21,82 @@ $removeAllBtn = $( '#revoke-all-application-passwords' ), tmplNewAppPass = wp.template( 'new-application-password' ), tmplAppPassRow = wp.template( 'application-password-row' ), - userId = $( '#user_id' ).val(); + userId = $( '#user_id' ).val(), + settings = window.wpApplicationPasswordsSettings || {}; + + $newAppPassPresetField.on( 'change', function() { + if ( 'custom' === $( this ).val() ) { + $newAppPassCustomWrap.removeClass( 'hidden' ); + $newAppPassExpiresField.trigger( 'focus' ); + } else { + $newAppPassCustomWrap.addClass( 'hidden' ); + $newAppPassExpiresField.val( '' ); + } + } ); + + /** + * Calculates the ISO string for expiration based on selected preset or custom date. + * + * Expiration is set to 23:59:59 of the target day in the site's local timezone. + * + * @since 7.2.0 + * @return {string|null} The ISO 8601 date string, or null if no expiration or invalid. + */ + function calculateExpiresIso() { + var preset = $newAppPassPresetField.val(), + days = 0, + baseDateIso = settings.todayEndIso, + baseDate; + + if ( 'no_expiry' === preset ) { + return null; + } + + if ( 'custom' === preset ) { + var customVal = $newAppPassExpiresField.val(); + if ( ! customVal ) { + return null; + } + + // Format custom date string into ISO for end of day in site timezone. + var siteTimezone = wp.date.getSettings().timezone; + var offsetString = '+00:00'; + if ( siteTimezone && siteTimezone.offsetFormatted ) { + offsetString = siteTimezone.offsetFormatted; + // Format to standard ISO offset like +06:00 or -05:00. + if ( ! /^[-+]\d{2}:\d{2}$/.test( offsetString ) ) { + var numOffset = parseFloat( siteTimezone.offset || 0 ); + var sign = numOffset >= 0 ? '+' : '-'; + var absH = Math.floor( Math.abs( numOffset ) ); + var absM = Math.round( ( Math.abs( numOffset ) - absH ) * 60 ); + offsetString = sign + ( '0' + absH ).slice( -2 ) + ':' + ( '0' + absM ).slice( -2 ); + } + } + + var customIsoDate = new Date( customVal + 'T23:59:59' + offsetString ); + if ( ! isNaN( customIsoDate.getTime() ) ) { + return customIsoDate.toISOString(); + } + + return null; + } + + if ( '7_days' === preset ) { + days = 7; + } else if ( '30_days' === preset ) { + days = 30; + } else if ( '90_days' === preset ) { + days = 90; + } + + if ( days > 0 ) { + baseDate = baseDateIso ? new Date( baseDateIso ) : new Date(); + baseDate.setUTCDate( baseDate.getUTCDate() + days ); + return baseDate.toISOString(); + } + + return null; + } $newAppPassButton.on( 'click', function( e ) { e.preventDefault(); @@ -42,13 +119,9 @@ name: name }; - var expires = $newAppPassExpiresField.val(); - if ( expires ) { - var expiresDate = new Date( expires ); - - if ( ! isNaN( expiresDate.getTime() ) ) { - request.expires = expiresDate.toISOString(); - } + var expiresIso = calculateExpiresIso(); + if ( expiresIso ) { + request.expires = expiresIso; } /** @@ -69,6 +142,8 @@ $newAppPassButton.removeProp( 'aria-disabled' ).removeClass( 'disabled' ); } ).done( function( response ) { $newAppPassField.val( '' ); + $newAppPassPresetField.val( '30_days' ); + $newAppPassCustomWrap.addClass( 'hidden' ); $newAppPassExpiresField.val( '' ); $newAppPassButton.prop( 'disabled', false ); @@ -95,85 +170,6 @@ } ).fail( handleErrorResponse ); }); - /** - * Handles the inline editing of an application password expiration date. - * * @since 7.1.0 - */ - $appPassTbody.on( 'click', '.edit-expires', function( e ) { - e.preventDefault(); - - var $button = $( this ), - $tr = $button.closest( 'tr' ), - uuid = $tr.data( 'uuid' ), - currentExpires = $tr.data( 'expires' ), - $td = $button.closest( 'td' ); - - if ( $td.find( '.edit-expires-form' ).length ) { - return; - } - - var $form = $( '
' ); - var $input = $( '' ); - - if ( currentExpires ) { - $input.val( currentExpires.split( 'T' )[0] ); - } - - var $buttonContainer = $( '
' ); - var $saveBtn = $( '' ); - var $cancelBtn = $( '' ); - - $buttonContainer.append( $saveBtn ).append( $cancelBtn ); - $form.append( $input ).append( $buttonContainer ); - - $td.append( $form ); - $button.hide(); - $input.trigger( 'focus' ); - - // Close form on Escape key. - $input.on( 'keydown', function( e ) { - if ( 27 === e.which ) { - $cancelBtn.trigger( 'click' ); - } - if ( 13 === e.which ) { - e.preventDefault(); - $saveBtn.trigger( 'click' ); - } - }); - - $cancelBtn.on( 'click', function() { - $form.remove(); - $button.show().trigger( 'focus' ); - } ); - - $saveBtn.on( 'click', function() { - var newExpires = $input.val(); - var expiresDate = newExpires ? new Date( newExpires ) : null; - var requestData = { - expires: ( expiresDate && ! isNaN( expiresDate.getTime() ) ) ? expiresDate.toISOString() : null - }; - - clearNotices(); - $saveBtn.prop( 'disabled', true ); - $cancelBtn.prop( 'disabled', true ); - - wp.apiRequest( { - path: '/wp/v2/users/' + userId + '/application-passwords/' + uuid + '?_locale=user', - method: 'PUT', - data: JSON.stringify( requestData ), - contentType: 'application/json' - } ).always( function() { - $saveBtn.prop( 'disabled', false ); - $cancelBtn.prop( 'disabled', false ); - } ).done( function( response ) { - var $newRow = $( tmplAppPassRow( response ) ); - $tr.replaceWith( $newRow ); - $newRow.find( '.edit-expires' ).trigger( 'focus' ); - addNotice( wp.i18n.__( 'Application password expiration updated.' ), 'success' ); - } ).fail( handleErrorResponse ); - } ); - } ); - $appPassTbody.on( 'click', '.delete', function( e ) { e.preventDefault(); diff --git a/src/wp-admin/css/forms.css b/src/wp-admin/css/forms.css index 8cdf87b743cc8..92101bde6ccd0 100644 --- a/src/wp-admin/css/forms.css +++ b/src/wp-admin/css/forms.css @@ -1075,24 +1075,6 @@ table.form-table td .updated p { max-width: 20em; } -.edit-expires-form { - margin-top: 8px; - width: 100%; -} - -.edit-expires-input { - display: block; - width: 100%; - max-width: 150px; - margin-bottom: 5px; -} - -.edit-expires-button-group { - display: flex; - gap: 4px; - justify-content: flex-start; -} - .column-expires { vertical-align: top; } diff --git a/src/wp-admin/includes/class-wp-application-passwords-list-table.php b/src/wp-admin/includes/class-wp-application-passwords-list-table.php index e4912e82c9b65..050d37713b49f 100644 --- a/src/wp-admin/includes/class-wp-application-passwords-list-table.php +++ b/src/wp-admin/includes/class-wp-application-passwords-list-table.php @@ -105,7 +105,7 @@ public function column_last_ip( $item ) { /** * Handles the expires column output. * - * @since 7.1.0 + * @since 7.2.0 * * @param array $item The current application password item. */ @@ -118,17 +118,12 @@ public function column_expires( $item ) { printf( '%s', /* translators: %s: Expiration date for the Application Password. */ - sprintf( __( 'Expired on %s' ), $date ) + sprintf( esc_html__( 'Expired on %s' ), esc_html( $date ) ) ); } else { - echo $date; + echo esc_html( $date ); } } - printf( - '
', - esc_attr__( 'Edit Application Password Expiration Date' ), - esc_html__( 'Edit Expiry' ) - ); } /** @@ -281,7 +276,6 @@ public function print_js_template_row() { <# } else { #> — <# } #> -
wp_date( 'Y-m-d' ), + 'todayEndIso' => wp_date( 'c', strtotime( 'today 23:59:59', current_datetime()->getTimestamp() ) ), + ) + ); } if ( IS_PROFILE_PAGE ) { @@ -848,10 +856,26 @@

+
- - -

+ + +
+ + Array of preset keys and translatable labels. + */ + public static function get_expiry_presets() { + return array( + self::EXPIRY_7_DAYS => __( '7 days' ), + self::EXPIRY_30_DAYS => __( '30 days' ), + self::EXPIRY_90_DAYS => __( '90 days' ), + self::EXPIRY_NO_EXPIRY => __( 'No expiry' ), + self::EXPIRY_CUSTOM => __( 'Custom date' ), + ); + } + /** * Checks if application passwords are being used by the site. * @@ -61,6 +89,7 @@ public static function is_in_use() { * @since 5.6.0 * @since 5.7.0 Returns WP_Error if application name already exists. * @since 6.8.0 The hashed password value now uses wp_fast_hash() instead of phpass. + * @since 7.2.0 The application password expiration functionality added. * * @param int $user_id User ID. * @param array $args { @@ -98,6 +127,18 @@ public static function create_new_application_password( $user_id, $args = array( $new_password = wp_generate_password( static::PW_LENGTH, false ); $hashed_password = self::hash_password( $new_password ); + $expires = null; + if ( ! empty( $args['expires'] ) ) { + $expires = (int) $args['expires']; + if ( $expires <= time() ) { + return new WP_Error( + 'application_password_past_expiration', + __( 'Application password expiration date must be in the future.' ), + array( 'status' => 400 ) + ); + } + } + $new_item = array( 'uuid' => wp_generate_uuid4(), 'app_id' => empty( $args['app_id'] ) ? '' : $args['app_id'], @@ -106,7 +147,7 @@ public static function create_new_application_password( $user_id, $args = array( 'created' => time(), 'last_used' => null, 'last_ip' => null, - 'expires' => isset( $args['expires'] ) ? (int) $args['expires'] : null, + 'expires' => $expires, ); $passwords = static::get_user_application_passwords( $user_id ); diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php index 1fdb846c707ac..3480620e91b16 100644 --- a/tests/phpunit/tests/auth.php +++ b/tests/phpunit/tests/auth.php @@ -1774,19 +1774,48 @@ public function test_authenticate_application_password_expired() { add_filter( 'application_password_is_api_request', '__return_true' ); add_filter( 'wp_is_application_passwords_available', '__return_true' ); - list( $password ) = WP_Application_Passwords::create_new_application_password( + // Create with valid future expiry first. + list( $password, $item ) = WP_Application_Passwords::create_new_application_password( self::$user_id, array( 'name' => 'phpunit', - 'expires' => time() - DAY_IN_SECONDS, + 'expires' => time() + DAY_IN_SECONDS, ) ); + // Simulate passage of time by updating the stored expiry to a past timestamp. + WP_Application_Passwords::update_application_password( + self::$user_id, + $item['uuid'], + array( 'expires' => time() - DAY_IN_SECONDS ) + ); + $error = wp_authenticate_application_password( null, self::$_user->user_login, $password ); $this->assertWPError( $error ); $this->assertSame( 'application_password_expired', $error->get_error_code() ); } + /** + * @ticket 53995 + */ + public function test_authenticate_application_password_with_valid_future_expiry() { + add_filter( 'application_password_is_api_request', '__return_true' ); + add_filter( 'wp_is_application_passwords_available', '__return_true' ); + + list( $password ) = WP_Application_Passwords::create_new_application_password( + self::$user_id, + array( + 'name' => 'phpunit_future', + 'expires' => time() + 7 * DAY_IN_SECONDS, + ) + ); + + $user = wp_authenticate_application_password( null, self::$_user->user_login, $password ); + $this->assertNotWPError( $user ); + $this->assertInstanceOf( WP_User::class, $user ); + $this->assertSame( self::$user_id, $user->ID ); + } + /** * @ticket 51939 */ diff --git a/tests/phpunit/tests/rest-api/application-passwords.php b/tests/phpunit/tests/rest-api/application-passwords.php index a831407075bb5..57b9d4a26626d 100644 --- a/tests/phpunit/tests/rest-api/application-passwords.php +++ b/tests/phpunit/tests/rest-api/application-passwords.php @@ -215,4 +215,34 @@ public function test_can_create_duplicate_app_password_names() { $created = WP_Application_Passwords::create_new_application_password( self::$user_id, array( 'name' => 'My App' ) ); $this->assertNotWPError( $created, 'Second attempt to create an application password should not return an error' ); } + + /** + * @ticket 53995 + */ + public function test_create_application_password_rejects_past_expiration() { + $result = WP_Application_Passwords::create_new_application_password( + self::$user_id, + array( + 'name' => 'Past Expire App', + 'expires' => time() - DAY_IN_SECONDS, + ) + ); + + $this->assertWPError( $result ); + $this->assertSame( 'application_password_past_expiration', $result->get_error_code() ); + } + + /** + * @ticket 53995 + */ + public function test_get_expiry_presets() { + $presets = WP_Application_Passwords::get_expiry_presets(); + + $this->assertIsArray( $presets ); + $this->assertArrayHasKey( WP_Application_Passwords::EXPIRY_7_DAYS, $presets ); + $this->assertArrayHasKey( WP_Application_Passwords::EXPIRY_30_DAYS, $presets ); + $this->assertArrayHasKey( WP_Application_Passwords::EXPIRY_90_DAYS, $presets ); + $this->assertArrayHasKey( WP_Application_Passwords::EXPIRY_NO_EXPIRY, $presets ); + $this->assertArrayHasKey( WP_Application_Passwords::EXPIRY_CUSTOM, $presets ); + } } From e32eaf2b3ed85203c03897afb2b36217ff82c832 Mon Sep 17 00:00:00 2001 From: shewa <30765517+shewa12@users.noreply.github.com> Date: Wed, 7 Oct 2026 17:03:38 +0600 Subject: [PATCH 08/10] Tests: Update application passwords E2E tests for presets and remove edit action --- .../profile/applications-passwords.test.js | 64 ++++++++----------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/tests/e2e/specs/profile/applications-passwords.test.js b/tests/e2e/specs/profile/applications-passwords.test.js index c5a2ad013d3ec..b921303ff92bd 100644 --- a/tests/e2e/specs/profile/applications-passwords.test.js +++ b/tests/e2e/specs/profile/applications-passwords.test.js @@ -40,54 +40,38 @@ test.describe( 'Manage applications passwords', () => { ); } ); - test('should correctly create a new application password with expiration', async ( { + test( 'should correctly create a new application password with preset expiration', async ( { page, - applicationPasswords + applicationPasswords, } ) => { - const expiresDate = new Date(); - expiresDate.setDate( expiresDate.getDate() + 7 ); - const expiresString = expiresDate.toISOString().split( 'T' )[ 0 ]; - - await applicationPasswords.create( TEST_APPLICATION_NAME, expiresString ); + await applicationPasswords.create( TEST_APPLICATION_NAME, '7_days' ); const [ app ] = await applicationPasswords.get(); - expect( app['name'] ).toBe( TEST_APPLICATION_NAME ); - expect( app['expires'] ).not.toBeNull(); - expect( app['expires'].startsWith( expiresString ) ).toBe( true ); + expect( app.name ).toBe( TEST_APPLICATION_NAME ); + expect( app.expires ).not.toBeNull(); const successMessage = page.getByRole( 'alert' ); await expect( successMessage ).toHaveClass( /notice-success/ ); } ); - test('should correctly update an application password expiration date', async ( { + test( 'should correctly create a new application password with custom expiration date', async ( { page, - applicationPasswords + applicationPasswords, } ) => { - await applicationPasswords.create(); - - const [ app ] = await applicationPasswords.get(); - expect( app['expires'] ).toBeNull(); - - const editButton = page.getByRole( 'button', { name: 'Edit Expiration Date' } ); - await expect( editButton ).toBeVisible(); - await editButton.click(); - - const expiresInput = page.locator( '.edit-expires-input' ); - await expect( expiresInput ).toBeVisible(); - const expiresDate = new Date(); - expiresDate.setDate( expiresDate.getDate() + 10 ); + expiresDate.setDate( expiresDate.getDate() + 7 ); const expiresString = expiresDate.toISOString().split( 'T' )[ 0 ]; - await expiresInput.fill( expiresString ); - const saveButton = page.getByRole( 'button', { name: 'Save' } ); - await saveButton.click(); + await applicationPasswords.create( `${ TEST_APPLICATION_NAME } Custom`, 'custom', expiresString ); - await expect( page.getByRole( 'alert' ) ).toContainText( 'Application password expiration updated.' ); + const apps = await applicationPasswords.get(); + const app = apps.find( ( item ) => item.name === `${ TEST_APPLICATION_NAME } Custom` ); + expect( app ).toBeDefined(); + expect( app.expires ).not.toBeNull(); + expect( app.expires.startsWith( expiresString ) ).toBe( true ); - const [ updatedApp ] = await applicationPasswords.get(); - expect( updatedApp['expires'] ).not.toBeNull(); - expect( updatedApp['expires'].startsWith( expiresString ) ).toBe( true ); + const successMessage = page.getByRole( 'alert' ); + await expect( successMessage ).toHaveClass( /notice-success/ ); } ); test( 'should correctly revoke a single application password', async ( { @@ -144,17 +128,23 @@ class ApplicationPasswords { this.admin = admin; } - async create(applicationName = TEST_APPLICATION_NAME, expires = null) { + async create(applicationName = TEST_APPLICATION_NAME, preset = null, customDate = null) { await this.admin.visitAdminPage( '/profile.php' ); const newPasswordField = this.page.getByRole( 'textbox', { name: 'New Application Password Name' } ); await expect( newPasswordField ).toBeVisible(); await newPasswordField.fill( applicationName ); - if ( expires ) { - const newPasswordExpiresField = this.page.getByLabel( 'Expires on' ); - await expect( newPasswordExpiresField ).toBeVisible(); - await newPasswordExpiresField.fill( expires ); + if ( preset ) { + const presetSelect = this.page.getByLabel( 'Expiration' ); + await expect( presetSelect ).toBeVisible(); + await presetSelect.selectOption( preset ); + + if ( 'custom' === preset && customDate ) { + const customDateField = this.page.getByLabel( 'Custom expiration date' ); + await expect( customDateField ).toBeVisible(); + await customDateField.fill( customDate ); + } } await this.page.getByRole( 'button', { name: 'Add Application Password' } ).click(); From b2716aaa447d624116dc86859fb87e9425302b78 Mon Sep 17 00:00:00 2001 From: shewa <30765517+shewa12@users.noreply.github.com> Date: Thu, 8 Oct 2026 10:22:15 +0600 Subject: [PATCH 09/10] Tests: Use exact label matching for Expiration in Playwright E2E tests --- tests/e2e/specs/profile/applications-passwords.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/specs/profile/applications-passwords.test.js b/tests/e2e/specs/profile/applications-passwords.test.js index b921303ff92bd..770a376305b0f 100644 --- a/tests/e2e/specs/profile/applications-passwords.test.js +++ b/tests/e2e/specs/profile/applications-passwords.test.js @@ -136,7 +136,7 @@ class ApplicationPasswords { await newPasswordField.fill( applicationName ); if ( preset ) { - const presetSelect = this.page.getByLabel( 'Expiration' ); + const presetSelect = this.page.getByLabel( 'Expiration', { exact: true } ); await expect( presetSelect ).toBeVisible(); await presetSelect.selectOption( preset ); From deaa9a87b163658272ecee4de0f435dbfe01513a Mon Sep 17 00:00:00 2001 From: shewa <30765517+shewa12@users.noreply.github.com> Date: Thu, 8 Oct 2026 10:46:49 +0600 Subject: [PATCH 10/10] Tests: Auto-load .env in Playwright E2E configuration --- tests/e2e/playwright.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e/playwright.config.js b/tests/e2e/playwright.config.js index 0de694e324c24..38a3bfea402b8 100644 --- a/tests/e2e/playwright.config.js +++ b/tests/e2e/playwright.config.js @@ -3,6 +3,7 @@ */ import path from 'node:path'; import { defineConfig } from '@playwright/test'; +import 'dotenv/config'; /** * WordPress dependencies