Custom Authentification Challenge

I’m trying to create a custom auth challenge with a 9 digit code. I’ve been following this example: Auth challenges | Kirby CMS but I can’t figure out what I have to do next. I’ve been trying to call it in my config.php but nothing happens :frowning:

Here’s my code:

<?php

namespace Vanderlanth;

use Kirby;
use Kirby\Cms\Auth\Challenge;
use Kirby\Cms\User;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Str;

class CustomChallenge extends Challenge
{
    /**
     * Checks whether the challenge is available
     * for the passed user and purpose
     *
     * @param \Kirby\Cms\User $user User the code will be generated for
     * @param string $mode Purpose of the code ('login', 'reset' or '2fa')
     * @return bool
     */
    public static function isAvailable(User $user, string $mode): bool
    {
        // check the plugin configuration and the requirements of your challenge
        return true;
    }

    /**
     * Generates a random one-time auth code and returns that code
     * for later verification
     *
     * @param \Kirby\Cms\User $user User to generate the code for
     * @param array $options Details of the challenge request:
     *                       - 'mode': Purpose of the code ('login', 'reset' or '2fa')
     *                       - 'timeout': Number of seconds the code will be valid for
     * @return string|null The generated and sent code or `null` in case
     *                     there was no code to generate by this algorithm
     */
    public static function create(User $user, array $options): ?string
    {
        $code = Str::random(9, 'num');
        $formatted = substr($code, 0, 3) . ' ' . substr($code, 3, 3) . ' ' . substr($code, 6, 3);


        $kirby = $user->kirby();
        $kirby->email([
            'from' => 'suppor@overthere.link',
            'fromName' =>'overthere.link',
            'to' => $user,
            'subject' => 'Login code',
            'template' => 'auth/login',
            'data' => [
                'user'    => $user,
                'site'    => $kirby->system()->title(),
                'code'    => $formatted,
                'timeout' => 60
            ]
        ]);

        return $code;
    }
}

Kirby::plugin('vanderlanth/custom-challenge', [
    'authChallenges' => [
        'custom' => 'vanderlanth\CustomChallenge'
    ]
]);

and the auth part in the config.php

'auth' => [
  'trials' => 3,
  'timeout' => 3,
  'methods' => [
      'password' => ['2fa' => true]
  ],
  'challenge' => ['custom' ]
]

I would be infinitely grateful if someone can help me on this one.

Cheers,
Nico

This must be 'challenges' => ['custom']

I’d also recommend to change the placeholder to

            'login.code.placeholder.custom' => '000 000 000',

in the translation strings

1 Like