Search by

hryvinskyi / magento2-customer-sign-in

hryvinskyi

The customer sign-in engine for Magento 2: the pool of sign-in methods, a customer's factors and recovery codes, the pending sign-in between a first and a second step, the verification gate that finishes it, and the e-mails that tell a customer their sign-in methods changed.

Package info

github.com/hryvinskyi/magento2-customer-sign-in

Type:magento2-module

pkg:composer/hryvinskyi/magento2-customer-sign-in

Statistics

Installs: 1

Dependents: 6

Suggesters: 1

Stars: 0

Open Issues: 0

1.0.0 2026-12-03 08:33 UTC

This package is auto-updated.

Last update: 2026-12-03 14:18:20 UTC


README

The engine of the customer sign-in suite. It implements the contracts of hryvinskyi/magento2-customer-sign-in-api: the pool of sign-in methods, each customer's factors and recovery codes, the pending sign-in that waits between a first and a second step, and the verification gate that finishes it. It holds the store's password sign-in back for its second step, lets a website require a second step from the customers of chosen groups, refuses web API tokens by password to customers who need a second step or owe the one the store requires, and e-mails customers when their sign-in methods change. It has no storefront page and no admin screen of its own: the pages are in hryvinskyi/magento2-customer-sign-in-frontend, the customer tab in hryvinskyi/magento2-customer-sign-in-admin-ui, and each method in its own package (-email, -passkey, -totp, -sms).

What it does for a store

  • Methods by role. Each installed method states its roles (signing in alone, answering a second step, making a second step due, recovering one); the API package's README describes them. The engine contributes the recovery codes itself, as the method recovery.
  • A second step after the password. A customer who has a factor whose method makes a second step due (an authenticator app, a phone) is asked for it after every first step, the password included. A first step by a method that can also answer a second step (a passkey) opens the session at once.
  • Enrolment in one transaction. A new factor, the method's own data and, for the customer's first factor that makes a second step due, ten recovery codes are written together or not at all. A factor's name is 1 to 128 characters; an empty or longer one is refused with a message the customer can act on.
  • Recovery codes. Ten codes of ten characters, drawn from an alphabet without the characters people confuse (0 and O, 1, I and L), shown once as XXXXX-XXXXX. A code is read without regard to letter case, spaces and hyphens, and is accepted once. The codes go when the customer's last factor that makes a second step due is removed, and cannot be removed on their own.
  • The pending sign-in. The session holds only a random token; the pending sign-in itself is a row of the database. It belongs to the website it was started on, lives Second Step Time Limit (minutes), and allows Second Step Attempts entries. A newer first step replaces it.
  • The verification gate. A second-step entry is refused, before the method's check runs, when there is no pending sign-in, when it was started on another website, when the factor belongs to another customer, when the factor's method can neither verify nor recover, and when the customer's hourly budget of wrong entries is spent. Then one entry is reserved by one conditional update of the database, so parallel requests use no more entries than the pending sign-in allows. A check that passes opens the session and marks the factor used. A check that fails, or throws, is counted; the entry that uses the last one discards the pending sign-in.
  • An hourly budget of wrong entries per customer. Wrong Second Step Entries per Hour counts across all of a customer's pending sign-ins, over a sliding hour. Only wrong entries count. When the counter cannot be read, the check goes on, still bounded by the pending sign-in's own entries.
  • A switched-off method. A method switched off for a website is not offered there and refuses new sign-ins and enrolments. A customer who already has a factor of it that answers a second step is still asked for it, and it still works, so switching a method off locks nobody out.
  • The password sign-in waits. Right after the platform verified a customer's password, signing that customer into the storefront session parks the sign-in as a pending sign-in instead, when the customer needs a second step. Nobody is signed in, the customer login event is not dispatched, and what the password check already wrote into the session (the customer's tax class and default tax addresses, the visitor bound to the customer) is taken out, so the guest session holds none of the customer's data. A session opened without a password check in the same request (a switch of store view, an administrator signed in as the customer, a registration) is opened unchanged.
  • A second step the store requires. With Require a Second Step on for a website, the customers of the groups chosen in Customer Groups That Need a Second Step must have one. The requirement covers a customer only while at least one method that makes a second step due (a passkey, an authenticator app, a phone) is switched on for the website, since without one nobody could set it up; with no group chosen it covers nobody. A covered customer who has no factor of such a method, switched on or not, owes a second step. The customer's group is read from their account on every check, so a group an administrator changes counts from the next request. The engine decides who owes a second step; the storefront pages that send such a customer to set one up are in hryvinskyi/magento2-customer-sign-in-frontend and its checkout bridge.
  • The last second step stays while the requirement covers the customer. A customer whom the requirement of the request's website covers cannot remove their last factor that makes a second step due: the removal is refused with "Your store requires a second sign-in step. Add another method before you remove this one." A store administrator and a data-erasure run can remove it.
  • No web API token by password alone. A customer access token for a customer who needs a second step is refused with "This account needs a second sign-in step. Sign in on the store's website." A customer access token for a customer who owes the second step the store requires is refused with "Your store requires a second sign-in step. Sign in on the store's website and add one." For that check the requirement is read for the website the customer's account belongs to, not the website of the request, so a website whose requirement is off issues no token to them either. REST answers 401, the GraphQL generateCustomerToken mutation an authentication error, and the SOAP token service refuses it too. A token issued without a password check in the request (one a store administrator issues for the customer), and a token for any other kind of user, are left alone.
  • A recent sign-in. A password sign-in, or a sign-in of the suite, stamps the session with the customer and the time. The stamp counts as recent for Recent Sign-in Window (minutes), for that customer only, and is cleared at sign-out. A customer must have signed in that recently to add or remove a sign-in method.
  • Accounts per website. While customer accounts are shared per website, a first step for an account of another website is refused and opens nothing.
  • E-mails to the customer. When a sign-in method is added or removed, and when a recovery code is used, the customer gets an e-mail in the store view their account belongs to. It names the method and the factor and says when the change happened; it never holds a code or a secret. The removal e-mail says so when a store administrator removed the method. A removal by a data-erasure run sends nothing. A message that cannot be sent is logged and dropped; the change stands.

Requirements

  • Magento Open Source or Adobe Commerce 2.4.x. The suite is built and tested on 2.4.9.
  • PHP 8.1 to 8.5.
  • hryvinskyi/magento2-customer-sign-in-api ^1.0, hryvinskyi/magento2-verification-code ^1.0, hryvinskyi/magento2-rate-limit ^1.1, hryvinskyi/magento2-clock ^1.0 and psr/log.
  • The platform's Customer, Integration, Email, Store, Config, Backend, Authorization and Cron modules.

Installation

Run from the Magento root. hryvinskyi/magento2-customer-sign-in-pack installs this package with the rest of the suite.

composer require hryvinskyi/magento2-customer-sign-in
bin/magento module:enable Hryvinskyi_Clock Hryvinskyi_RateLimit Hryvinskyi_VerificationCode Hryvinskyi_CustomerSignInApi Hryvinskyi_CustomerSignIn
bin/magento setup:upgrade
bin/magento setup:di:compile  # production mode only
bin/magento cache:flush

setup:upgrade creates the three tables listed under Stored data. Verify: bin/magento module:status Hryvinskyi_CustomerSignIn prints Hryvinskyi_CustomerSignIn : Module is enabled.

The platform's cron must run for the daily clean-up (see Stored data). With the engine alone, the only method is the recovery codes, which no customer can get without another method, so nothing changes for customers until a method package is installed. A store that uses Magento's "Remember Me" also installs hryvinskyi/magento2-customer-sign-in-persistent (see Limits).

Configuration

Stores > Settings > Configuration > Customers > Sign-in Methods > General

Field Path Scope Default Effect
Second Step Time Limit (minutes) customer_sign_in/general/pending_lifetime default, website 10 How long a customer has for the second step after the first one passed.
Second Step Attempts customer_sign_in/general/pending_attempts default, website 5 Wrong entries after which the customer must start signing in again.
Wrong Second Step Entries per Hour customer_sign_in/general/verify_failures_per_hour default, website 10 Across all sign-ins of one customer. When reached, the second step is refused until the hour has passed.
Recent Sign-in Window (minutes) customer_sign_in/general/recent_sign_in_window default, website 15 A customer must have signed in this recently to add or remove a sign-in method.
Require a Second Step customer_sign_in/general/require_second_step default, website 0 (No) Yes: the customers of the chosen groups must set up a second step (see What it does for a store).
Customer Groups That Need a Second Step customer_sign_in/general/require_second_step_groups default, website none The groups the requirement covers. Shown while Require a Second Step is Yes, and required then. Stored as group ids separated by commas.
Email Customers About Changes customer_sign_in/general/notify default, website, store view 1 (Yes) When a sign-in method is added or removed, and when a recovery code is used.
Email Sender customer_sign_in/general/sender_identity default, website, store view general (General Contact) Shown while Email Customers About Changes is Yes.
Sign-in Method Added Email Template customer_sign_in/general/template_factor_added default, website, store view customer_sign_in_general_template_factor_added The shipped template Sign-in Method Added.
Sign-in Method Removed Email Template customer_sign_in/general/template_factor_removed default, website, store view customer_sign_in_general_template_factor_removed The shipped template Sign-in Method Removed.
Recovery Code Used Email Template customer_sign_in/general/template_recovery_code_used default, website, store view customer_sign_in_general_template_recovery_code_used The shipped template Recovery Code Used.

