InMemorySamlRepository.php 1.22 KB
Newer Older
1
2
3
4
5
6
7
<?php


namespace Source\Gateways\Saml;

use Source\Entities\SamlUser;

Jacob Priddy's avatar
Jacob Priddy committed
8
9
class InMemorySamlRepository implements SamlRepository
{
10
    protected ?SamlUser $userToLogInAs = null;
11

Jacob Priddy's avatar
Jacob Priddy committed
12
    protected ?SamlUser $loggedInUser;
13
14
15
16
17
18
19
20
21
22
23
24
25

    protected string $loginUrl;

    protected string $logoutUrl;

    public function __construct(
        string $loginUrl,
        string $logoutUrl
    ) {
        $this->loginUrl = $loginUrl;
        $this->logoutUrl = $logoutUrl;
    }

26
    public function setLoginUser(?SamlUser $user): void
Jacob Priddy's avatar
Jacob Priddy committed
27
    {
28
29
30
31
32
33
        $this->userToLogInAs = $user;
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
34
35
    public function login(array $options = []): string
    {
36
        $this->loggedInUser = $this->userToLogInAs;
37

38
39
40
41
42
43
        return $this->loginUrl;
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
44
45
    public function handleLogin(): ?SamlUser
    {
Jacob Priddy's avatar
Jacob Priddy committed
46
47
48
49
50
        if ($this->isAuthenticated()) {
            return $this->loggedInUser;
        }

        return null;
51
52
53
54
55
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
56
57
    public function logout(): string
    {
58
        $this->loggedInUser = null;
59

60
61
62
63
64
65
        return $this->logoutUrl;
    }

    /**
     * @inheritDoc
     */
Jacob Priddy's avatar
Jacob Priddy committed
66
67
    public function isAuthenticated(): bool
    {
68
69
70
        return $this->loggedInUser !== null;
    }
}