silverstripe / silverstripe-webauthn-authenticator
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SilverStripe\WebAuthn;
6

7
use SilverStripe\MFA\Model\RegisteredMethod;
8
use SilverStripe\MFA\Service\RegisteredMethodManager;
9
use SilverStripe\MFA\Store\StoreInterface;
10
use SilverStripe\Security\Member;
11
use Webauthn\PublicKeyCredentialUserEntity;
12

13
trait CredentialRepositoryProviderTrait
14
{
15
    /**
16
     * @param StoreInterface $store
17
     * @param RegisteredMethod|null $registeredMethod
18
     * @return CredentialRepository
19
     */
20 2
    protected function getCredentialRepository(
21
        StoreInterface $store,
22
        RegisteredMethod $registeredMethod = null
23
    ): CredentialRepository {
24 2
        $state = $store->getState();
25

26
        // Check state from the store (session)
27 2
        if (isset($state['repository']) && $state['repository'] instanceof CredentialRepository) {
28 2
            return $state['repository'];
29
        }
30

31
        // Check if the member has an existing webauthn registered method to add to
32 2
        $member = $store->getMember();
33 2
        if (!$registeredMethod) {
34 2
            $registeredMethod = RegisteredMethodManager::singleton()->getFromMember(
35 2
                $member,
36 2
                new Method()
37
            );
38
        }
39

40 2
        if ($registeredMethod) {
41 2
            $credentialRepository = CredentialRepository::fromArray(
42 2
                (array) json_decode($registeredMethod->Data, true),
43 2
                (string) $member->ID
44
            );
45
        } else {
46 2
            $credentialRepository = new CredentialRepository((string) $member->ID);
47
        }
48

49
        // Persist the credential repository in the store
50 2
        $store->addState(['repository' => $credentialRepository]);
51

52 2
        return $credentialRepository;
53
    }
54

55
    /**
56
     * @param Member $member
57
     * @return PublicKeyCredentialUserEntity
58
     */
59 2
    protected function getUserEntity(Member $member): PublicKeyCredentialUserEntity
60
    {
61 2
        return new PublicKeyCredentialUserEntity(
62 2
            $member->getName(),
63 2
            (string) $member->ID,
64 2
            $member->getName()
65
        );
66
    }
67
}

Read our documentation on viewing source code .

Loading