Search by

hryvinskyi / magento2-customer-sign-in-api

hryvinskyi

Contracts of the customer sign-in suite: sign-in methods and their roles, a customer's enrolled factors and recovery codes, the pending sign-in between a first and a second step, the verification gate that finishes it, and the outcomes and refusals a sign-in can end in.

Package info

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

Type:magento2-module

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

Statistics

Installs: 1

Dependents: 5

Suggesters: 0

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:17:22 UTC


README

The contracts of the customer sign-in suite: sign-in methods and the roles they play, the factors a customer enrolled and their recovery codes, the pending sign-in that waits between a first and a second step, the verification gate that finishes it, the store's requirement of a second step from chosen customer groups, and the closed sets of outcomes and refusals a sign-in step ends in. It holds interfaces, enums and exceptions only. The engine, hryvinskyi/magento2-customer-sign-in, implements them; a module that adds a sign-in method, a storefront page or an admin screen depends on this package.

The model

  • A sign-in method is one way for a customer to prove who they are: a passkey, a code sent by e-mail, an authenticator app, a code sent by text message. Each module that adds one contributes it to the method pool under its code.

  • A role is what a method can do in a sign-in. The set is closed, a method states at least one, and every sign-in is decided from them:

    Role Value The method…
    SignInRole::SignIn sign_in proves who the customer is on its own, as the first step.
    SignInRole::Verification verification answers a second step that is due. Having such a factor does not by itself make the second step due.
    SignInRole::Enforcement enforcement makes the second step due for every later sign-in of the customer who has it.
    SignInRole::Recovery recovery is a backup answer to a second step that is due, and never makes it due.
  • A factor is one method enrolled by one customer, with a name. A customer whose factors include one with the Enforcement role needs a second step after the first one, whether that method is switched on or not.

  • Recovery codes are one factor of the method recovery. The customer's first factor with the Enforcement role creates them, and removing the last one removes them.

  • A pending sign-in is a first step that passed while a second step is due. No session is open while it waits. It lives for a limited time and allows a limited number of entries, after which the sign-in starts over.

  • Switched off for a website. A switched-off method 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.

  • The second-step requirement. A website can require a second step from the customers of chosen groups. It covers a customer when it is switched on for the website, the customer's group is one of the groups chosen there, and at least one method with the Enforcement role is switched on for that website, so the customer can set one up. A covered customer who has no Enforcement factor, of a method switched on or not, owes a second step. The caller names the website: the website of the request for a storefront page, the website the account belongs to for a customer access token.

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.
  • magento/framework.

Installation

Run from the Magento root. A module that uses the contracts requires this package; hryvinskyi/magento2-customer-sign-in-pack installs it with the rest of the suite.

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

Verify: bin/magento module:status Hryvinskyi_CustomerSignInApi prints Hryvinskyi_CustomerSignInApi : Module is enabled.

Nothing implements the contracts until hryvinskyi/magento2-customer-sign-in is installed.

Configuration

None.

Contracts

Every interface, enum and exception here is @api, in the namespace Hryvinskyi\CustomerSignInApi\Api.

Methods

Contract Does
SignInMethodInterface getCode(): lower-case letters and underscores, 2 to 32 characters, stored with every factor, so it never changes once customers enrolled; password names the platform's own password check and recovery the recovery codes, and no contributed method uses either. getLabel(): the translated name the customer reads. getRoles(): a list of SignInRole, at least one, each once. isEnabled(int $websiteId).
SignInMethodPoolInterface get($code) (throws NoSuchEntityException for a code no installed module contributes) and all(), keyed by code, switched on or not. The platform's password check is not a member.

Factors and recovery codes

