Skip to content

Alternative to #741 - Autosubmit Tweak#820

Open
georgestephanis wants to merge 7 commits intomasterfrom
alternative/pr-741-autosubmit-tweak
Open

Alternative to #741 - Autosubmit Tweak#820
georgestephanis wants to merge 7 commits intomasterfrom
alternative/pr-741-autosubmit-tweak

Conversation

@georgestephanis
Copy link
Collaborator

Alternative take on #741 // cc: @eric-michel -- this is a first draft, and the code should be reasonably intelligible, but I let Copilot generate the following summary:

This pull request introduces a more flexible and extensible approach to configuring authentication code lengths and auto-submit behavior across all two-factor authentication providers. It centralizes code length logic, adds new filters for customization, and ensures that UI elements reflect these dynamic settings.

Key changes include:

Core logic improvements

  • Added a new static method get_code_length to Two_Factor_Provider, with a corresponding two_factor_code_length filter, allowing the default code length to be set for all providers in a consistent way.
  • Updated the get_code method in Two_Factor_Provider to use the new get_code_length method when no length is specified, making code generation more flexible and consistent.

Provider-specific enhancements

  • Modified backup codes and email providers to use get_code_length for determining code/token length, and updated their filters to use the new centralized logic. [1] [2]
  • Introduced the two_factor_autosubmit_length filter to allow customization of the input length at which authentication forms auto-submit, and applied this filter in backup code, email, and TOTP provider authentication screens. [1] [2] [3]
  • Ensured that the data-digits attribute in TOTP provider input fields accurately reflects the filtered code length, improving UI consistency.

User interface and documentation

  • Improved the JavaScript handling of expected input length by parsing the data-digits attribute as an integer, ensuring correct behavior with dynamic code lengths.
  • Updated readme.txt to document the new two_factor_code_length and two_factor_autosubmit_length filters for developers.

Introduce provider-wide code length controls and an autosubmit length filter.

- Add Two_Factor_Provider::get_code_length() and update get_code() to accept a null length and fall back to the provider-specific length. Providers can now use two_factor_code_length to customize default code lengths per provider.
- Use self::get_code_length() in backup-codes and email providers so their default token/backup lengths are filterable.
- Add two_factor_autosubmit_length filter and apply it in backup-codes, email and TOTP providers to control the data-digits attribute (setting it to 0 disables autosubmit).
- Update readme.txt to document the new two_factor_code_length and two_factor_autosubmit_length filters.
- Minor whitespace cleanups in TOTP packing code.

These changes centralize code-length behavior across providers and make the client-side autosubmit behavior configurable.
Ensure the expectedLength value is treated as a number by parsing inputEl.dataset.digits with parseInt(..., 10). This prevents string-based comparisons or unexpected behavior when dataset values are present (or missing), while preserving the optional chaining and defaulting to 0.
@georgestephanis georgestephanis self-assigned this Mar 4, 2026
@github-actions
Copy link

github-actions bot commented Mar 4, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @Copilot.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: Copilot.

Co-authored-by: georgestephanis <georgestephanis@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@georgestephanis
Copy link
Collaborator Author

To be clear, I may be overcomplicating something that provides a simpler API in the prior PR. This take just makes more sense to my brain, for what little that's worth.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a more flexible and extensible approach to configuring authentication code lengths and auto-submit behavior. It adds a centralized get_code_length() method in Two_Factor_Provider that applies a new two_factor_code_length filter as the base default for all providers, and a new two_factor_autosubmit_length filter that controls the data-digits attribute used by the JavaScript to determine when to auto-submit the authentication form.

Changes:

  • Added get_code_length() static method to Two_Factor_Provider with a two_factor_code_length filter, and updated get_code() to use it when no length is specified
  • Added two_factor_autosubmit_length filter applied in backup codes, email, and TOTP authentication_page methods to independently control the auto-submit threshold; updated data-digits output accordingly
  • Improved JS robustness by parsing data-digits as an integer with parseInt() in class-two-factor-core.php

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
providers/class-two-factor-provider.php Adds get_code_length() method with two_factor_code_length filter; changes get_code() default length from 8 to null (resolves via get_code_length())
providers/class-two-factor-backup-codes.php Uses get_code_length() as default for two_factor_backup_code_length; adds two_factor_autosubmit_length filter in authentication_page
providers/class-two-factor-email.php Uses get_code_length() as default for two_factor_email_token_length; adds two_factor_autosubmit_length filter in authentication_page
providers/class-two-factor-totp.php Adds two_factor_autosubmit_length filter in authentication_page; updates data-digits from hardcoded constant to filtered value; minor whitespace cleanup
class-two-factor-core.php Parses data-digits dataset attribute with parseInt() for more reliable JS numeric comparison
readme.txt Documents new two_factor_code_length and two_factor_autosubmit_length filter hooks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +170 to +180
* @since 0.?.0
*
* @param int $default Default code length if not filtered.
* @param string|null $provider The provider class name. Null uses the called class.
* @return int Number of characters.
*/
public static function get_code_length( $default = 8, $provider = null ) {
/**
* Filter the length of the code for a user.
*
* @since 0.?.0
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @since 0.?.0 version placeholder is present in the get_code_length() method and its filter documentation. This needs to be replaced with the actual version number before the code is released. Based on the codebase, the current version is 0.15.0 (per CHANGELOG.md), so this would be 0.16.0.

Copilot uses AI. Check for mistakes.
*
* To disable autosubmit, set the digits to `0` via the core method `__return_zero`.
*
* @since 0.?.0
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The @since 0.?.0 version placeholder in the two_factor_autosubmit_length filter documentation needs to be replaced with the actual release version before merging.

Suggested change
* @since 0.?.0
* @since 0.15.0

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link

Copilot AI commented Mar 4, 2026

@georgestephanis I've opened a new pull request, #821, to work on those changes. Once the pull request is ready, I'll request review from you.

georgestephanis and others added 4 commits March 4, 2026 10:50
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Initial plan

* Add test coverage for get_code_length() and two_factor_autosubmit_length filter

Co-authored-by: georgestephanis <941023+georgestephanis@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: georgestephanis <941023+georgestephanis@users.noreply.github.com>
@eric-michel
Copy link
Contributor

I definitely support bringing some consistency to how the code lengths are retrieved for different providers. I noticed the same issue when I was working on my PR, but decided not to try and address that issue to keep the scope small (and because I don't have enough experience with this repo to know if there was a reason for the differences). So if there's support for bigger change I'm definitely all for it.

Having said that, I don't see the value in setting the number of digits for auto submission rather than a simple true/false value (and the corresponding hook names). Any value other than the code length or 0 will result in auto submission at the wrong time. Do you see other applications for altering the number other than simply turning auto submission off?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants