hryvinskyi / magento2-webauthn
WebAuthn relying-party ceremonies for Magento 2: the options a browser needs to create or use a passkey, parsing of what the browser returns, and verification of registrations and assertions, with a browser client any widget can import. Stores nothing: the consumer keeps credentials and challenges.
Package info
github.com/hryvinskyi/magento2-webauthn
Type:magento2-module
pkg:composer/hryvinskyi/magento2-webauthn
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- ext-json: *
- ext-openssl: *
- ext-sodium: *
- lbuchs/webauthn: ^2.2
- magento/framework: *
- magento/module-backend: *
- magento/module-config: *
- magento/module-store: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
The server's and the browser's halves of a WebAuthn ceremony, for a module that lets people create and use passkeys: the relying party of a store view, the options a browser needs, the reading of what the browser returns, and the verification of a new passkey and of each use of one. The browser half is a RequireJS module any widget can import. The module stores nothing: the module that uses it keeps the passkeys and the challenges.
What it does for a store
- The relying party of a store view. Its id is the host of the store view's secure base URL. Its origins are the origins of the secure base URL and of the secure link URL, each once, as a browser reports them (lower-case scheme and host, the port only when it is not the scheme's default). Its name is the Relying party name setting, else the store's name, else the host. A store view whose secure base URL names no host is refused with "The secure base URL of store view %1 names no host, so passkeys cannot be used there."
- Challenges. 32 random bytes from a cryptographically secure source. The caller keeps one until the browser answers, and never accepts it a second time.
- The passkey asked for. Creation options ask for a discoverable passkey (one the device can offer without a user name first) that verifies the person (a fingerprint, a face, a PIN), and no attestation. The algorithms offered are ES256, EdDSA and RS256, in that order. Both ceremonies time out after five minutes. Request options without a list of passkeys let the device offer any passkey it holds for the relying party.
- Reading the browser's answer. The parser reads the JSON the browser client sends into raw bytes. It refuses a credential type other than
public-key, anidandrawIdof different bytes, a binary field that is not base64url without padding, and a missing field. - Verification. A new passkey and each use of one are verified with the person present and verified, an origin the relying party lists, and the challenge the caller kept. On a use, a signature counter that does not grow is refused, unless the stored and the new counter are both zero: the passkey may have been copied.
- One message for every failure. A ceremony that fails throws
CeremonyFailedExceptionwith "The passkey could not be verified." The technical reason is its cause (getPrevious()), for a log, never for a person.
Requirements
- Magento Open Source or Adobe Commerce 2.4.x. The module is built and tested on 2.4.9.
- PHP 8.1 to 8.5, with the
json,opensslandsodiumextensions. lbuchs/webauthn^2.2, and the platform's Store, Config and Backend modules.- A store view served over HTTPS: browsers offer passkeys only on a secure page.
Installation
Run from the Magento root.
composer require hryvinskyi/magento2-webauthn bin/magento module:enable Hryvinskyi_WebAuthn bin/magento setup:upgrade bin/magento setup:di:compile # production mode only bin/magento setup:static-content:deploy -f # production mode only bin/magento cache:flush
Verify: bin/magento module:status Hryvinskyi_WebAuthn prints Hryvinskyi_WebAuthn : Module is enabled.
The module creates no table. Nothing changes in the store until another module runs a ceremony.
Configuration
Stores > Settings > Configuration > General > Passkeys (WebAuthn) > Relying Party
| Field | Path | Scope | Default | Effect |
|---|---|---|---|---|
| Relying party name | webauthn/general/rp_name |
default, website, store view | empty | The name a browser or a phone shows when a person creates a passkey. Empty: the store's name, else the store view's host. |
Admin permission
| Resource id | Title | Where |
|---|---|---|
Hryvinskyi_WebAuthn::config |
Passkeys (WebAuthn) | the Passkeys (WebAuthn) configuration section |
Give a role the resource under System > Permissions > User Roles > Role Resources, where it sits under Stores > Settings > Configuration.
Running a ceremony
The server builds the options, the browser client runs the ceremony and sends the result back, and the server verifies it.
Create a passkey
use Hryvinskyi\WebAuthn\Api\CeremonyVerifierInterface; use Hryvinskyi\WebAuthn\Api\ChallengeGeneratorInterface; use Hryvinskyi\WebAuthn\Api\CreationOptionsBuilderInterface; use Hryvinskyi\WebAuthn\Api\RelyingPartyResolverInterface; use Hryvinskyi\WebAuthn\Api\ResponseParserInterface; use Hryvinskyi\WebAuthn\Api\UserEntityFactoryInterface; // 1. The options, for the browser. $rp = $this->relyingParties->forStore($storeId); $challenge = $this->challenges->generate(); // keep it, in the session, until the browser answers $user = $this->users->create($handle, $email, $displayName); $options = $this->creationOptions->build($rp, $user, $challenge, $credentialIdsTheAccountHas); // Send $options to the browser as JSON; the client takes the parsed object. // 2. The browser's answer, as decoded JSON. $credential = $this->verifier->verifyRegistration($rp, $challenge, $this->parser->registration($json)); // Keep $credential->getCredentialId(), getPublicKeyPem() and getSignCount() with the account.
$handleis 16 to 64 random bytes that you keep with the account; the device returns it on each use. A handle of another length throws\InvalidArgumentException.$credentialIdsTheAccountHaslists the raw ids of the account's passkeys, so a device that already holds one is not asked again.RegisteredCredentialInterfacealso gives the device model (getAaguid(), 32 hex characters, all zeros when the device does not say), its transports and whether it verified the person.
Use a passkey
use Hryvinskyi\WebAuthn\Api\RequestOptionsBuilderInterface; use Hryvinskyi\WebAuthn\Api\StoredCredentialFactoryInterface; $challenge = $this->challenges->generate(); $options = $this->requestOptions->build($rp, $challenge, []); // [] lets the device offer any passkey it holds // The browser's answer: find the passkey you kept by $response->getCredentialId(). $response = $this->parser->assertion($json); $stored = $this->storedCredentials->create($credentialId, $publicKeyPem, $signCount); $result = $this->verifier->verifyAssertion($rp, $challenge, $response, $stored); // Save $result->getSignCount() as the passkey's new counter.
Every call that verifies throws CeremonyFailedException; the parser throws it for an answer of another shape. Drop the challenge after the first answer, whatever its outcome.
The browser client
Hryvinskyi_WebAuthn/js/webauthn-client is a RequireJS module, served in both the storefront and the admin. It holds no wording, touches no page and reads no global: the caller hands it a port, an object with the browser's credentials container and its PublicKeyCredential.
define(['Hryvinskyi_WebAuthn/js/webauthn-client'], function (client) { 'use strict'; return async function createPasskey(options) { // options: the server's creation options, already parsed (for example from `await response.json()`) const port = { credentials: window.navigator.credentials, PublicKeyCredential: window.PublicKeyCredential }; const result = await client.create(port, options); if (result.ok) { return result.value; // POST this JSON to the server } // result.kind: cancelled, already_registered, insecure, aborted, unsupported or failed return null; }; });
| Member | Does |
|---|---|
create(port, options, signal?) |
Creates a passkey from the server's creation options, parsed from JSON. |
get(port, options, signal, mediation) |
Uses a passkey, from the server's parsed request options. mediation is optional, or conditional for the autofill of a user name field. |
isSupported(port) |
Whether the browser offers passkeys to the page. |
isConditionalMediationAvailable(port) |
Whether the browser lists passkeys in a field's autofill. Never rejects. |
toCreationOptions, toRequestOptions, serializeRegistration, serializeAssertion, toBase64Url, fromBase64Url |
The conversions create and get use. |
ContractError |
Thrown by the conversions for options of the wrong shape; its path names the member. |
create and get never reject. They resolve with {ok: true, value}, the credential in the JSON the parser reads, or with {ok: false, kind}. The kind comes from the browser's error: NotAllowedError is cancelled, InvalidStateError already_registered, SecurityError insecure, AbortError aborted. A ceremony whose signal was aborted is aborted, a browser without passkeys unsupported, anything else failed. The caller chooses the words for each kind.
Extension points
Every interface under Api/, its data contracts and CeremonyFailedException are @api. Each is bound by one preference in etc/di.xml, which your module can replace; CeremonyVerifierInterface is the only one that names lbuchs/webauthn (Model\LbuchsCeremonyVerifier).
For tests, Hryvinskyi\WebAuthn\Test\Fixture\SoftwareAuthenticator is an ES256 passkey device in PHP. It answers creation and request options with the JSON the browser client sends; its origin, counter, user-verified flag, user handle and relying party can be set.
Stored data
None. The caller keeps each passkey's id, public key (PEM) and signature counter, the account's handle, and each challenge until the browser answers.
Limits
- Passkeys belong to one host. A passkey is bound to the relying party id, the host of the secure base URL. A store view on another host, a subdomain included, needs its own passkeys, and changing a store view's host leaves its passkeys unusable.
- No attestation. The device's maker is not checked against a list of trusted devices.
- Three algorithms. A device that offers none of ES256, EdDSA and RS256 cannot create a passkey.
- A copied passkey is caught only by its counter. Many devices keep the counter at zero, and then a copy cannot be told apart.
- HTTPS only. A browser refuses a ceremony on an insecure page; the client reports
insecure.
Registrations
| Where | What |
|---|---|
| Preferences | Every interface under Api/ and Api/Data/ to its implementation under Model/. |
| Configuration | system.xml section webauthn; default in config.xml. |
| ACL | Hryvinskyi_WebAuthn::config. |
| RequireJS | view/base/web/js/webauthn-client.js. |
The module registers no route, table, cron job, command, event or plugin.
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/WebAuthn/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/WebAuthn/Test/Integration. - Browser client: Node 22.15 or later, in
app/code/Hryvinskyi/WebAuthn/Test/Js:npm ci, thennpm run check(type check) andnpm test. - Static analysis: PHPStan 2.x at level 10 with the package's
phpstan.neon.dist(phpstan analyse -c app/code/Hryvinskyi/WebAuthn/phpstan.neon.dist --level=10, afterbin/magento setup:di:compile), andvendor/bin/phpcs --standard=app/code/Hryvinskyi/WebAuthn/phpcs.xml.dist app/code/Hryvinskyi/WebAuthn.
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.