The four numbers are read for the website. A value that is missing, not a whole number or below 1 is read as its default, so a mistyped value never turns a limit off. The requirement and its groups are read for the website the check names: the website of the request on the storefront, the website of the customer's account for a web API token. The e-mail settings are read for the store view of the customer's account. Whether each method is switched on is a setting of that method's package.

Admin permissions

The package declares the resources and publishes their ids as the constants of Hryvinskyi\CustomerSignIn\Api\AclResourcesInterface. Give a role the ones it needs under System > Permissions > User Roles > Role Resources.

Resource id Title Where in the tree Opens
Hryvinskyi_CustomerSignIn::manage_factors Manage customer sign-in methods Customers > All Customers Removing a customer's sign-in methods on the customer page, in hryvinskyi/magento2-customer-sign-in-admin-ui.
Hryvinskyi_CustomerSignIn::config Customer Sign-in Methods Stores > Settings > Configuration The Sign-in Methods configuration section.

Stored data

Table One row per Holds
hryvinskyi_customer_sign_in_factor factor Customer id, method code, the factor's name, when it was enrolled and last finished a sign-in (UTC). No secret: each method keeps its own data in its own package.
hryvinskyi_customer_sign_in_recovery_code recovery code The factor it belongs to, a keyed hash (HMAC-SHA256 under the deployment key) of the customer id and the code, and when it was used. No readable copy of a code.
hryvinskyi_customer_sign_in_pending pending sign-in The SHA-256 of the random token the session holds (32 random bytes), customer id, website id, the first step's method, when it started, the entries used and allowed, and when it was finished, replaced or discarded. The table alone finishes no sign-in.

All three are deleted with the customer account (foreign keys to customer_entity). The cron job hryvinskyi_customer_sign_in_pending_purge (group default, daily at 03:23) deletes the pending sign-ins started more than 24 hours earlier.

The hourly budget of wrong entries is kept by hryvinskyi/magento2-rate-limit in the platform's cache, in the bucket customer_sign_in_verify, keyed by a hash of the customer id.

E-mail templates

Template id Label Variables
customer_sign_in_general_template_factor_added Sign-in Method Added customer_name, changed_at, method_label, factor_label
customer_sign_in_general_template_factor_removed Sign-in Method Removed customer_name, changed_at, method_label, factor_label, removed_by_admin (1 or empty)
customer_sign_in_general_template_recovery_code_used Recovery Code Used customer_name, changed_at

changed_at is the store view's local time, Y-m-d H:i. A store edits a copy under Marketing > Communications > Email Templates (Load Default Template) and chooses it in the matching setting.

Limits

  • A sign-in without the platform's password check is not held back. A module that signs a customer in some other way (a social login) opens a session without the second step. Only a password checked by AccountManagementInterface::authenticate() in the same request is held back.
  • No second step outside the storefront. A customer who needs one gets no customer access token by password from REST, SOAP or GraphQL, and a private RSS feed, which signs in by HTTP authentication, answers 401 to them. The suite has no web API that completes a second step.
  • "Remember Me" needs the persistent bridge. Magento's persistent shopping cart saves a remembered session when the password is checked, before the engine parks the sign-in. Without hryvinskyi/magento2-customer-sign-in-persistent, a customer who ticked Remember Me is remembered while their second step is still due.
  • The budget can be spent by someone else. Anyone who passes a customer's first step (who knows their password) can spend that customer's budget of wrong entries and block their second step for up to an hour.
  • A full cache flush resets the budget. bin/magento cache:flush removes the counters with everything else.
  • Long-running application servers. The record of which password was checked lives in memory for one request. PHP-FPM builds it new for each request; an application server that keeps objects between requests relies on the platform's object resetter (Magento\Framework\ObjectManager\Resetter) to clear it, which Magento 2.4.6 does not have.
  • E-mail is sent in the request. A message that cannot be sent is logged and not retried.
  • Tokens issued before the requirement. The requirement refuses new customer access tokens only. A token issued before Require a Second Step was switched on, or before the customer's group was chosen, works until it expires.

