ApiGuard.php 3.33 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
8
9
10
<?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
11
12
use Source\UseCases\Token\Authenticate\AuthenticateUseCase;
use Source\UseCases\Token\Authenticate\TranslationPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
13
14
15
16
17
18
19
20
21

class ApiGuard implements Guard {
    use GuardHelpers;

    /**
     * The request instance.
     *
     * @var Request
     */
22
    protected Request $request;
Jacob Priddy's avatar
Jacob Priddy committed
23
24
25
26
27
28

    /**
     * The name of the query string item from the request containing the API token.
     *
     * @var string
     */
29
    protected string $inputKey;
Jacob Priddy's avatar
Jacob Priddy committed
30
31

    /**
32
     * @var AuthenticateUseCase
Jacob Priddy's avatar
Jacob Priddy committed
33
     */
Jacob Priddy's avatar
Jacob Priddy committed
34
35
    protected AuthenticateUseCase $authenticator;

Jacob Priddy's avatar
Jacob Priddy committed
36
37
38
    /**
     * Create a new authentication guard.
     *
Jacob Priddy's avatar
Jacob Priddy committed
39
40
41
     * @param AuthenticateUseCase $authenticator
     * @param Request             $request
     * @param string              $inputKey
Jacob Priddy's avatar
Jacob Priddy committed
42
43
     */
    public function __construct(
Jacob Priddy's avatar
Jacob Priddy committed
44
        AuthenticateUseCase $authenticator,
Jacob Priddy's avatar
Jacob Priddy committed
45
        Request $request,
46
        $inputKey = 'api_token'){
Jacob Priddy's avatar
Jacob Priddy committed
47
48
        $this->request = $request;
        $this->inputKey = $inputKey;
Jacob Priddy's avatar
Jacob Priddy committed
49
        $this->authenticator = $authenticator;
Jacob Priddy's avatar
Jacob Priddy committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    }

    /**
     * Get the currently authenticated user.
     *
     * @return Authenticatable|null
     */
    public function user() {
        // 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();

        if (!empty($token)) {
            $user = $this->retrieveByToken($token);
        }

        return $this->user = $user;
    }

    /**
     * Get the token for the current request.
     *
79
     * @return string|null
Jacob Priddy's avatar
Jacob Priddy committed
80
     */
81
    public function getTokenForRequest(): ?string {
Jacob Priddy's avatar
Jacob Priddy committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        $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();
        }

Jacob Priddy's avatar
Jacob Priddy committed
96
97
98
99
        if (empty($token)) {
            $token = $this->request->cookie($this->inputKey);
        }

Jacob Priddy's avatar
Jacob Priddy committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
        return $token;
    }

    /**
     * Validate a user's credentials.
     *
     * @param array $credentials
     * @return bool
     */
    public function validate(array $credentials = []) {
        if (empty($credentials[$this->inputKey])) {
            return false;
        }

        if ($this->retrieveByToken($credentials[$this->inputKey])) {
            return true;
        }

        return false;
    }

    /**
     * Set the current request instance.
     *
     * @param Request $request
     * @return $this
     */
    public function setRequest(Request $request): self {
        $this->request = $request;

        return $this;
    }

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

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

        return $presenter->getViewModel();
Jacob Priddy's avatar
Jacob Priddy committed
144
145
    }
}