Auth challenges
An auth challenge confirms a login, e.g. with a one-time code or a passkey. Kirby uses challenges as second factor after a password, for passwordless logins with the code method and for password resets. Kirby comes with the email, totp and webauthn challenges. Plugins can add their own with the authChallenges extension.
Registering a challenge
<?php
load([
'Acme\\Auth\\SmsChallenge' => 'classes/SmsChallenge.php'
], __DIR__);
Kirby::plugin(
name: 'acme/auth',
extends: [
'authChallenges' => [
'sms' => Acme\Auth\SmsChallenge::class
]
]
);
Registering a challenge doesn't enable it. Site developers add it to the auth.challenges option, in the position they want:
return [
'auth' => [
'challenges' => ['webauthn', 'totp', 'sms', 'email']
]
];
Kirby passes the key you register to your class as its type: $this->type() returns sms. Like auth methods, one class can be registered under several types, each with its own options.
The challenge class
Auth challenges extend the abstract Kirby\Auth\Challenge class. Kirby creates a challenge for a specific user and purpose ('login', 'password-reset' or '2fa'), together with the registered type, the timeout and its options. They are available as $this->user, $this->mode, $this->type, $this->timeout and $this->options.
| Method | Description | |
|---|---|---|
create() |
required | Generates and sends the code (or whatever your challenge needs) and returns the data to verify it later. Return null if there's nothing to remember, e.g. for codes generated by an app. |
verify() |
required | Checks the user's input against the data from create() |
form() |
needed for the Panel | The Vue component (and its props) for the login view |
icon() |
optional | Icon in the challenge switcher, default: key |
isAvailable() |
optional | Whether $this->user can use this challenge for $this->mode, default: true |
static isEnabled() |
optional | Whether the challenge can be used on this site at all, default: true. It is static, because Kirby checks it without a user, and receives the Kirby\Auth\Auth object, the registered $type and its $options. |
isSingleUse() |
optional | Return true if a failed attempt must discard the pending challenge (e.g. a signed nonce), default: false |
label() |
optional | Text in the challenge switcher, default: the translation login.challenge.{type}.label |
settings() |
optional | Kirby\Panel\Ui\Button objects for the security drawer |
type() |
The key the challenge is registered with |
If you override the constructor, accept all arguments of the parent constructor and pass them on, as Kirby passes them as named arguments. The constructor must not throw or have side effects: Kirby also creates challenges just to check whether they are available.
The Pending object
create() returns a Kirby\Auth\Pending object with two optional slots:
use Kirby\Auth\Pending;
return new Pending(
// sent to the Panel, e.g. a masked phone number
public: '•••• 1234',
// stays in the session, only passed to verify()
secret: password_hash($code, PASSWORD_DEFAULT)
);
Both end up in the session, so only use strings, numbers and arrays. You get the same object back in form() and verify(). Hashing is your job: never put a plain code into secret.
Kirby checks the timeout before it calls verify() and rate-limits every attempt. A wrong code can be retried until the challenge times out, unless isSingleUse() returns true. The timeout is set with the auth.challenge.timeout option.
Availability means "set up"
Every user can opt into a second factor. Kirby considers a second factor as set up when isAvailable() returns true for the 2fa mode. Such users are asked for it after every password login, even if the site doesn't require a second factor from everyone.
If your challenge is available for every user (e.g. everybody with a phone number), make the 2fa mode opt-in: store a flag in the user's secrets once the user has confirmed the factor, and check it in isAvailable(). The core email challenge does exactly that with $user->secret('email'). Add a settings() button so users can turn the factor on and off in their security drawer.
For the login and password-reset modes, Kirby only offers challenges that the user has also set up as second factor (if they have any), so a password reset can never be weaker than the user's two-factor setup.
Example: codes via SMS
This challenge sends a code via SMS to the phone number in the user's content:
<?php
namespace Acme\Auth;
use Kirby\Auth\Challenge;
use Kirby\Auth\Pending;
use Kirby\Panel\Ui\Component;
use Kirby\Toolkit\Str;
use SensitiveParameter;
class SmsChallenge extends Challenge
{
public function isAvailable(): bool
{
return $this->user->content()->get('phone')->isNotEmpty();
}
public function create(): Pending
{
$code = Str::random(6, 'num');
$phone = $this->user->content()->get('phone')->value();
// your SMS provider's client
Gateway::send($phone, 'Your login code: ' . $code);
return new Pending(
public: '•••• ' . substr($phone, -4),
secret: password_hash($code, PASSWORD_DEFAULT)
);
}
public function form(Pending $pending): Component
{
return new Component(
component: 'k-login-sms-challenge-form',
submit: $this->submit(),
phone: $pending->public(),
user: $this->user->email(),
);
}
public function icon(): string
{
return 'mobile';
}
public function verify(
#[SensitiveParameter]
mixed $input,
Pending $data
): bool {
$hash = $data->secret();
if (is_string($input) === false || is_string($hash) === false) {
return false;
}
return password_verify(str_replace(' ', '', $input), $hash);
}
}
With isAvailable() as above, every user with a phone number is asked for an SMS code after their password. Make the 2fa mode opt-in as described above if that's not what you want.
The plugin registers the challenge and its translations:
Kirby::plugin(
name: 'acme/auth',
extends: [
'authChallenges' => [
'sms' => Acme\Auth\SmsChallenge::class
],
'translations' => [
'en' => [
'login.challenge.sms.label' => 'Code via SMS',
'login.code.label.sms' => 'Code from SMS',
'login.code.placeholder.sms' => '000000',
'login.code.text.sms' => 'We sent a code to { phone }.'
]
]
]
);
The challenge brings its own form for the login view:
<template>
<form class="k-login-form" @submit.prevent="onSubmit">
<k-user-info :user="user" />
<k-text-field
:autofocus="true"
:counter="false"
:help="$t('login.code.text.sms', { phone })"
:label="$t('login.code.label.sms')"
:placeholder="$t('login.code.placeholder.sms')"
:required="true"
:value="code"
autocomplete="one-time-code"
icon="unlock"
name="code"
@input="code = $event"
/>
<k-login-footer :back="true">
<k-login-submit :icon="submit.icon" :label="submit.label" />
</k-login-footer>
</form>
</template>
<script>
export default {
props: {
phone: String,
submit: Object,
user: String
},
emits: ["submit"],
data() {
return {
code: ""
};
},
methods: {
onSubmit() {
this.$emit("submit", { code: this.code });
}
}
};
</script>
import LoginSmsChallengeForm from "./components/LoginSmsChallengeForm.vue";
window.panel.plugin("acme/auth", {
components: {
"k-login-sms-challenge-form": LoginSmsChallengeForm
}
});
If you don't need the phone number in the form, <k-login-code type="sms" :value="code" @input="code = $event" /> replaces the whole <k-text-field>.
The auth methods reference explains how the login view renders your form, how to add entries to the security drawer and how to protect custom routes with Auth::guard().