Registrations

Where What
Preferences Every contract of the API package, and the engine's own stores and services.
Pool Hryvinskyi\CustomerSignIn\Model\MethodPool Argument methods: recovery → Model\Method\RecoveryMethod.
Plugin after on Magento\Customer\Api\AccountManagementInterface::authenticate Plugin\Customer\RecordPasswordCheck notes whose password was verified in the request.
Plugin around on Magento\Customer\Model\Session::setCustomerDataAsLoggedIn and ::setCustomerAsLoggedIn Plugin\Customer\ParkPasswordSignIn parks a password sign-in that needs a second step.
Plugin before on Magento\Integration\Api\UserTokenIssuerInterface::create Plugin\Integration\RefuseTokenWithoutSecondStep refuses a customer token issued after a password check in the request: with VerificationRequiredException when the customer needs a second step, with SecondStepSetUpRequiredException when they owe the one the requirement of their account's website asks for.
Event customer_logout (frontend) Observer\ForgetSignInStamp clears the recent sign-in stamp.
Tables hryvinskyi_customer_sign_in_factor, hryvinskyi_customer_sign_in_recovery_code, hryvinskyi_customer_sign_in_pending.
Cron hryvinskyi_customer_sign_in_pending_purge, 23 3 * * *, group default.
E-mail templates The three above.
Configuration system.xml section customer_sign_in; defaults in config.xml.
ACL The two resources above.

The plugins apply in every area, since a password is checked by the storefront and the web APIs alike. The package registers no route, layout, template or command.

Extension points

The contracts are in the API package; its README describes them and how a method uses them.

Add a sign-in method

Implement Hryvinskyi\CustomerSignInApi\Api\SignInMethodInterface, then add it to the pool under its code, in your module's global etc/di.xml:

<type name="Hryvinskyi\CustomerSignIn\Model\MethodPool">
    <arguments>
        <argument name="methods" xsi:type="array">
            <item name="hardware_key" xsi:type="object">Vendor\Module\Model\Method\HardwareKeyMethod</item>
        </argument>
    </arguments>
</type>

The item's name must be the method's code. An item that is not a sign-in method, or whose name differs from its code, makes the pool refuse to be built, naming the item. Do not use the codes password and recovery. The code is stored with every factor, so it never changes once customers enrolled.

Add to the pool only in a global etc/di.xml. An area di.xml (etc/frontend/di.xml, etc/webapi_rest/di.xml, …) that names methods replaces the whole list in that area instead of adding to it: the recovery codes and the other packages' methods would be gone there.

A factor whose method is not installed stays in the table; it answers no second step and does not count as a method that makes one due.

Versioning

The package follows Semantic Versioning. What changed in each version is in CHANGELOG.md.

Tests

Run from the Magento root.

  • Unit: vendor/bin/phpunit -c app/code/Hryvinskyi/CustomerSignIn/phpunit.xml.dist.
  • Integration: the platform's harness in dev/tests/integration, one run at a time against its own database. cd dev/tests/integration && php -d memory_limit=4G ../../../vendor/bin/phpunit -c phpunit.xml ../../../app/code/Hryvinskyi/CustomerSignIn/Test/Integration. A change to db_schema.xml needs the harness's database rebuilt.
  • Static analysis: PHPStan 2.x at level 10 with the package's phpstan.neon.dist (phpstan analyse -c app/code/Hryvinskyi/CustomerSignIn/phpstan.neon.dist --level=10), and vendor/bin/phpcs --standard=app/code/Hryvinskyi/CustomerSignIn/phpcs.xml.dist app/code/Hryvinskyi/CustomerSignIn.

Translations

The wording ships in en_US and 25 more locales: the official languages of the European Union, Norwegian Bokmål and Ukrainian. The translations are machine-made; have them read by a native speaker before a store relies on them, and override a phrase in a theme or a language pack.