REST API
Nahnu Login Shield adds REST routes under /wp-json/nahnls/v1/. They let a headless front end, a mobile app or your own JavaScript sign users in with passkeys, magic links and two-factor codes, and let signed-in users manage their passkeys. If you build a normal WordPress site, you don’t need these. The built-in login screens and the PHP and JavaScript helpers already use them.
For the PHP side see Passwordless API. For a browser wrapper around these routes see JavaScript API (NAHNLSAuth).
Base address and requirements#
- Base:
https://your-site.example/wp-json/nahnls/v1/ - Passkeys need HTTPS (or
localhost). - A route works only when its feature is switched on (for example the Passwordless tab for passkeys and magic links, and the 2FA tab for two-factor). Otherwise it returns an error.
- Errors use WordPress’s standard shape:
{ "code": "...", "message": "...", "data": { "status": 400 } }.
Two kinds of routes#
Public routes are for visitors who aren’t signed in yet. They carry the same hard stops as the normal login (IP blocklist, lockouts, geo-blocking), have their own per-IP rate limits, and never reveal whether an account exists. They need no login cookie or nonce.
Authenticated routes are for signed-in users. They use WordPress’s standard REST authentication: the login cookie plus an X-WP-Nonce header (wp_create_nonce( 'wp_rest' )), or an application password.
All routes#
| Method and route | Auth | Body or query | Response |
|---|---|---|---|
POST passkeys/login/options | public | {} | { publicKey: PublicKeyCredentialRequestOptions } (base64url fields). 30 per minute per IP |
POST passkeys/login/verify | public | { credential, redirect?, remember? } | { user_id, redirect } and sets the login cookie. When 2FA is required the cookie is not kept, and the response is { user_id, redirect, pending_2fa: true }. Send the browser to redirect (the 2FA challenge page). 10 per minute per IP |
POST magic-link/request | public | { identifier, redirect?, origin? } | Always { sent: true }. HTTP 429 with the same body for account-agnostic limits only |
POST 2fa/verify (API 2) | public | { nonce_key, code?, backup_code? } | { redirect } and sets the login cookie. 10 per minute per IP |
POST 2fa/email (API 2) | public | { nonce_key } | { sent: true, email_masked } |
POST 2fa/passkey/options (API 2) | public | { nonce_key } | { publicKey } for the pending user’s own passkeys. 30 per minute per IP |
POST 2fa/passkey/verify (API 2) | public | { nonce_key, credential } | { redirect } and sets the login cookie. 10 per minute per IP |
GET passkeys | cookie and nonce | ?user_id= (needs edit_users) | { passkeys: [ { id, name, aaguid, transports, created, last_used, created_f, last_used_f } ] } |
DELETE passkeys/{id} | cookie and nonce | ?user_id= (needs edit_users) | { removed, passkeys } |
POST passkeys/register/options | cookie and nonce | {} | { publicKey: PublicKeyCredentialCreationOptions } |
POST passkeys/register/verify | cookie and nonce | { name, credential } | 201 { credential, passkeys } |
GET me/security | cookie and nonce | none | { user_id, two_factor, can_use_passkeys, can_use_magic_link, passkeys } |
(API 2) means the route needs NAHNLS_API_VERSION 2 or later.
The credential field is the JSON form of a browser PublicKeyCredential: { id, rawId, type, response: { … } }, with every ArrayBuffer encoded as base64url. The helpers NAHNLSAuth.util.encodeAssertion() and encodeAttestation() do this for you. The REST layer never returns a stored public key.
Sign in with a passkey#
POST passkeys/login/optionswith an empty body. You getpublicKeyoptions.- Pass the options to the browser:
navigator.credentials.get({ publicKey })(decode the base64url fields first). POST passkeys/login/verifywith the encoded credential.- If the response has
pending_2fa: true, no session exists yet. Send the browser toredirect, the 2FA challenge page, or finish the challenge with the2fa/*routes if you started it yourself.
const { publicKey } = await fetch('/wp-json/nahnls/v1/passkeys/login/options', { method: 'POST' })
.then(r => r.json());
const cred = await navigator.credentials.get({
publicKey: NAHNLSAuth.util.decodeRequestOptions(publicKey)
});
await fetch('/wp-json/nahnls/v1/passkeys/login/verify', {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ credential: NAHNLSAuth.util.encodeAssertion(cred), redirect: '/app' })
}); The challenge from the options call is tied to the IP that asked for it. If the verify call comes from a different IP, you get a generic 401. Fetch both from the same client IP.
Request a magic link#
POST /wp-json/nahnls/v1/magic-link/request
{ "identifier": "user@example.com", "redirect": "/members/" } The answer is always { "sent": true }, whether or not the account exists. Only account-agnostic limits return HTTP 429 (with the same body): the per-IP rate limit, a hard stop (blocked IP, lockout, geo-block), or a honeypot username.
origin (API 2) is 'wc' or 'wp'. Anything else returns 400 rest_invalid_param. 'wc' adds nahnls_origin=wc to the emailed link, so a 2FA step after sign-in opens on the WooCommerce My Account page.
This route does not run the CAPTCHA. A headless client has no widget. The route relies on the origin check, the hard stops, the honeypot and the IP rate limit.
Finish a two-factor challenge#
For logins that run over AJAX, start a challenge in PHP with nahnls_start_2fa_challenge() (see Functions reference). It returns a nonce_key. Then finish it with these routes. The user is not signed in yet, so the routes are public and need no X-WP-Nonce.
Rules
nonce_keymust be exactly 32 lowercase hex characters, and its challenge must still exist (10 minutes, extended on each wrong code). Otherwise you get400 nahnls_2fa_expired. Start the login again.- Wrong codes and failed passkey checks count toward the same limit as the challenge page. After 5, the challenge is deleted, the IP is locked out for 15 minutes (
429 nahnls_2fa_locked) and a lockout event is logged. - While an IP is blocked, locked out or geo-blocked, every
2fa/*route answers429 nahnls_2fa_locked. Over a per-IP rate limit it answers429 nahnls_2fa_rate. rememberandredirectcome from the challenge only, never from the request body, so a caller can’t make a session persistent or send the user off-site. The redirect goes throughlogin_redirectandwp_validate_redirect.- Success returns only
{ redirect }(no user data, no token), sets the login cookie, logs a success event, firesnahnls_2fa_verifiedand fireswp_loginagain. A challenge can be used once.
Routes
| Route | Body | Error codes |
|---|---|---|
2fa/verify | { nonce_key, code?, backup_code? } | 400 nahnls_2fa_invalid (with data.attempts_left), 400 nahnls_2fa_expired, 429 nahnls_2fa_locked |
2fa/email | { nonce_key } | 429 nahnls_2fa_rate (one email per 60 seconds per user, 3 requests per 15 minutes per IP), 400 nahnls_2fa_no_email, 500 nahnls_2fa_mail_failed, 400 nahnls_2fa_expired |
2fa/passkey/options | { nonce_key } | 400 nahnls_2fa_expired, 403 method_not_allowed, 400 nahnls_2fa_no_passkey |
2fa/passkey/verify | { nonce_key, credential } | As 2fa/verify, plus 400 nahnls_2fa_challenge_expired (the passkey challenge was used or is older than 2 minutes, so fetch new options; this isn’t counted as a failure) and 403 method_not_allowed |
code for 2fa/verify is the authenticator-app code or the emailed code (the authenticator is tried first). backup_code is a one-time backup code.
If 2FA is off, or NAHNLS_DISABLE_2FA is set, the 2fa/* routes answer 404 nahnls_feature_disabled.
Manage a user’s passkeys#
These routes need the login cookie and an X-WP-Nonce header, or an application password.
fetch('/wp-json/nahnls/v1/passkeys', {
credentials: 'same-origin',
headers: { 'X-WP-Nonce': wpApiSettings.nonce }
}).then(r => r.json()); GET passkeyslists the passkeys of the current user. An administrator can add?user_id=(needsedit_users).DELETE passkeys/{id}removes one.POST passkeys/register/optionsthenPOST passkeys/register/verifyadd a new one. The first call returns creation options fornavigator.credentials.create(). The second takes{ name, credential }and returns201.GET me/securityreturns a summary: whether 2FA is on, whether the user can use passkeys and magic links, and the passkey list.
Headless sites on another host#
Browser requests to the public routes are checked for where they come from. If the request carries an Origin (or Referer) header whose host isn’t your site’s, the route answers 403 nahnls_bad_origin. To allow your front end:
add_filter( 'nahnls_rest_allowed_origins', fn( array $hosts ) => [ ...$hosts, 'app.example.com' ] ); You also need:
- CORS for the REST API (
rest_allowed_cors_headersandAccess-Control-Allow-Credentials). credentials: 'include'on your requests. ForNAHNLSAuth, setwindow.NAHNLSAuthConfig = { credentials: 'include', … }.- Both passkey sign-in calls (
optionsandverify) from the same client IP.
Rate limits and lockouts#
The public routes are rate-limited per IP, and the limits are listed in the table above. Beyond that:
- A blocked, locked-out or geo-blocked IP is refused by every public route, like the normal login.
- Magic-link requests are limited per IP and per account (3 each per 15 minutes by default). Change them with the
nahnls_magic_link_rate_limitsfilter.
If your front end sits behind a proxy or CDN, make sure the plugin sees the real visitor IP, or all your users will share one limit. See Lockouts are keyed to the wrong IP (proxies and CDNs).
Role-based IP rules#
If you restrict some roles to certain IP addresses (Access tab), a signed-in user from the wrong IP is refused on the authenticated routes too. See Restrict roles to IP addresses.
Was this helpful?
Thanks for your feedback!
✓ Feedback received. Thank you!