DoorGuard.php 3.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php


namespace App\Guards;

use Illuminate\Http\Request;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Authenticatable;
Jacob Priddy's avatar
Jacob Priddy committed
10
use Source\Exceptions\AuthenticationException;
11
12
13
use Source\UseCases\Doors\Authenticate\AuthenticateUseCase;
use Source\UseCases\Doors\Authenticate\TranslationPresenter;

Jacob Priddy's avatar
Jacob Priddy committed
14
15
class DoorGuard implements Guard
{
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    use GuardHelpers;

    /**
     * The request instance.
     *
     * @var Request
     */
    protected Request $request;

    /**
     * The name of the query string item from the request containing the API token.
     *
     * @var string
     */
    protected string $inputKey;

    /**
     * @var AuthenticateUseCase
     */
    protected AuthenticateUseCase $authenticator;

    /**
     * Create a new authentication guard.
     *
     * @param AuthenticateUseCase $authenticator
     * @param Request             $request
     * @param string              $inputKey
     */
    public function __construct(
        AuthenticateUseCase $authenticator,
        Request $request,
Jacob Priddy's avatar
Jacob Priddy committed
47
48
        $inputKey = 'api_token'
    ) {
49
50
51
52
53
54
55
56
57
58
        $this->request = $request;
        $this->inputKey = $inputKey;
        $this->authenticator = $authenticator;
    }

    /**
     * Get the currently authenticated user.
     *
     * @return Authenticatable|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
59
60
    public function user()
    {
61
62
63
64
65
66
67
68
69
70
71
        // If we've already retrieved the user for the current request we can just
        // return it back immediately. We do not want to fetch the user data on
        // every call to this method because that would be tremendously slow.
        if ($this->user !== null) {
            return $this->user;
        }

        $user = null;

        $token = $this->getTokenForRequest();

Jacob Priddy's avatar
Jacob Priddy committed
72
        $user = $this->retrieveByToken($token);
73
74
75
76
77
78
79
80
81

        return $this->user = $user;
    }

    /**
     * Get the token for the current request.
     *
     * @return string|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
82
83
    public function getTokenForRequest(): ?string
    {
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
        $token = $this->request->query($this->inputKey);

        if (empty($token)) {
            $token = $this->request->input($this->inputKey);
        }

        if (empty($token)) {
            $token = $this->request->bearerToken();
        }

        if (empty($token)) {
            $token = $this->request->getPassword();
        }

        return $token;
    }

    /**
     * Validate a user's credentials.
     *
     * @param array $credentials
     * @return bool
     */
Jacob Priddy's avatar
Jacob Priddy committed
107
108
    public function validate(array $credentials = [])
    {
Jacob Priddy's avatar
Jacob Priddy committed
109
        if (!isset($credentials[$this->inputKey])) {
110
111
112
            return false;
        }

Jacob Priddy's avatar
Jacob Priddy committed
113
        return (bool) $this->retrieveByToken($credentials[$this->inputKey]);
114
115
116
117
118
119
120
121
    }

    /**
     * Set the current request instance.
     *
     * @param Request $request
     * @return $this
     */
Jacob Priddy's avatar
Jacob Priddy committed
122
123
    public function setRequest(Request $request): self
    {
124
125
126
127
128
129
130
131
132
133
        $this->request = $request;

        return $this;
    }

    /**
     * @param string $token
     *
     * @return Authenticatable|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
134
    public function retrieveByToken(?string $token): ?Authenticatable
Jacob Priddy's avatar
Jacob Priddy committed
135
    {
136
137
138
139
140
141
142
        $presenter = new TranslationPresenter();

        $this->authenticator->check($presenter, $token);

        return $presenter->getViewModel();
    }
}