Authenticate.php 3.59 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1 2 3 4
<?php

namespace Source\UseCases\Users\Authenticate;

5 6
use Source\Entities\User;
use Source\Gateways\Saml\SamlRepository;
Jacob Priddy's avatar
Jacob Priddy committed
7 8
use Source\Gateways\Users\UsersRepository;
use Source\Gateways\Tokens\TokensRepository;
9
use Source\Exceptions\AuthenticationException;
Jacob Priddy's avatar
Jacob Priddy committed
10

Jacob Priddy's avatar
Jacob Priddy committed
11 12
class Authenticate implements AuthenticateUseCase
{
13 14 15
    /**
     * @var \Source\Gateways\Users\UsersRepository
     */
Jacob Priddy's avatar
Jacob Priddy committed
16 17
    protected UsersRepository $users;

18 19 20
    /**
     * @var \Source\Gateways\Tokens\TokensRepository
     */
Jacob Priddy's avatar
Jacob Priddy committed
21 22
    protected TokensRepository $tokens;

Jacob Priddy's avatar
Jacob Priddy committed
23 24 25 26
    /**
     * @var \Source\Gateways\Saml\SamlRepository
     */
    protected SamlRepository $saml;
27

28 29 30
    /**
     * @param \Source\Gateways\Users\UsersRepository   $users
     * @param \Source\Gateways\Tokens\TokensRepository $tokens
Jacob Priddy's avatar
Jacob Priddy committed
31
     * @param \Source\Gateways\Saml\SamlRepository     $saml
32
     */
Jacob Priddy's avatar
Jacob Priddy committed
33
    public function __construct(UsersRepository $users, TokensRepository $tokens, SamlRepository $saml)
Jacob Priddy's avatar
Jacob Priddy committed
34
    {
Jacob Priddy's avatar
Jacob Priddy committed
35
        $this->saml = $saml;
Jacob Priddy's avatar
Jacob Priddy committed
36 37 38 39 40 41 42
        $this->users = $users;
        $this->tokens = $tokens;
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
43 44
    public function attempt(Presenter $presenter, array $credentials): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
45 46 47 48
        $email = $credentials['email'] ?? null;
        $password = $credentials['password'] ?? null;

        if (!$email || !$password) {
49
            throw new AuthenticationException();
Jacob Priddy's avatar
Jacob Priddy committed
50 51
        }

Jacob Priddy's avatar
Jacob Priddy committed
52
        $user = $this->users->findByEmail(strtolower($email));
Jacob Priddy's avatar
Jacob Priddy committed
53

Jacob Priddy's avatar
Jacob Priddy committed
54 55 56
        if (!$user ||
            !$user->getPassword() ||
            !$user->getPassword()->matches($password)) {
57
            throw new AuthenticationException();
Jacob Priddy's avatar
Jacob Priddy committed
58 59
        }

60
        $token = $this->tokens->createLoginToken($user->getId());
Jacob Priddy's avatar
Jacob Priddy committed
61 62 63 64 65

        $response = new ResponseModel($user, $token);

        $presenter->present($response);
    }
66

67 68 69
    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
70 71
    public function handToSaml(array $options = []): string
    {
Jacob Priddy's avatar
Jacob Priddy committed
72
        return $this->saml->login($options);
73 74 75 76 77
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
78 79
    public function handleSamlLogin(Presenter $presenter): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
80 81 82 83 84 85 86 87 88 89 90
        $samlUser = $this->saml->handleLogin();

        if (!$samlUser) {
            throw new UserCreationException();
        }

        // First check to see if the user exists in the database.
        $user = $this->users->findByEmail($samlUser->getEmail());

        // If the user does not exist, create them.
        if (!$user) {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            $user = $this->users->create(new User(
                0,
                $samlUser->getFirstName(),
                $samlUser->getLastName(),
                $samlUser->getDisplayName(),
                $samlUser->getEmail(),
                $samlUser->getEmplid()
            ));
        } else {
            $user = $this->users->update($user->getId(), new User(
                $user->getId(),
                $samlUser->getFirstName(),
                $samlUser->getLastName(),
                $samlUser->getDisplayName(),
                $samlUser->getEmail(),
                $samlUser->getEmplid(),
                $user->getPassword(),
                $user->getDoorcode(),
                $user->getExpiresAt(),
                $user->getCreatedAt(),
                $user->getUpdatedAt()
            ));
Jacob Priddy's avatar
Jacob Priddy committed
113 114 115 116 117 118
        }

        if (!$user) {
            throw new UserCreationException();
        }

119
        $token = $this->tokens->createLoginToken($user->getId());
Jacob Priddy's avatar
Jacob Priddy committed
120 121 122 123

        $response = new ResponseModel($user, $token);

        $presenter->present($response);
124 125 126 127 128
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
129 130
    public function samlLogout(?string $token): string
    {
Jacob Priddy's avatar
Jacob Priddy committed
131 132 133 134 135
        if ($token) {
            $this->tokens->invalidateToken($token);
        }

        return $this->saml->logout();
136
    }
Jacob Priddy's avatar
Jacob Priddy committed
137
}