Contract Does
CustomerFactorsInterface listFor($customerId): every factor, recovery codes included, in the order they were enrolled. get($customerId, $factorId): one factor, scoped to the customer; another customer's factor is answered with NoSuchEntityException. requiresVerification($customerId): whether the customer's sign-ins need a second step.
FactorEnrolmentInterface enrol($customerId, $methodCode, $label, $writer): one transaction holding the factor, the method's own data and, for the first Enforcement factor, the recovery codes. remove($customerId, $factorId, $actor), removeAll($customerId, $actor), markUsed($factorId). While the engine's Email Customers About Changes setting is on, the customer is e-mailed about every factor added and removed (the recovery codes excepted), except a removal by RemovalActor::Erasure.
FactorDataWriterInterface write(FactorInterface $factor): the method's own data for a factor being enrolled. It runs inside the enrolment's transaction, after the factor got its id; anything it throws rolls the whole enrolment back.
RecoveryCodesInterface regenerate($customerId): a new set, returned once in clear; every earlier code stops working. remaining($customerId). consume($customerId, $code): compared without regard to letter case, separators and spaces; while the engine's Email Customers About Changes setting is on, the customer is e-mailed when a code is used.

enrol() throws SignInRefusedException when the method is unknown, can neither sign in nor verify, or is switched off for the current website, and when the label is empty or too long. remove() throws it for the recovery codes, which go only with the last Enforcement factor, and when the actor is RemovalActor::Customer, the factor is the customer's last Enforcement factor, and the second-step requirement of the current website covers the customer. A store administrator (RemovalActor::Admin) and a data-erasure run (RemovalActor::Erasure) may remove it; removeAll() is never refused. regenerate() throws it when the customer has no Enforcement factor.

The sign-in

Contract Does
SignInFlowInterface completeFirstStep($customerId, $methodCode): after a first step that proved the customer's identity, opens the session when no second step is due or when the method can also answer one; otherwise parks a pending sign-in in the session, replacing an older one. pending() and cancelPending().
VerificationGateInterface verify($factorId, \Closure $check): the only way to finish a second step. It runs $check only when the pending sign-in, the factor and the customer's budget of failed entries allow it, and only after reserving one entry, 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; one that fails or throws is counted. A check that throws is logged and never rethrown.
ParkedSignInInterface isParkedInThisRequest(): whether a password sign-in in this request was parked for its second step. A storefront page that signs customers in by password reads it to send the customer to the second step instead of treating the sign-in as failed.
RecentSignInInterface isRecent(): whether the current session was opened recently, by a password sign-in or a sign-in of the suite, for the customer signed in now. A session opened without such a proof (an administrator signed in as the customer, a switch of store view) is never recent.

The second-step requirement

Contract Does
SecondStepRequirementInterface covers(int $customerId, int $websiteId): whether the website's requirement covers the customer (see The model). With no group chosen, or with no Enforcement method switched on for the website, it covers nobody. isOwedBy(int $customerId, int $websiteId): whether the requirement covers the customer and they have no factor whose method has the Enforcement role, switched on or not. For a customer id that no customer has, both answer false and neither throws.

A module that gives a customer access by a password alone, without a session of the store's website, refuses it with SecondStepSetUpRequiredException while isOwedBy() is true for the website the account belongs to.

Data

