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

namespace Source\UseCases\Users\Authenticate;

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

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

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

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

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

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

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

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

Jacob Priddy's avatar
Jacob Priddy committed
56 57 58
        if (!$user ||
            !$user->getPassword() ||
            !$user->getPassword()->matches($password)) {
59
            throw new AuthenticationException();
Jacob Priddy's avatar
Jacob Priddy committed
60 61 62 63 64 65
        }

        $token = $this->tokens->create(
            new Token(
                0,
                $user->getId(),
Jacob Priddy's avatar
Jacob Priddy committed
66
                $this->tokens::generateTokenString(),
Jacob Priddy's avatar
Jacob Priddy committed
67
                null,
68
                Carbon::now()->addDays(2)
Jacob Priddy's avatar
Jacob Priddy committed
69 70 71 72 73 74 75
            )
        );

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

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

77 78 79
    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
80 81
    public function handToSaml(array $options = []): string
    {
Jacob Priddy's avatar
Jacob Priddy committed
82
        return $this->saml->login($options);
83 84 85 86 87
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
88 89
    public function handleSamlLogin(Presenter $presenter): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        $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) {
            $user = $this->users->create(
                new User(
                    0,
                    $samlUser->getFirstName(),
                    $samlUser->getLastName(),
                    $samlUser->getDisplayName(),
                    $samlUser->getEmail(),
                    $samlUser->getEmplid(),
                    null,
                    null
                )
            );
        }

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

        $token = $this->tokens->create(
            new Token(
                0,
                $user->getId(),
Jacob Priddy's avatar
Jacob Priddy committed
123
                $this->tokens::generateTokenString(),
Jacob Priddy's avatar
Jacob Priddy committed
124 125 126 127 128 129 130 131
                null,
                Carbon::now()->addDays(2)
            )
        );

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

        $presenter->present($response);
132 133 134 135 136
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
137 138
    public function samlLogout(?string $token): string
    {
Jacob Priddy's avatar
Jacob Priddy committed
139 140 141 142 143
        if ($token) {
            $this->tokens->invalidateToken($token);
        }

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