Contract Holds
Data\FactorInterface Factor id, customer id, method code, label, when it was enrolled and last finished a sign-in (UTC). A read model: an enrolment creates it, a use or a removal changes it.
Data\EnrolmentResultInterface The factor, and the recovery codes created with it (null when it created none). The codes exist in clear only here.
Data\PendingSignInInterface Customer id, website id, the method of the first step (password for the platform's password check), when it started (UTC), entries left, how many entries did not work, and the factors that can answer the second step, switched-off methods included.
Data\SignInOutcomeInterface getKind(), getRefusalReason() and getAttemptsLeft().

Outcomes and refusals

SignInOutcomeKind: signed_in (the session is open), verification_required (a pending sign-in waits; no session was opened) and refused.

RefusalReason, the values stable codes a page words:

Case Value Means
NoPending no_pending The session holds no pending sign-in.
Expired expired The pending sign-in outlived its lifetime; the sign-in starts over.
NotYours not_yours The factor belongs to another customer.
MethodNotAllowed method_not_allowed A first step by a method that is unknown, switched off or cannot sign in alone, or a second step by a factor whose method neither verifies nor recovers.
WrongCode wrong_code The check did not pass; another entry is allowed.
AttemptsExhausted attempts_exhausted No entry left; the pending sign-in is discarded and the sign-in starts over.
TooManyFailures too_many_failures The customer's budget of failed second-step entries is spent for now; the check did not run.
WrongWebsite wrong_website The pending sign-in was started on another website, or, while accounts are kept per website, the account belongs to another one.

RemovalActor: customer, admin and erasure. The customer is told about a removal they or a store administrator made, and the notice says which; a removal by a data-erasure or anonymisation run is not told, because that run answers to the person for it.

Exceptions

  • Exception\SignInRefusedException (a LocalizedException): a refusal the customer can act on. Its message is written for the customer. Any other exception carries a message for a developer or an administrator and is not shown to a customer.
  • Exception\VerificationRequiredException (an AuthenticationException): a password was right, but the account needs a second step that only the store's website offers, so a password-only access such as a web API token is refused. The web APIs answer it as an authentication failure. Its message is fixed and says nothing about the account's methods: "This account needs a second sign-in step. Sign in on the store's website."
  • Exception\SecondStepSetUpRequiredException (an AuthenticationException): a password was right, but the store requires a second step from the customer and they have not set one up. A second step is set up only on the store's website, so a password-only access such as a web API token is refused. The web APIs answer it as an authentication failure. Its message is fixed, names no kind of method, and may be shown to the customer: "Your store requires a second sign-in step. Sign in on the store's website and add one."

Writing a sign-in method

  1. Implement SignInMethodInterface with your code, label, roles and per-website switch, and contribute it to the engine's method pool. The engine's README shows the di.xml entry and the rules of the pool.

  2. Enrol. After your own ceremony succeeds, call FactorEnrolmentInterface::enrol() with a FactorDataWriterInterface that stores your method's data (a public key, a secret) keyed by the new factor's id. Show the customer the recovery codes of EnrolmentResultInterface::getRecoveryCodes() once when it holds any.

  3. First step (a method with the SignIn role). When your check proves who the customer is, call SignInFlowInterface::completeFirstStep() and act on the outcome's kind: signed_in, verification_required (send the customer to the second step) or refused.

  4. Second step (a method with the Verification role). Read the factors of SignInFlowInterface::pending(), let the customer pick one, and run your check through the gate:

    $outcome = $this->gate->verify($factorId, function () use ($factorId, $typedCode): bool {
        // Catch your own expected refusals and answer false; anything thrown counts as a wrong entry.
        return $this->checker->matches($factorId, $typedCode);
    });
  5. Gate any change of a customer's methods on RecentSignInInterface::isRecent().

Limits

  • The package holds no implementation. A module that injects these contracts needs hryvinskyi/magento2-customer-sign-in installed, or the object manager cannot build it.
  • No contract completes a second step outside the storefront session. A customer who needs one gets no web API token by password (VerificationRequiredException), and neither does a customer who owes one the store requires (SecondStepSetUpRequiredException).

Versioning

The package follows Semantic Versioning. The values of the enums are stable codes and never change. What changed in each version is in CHANGELOG.md.

Tests

The package has no tests of its own; its contracts are proven through the engine's. Static analysis, from the Magento root: PHPStan 2.x at level 10 with the package's phpstan.neon.dist (phpstan analyse -c app/code/Hryvinskyi/CustomerSignInApi/phpstan.neon.dist --level=10, after bin/magento setup:di:compile), and vendor/bin/phpcs --standard=app/code/Hryvinskyi/CustomerSignInApi/phpcs.xml.dist app/code/Hryvinskyi/CustomerSignInApi.

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.