ApiGuard.php 3.47 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
<?php


namespace App\Guards;


use App\User;
8
use App\Token;
Jacob Priddy's avatar
Jacob Priddy committed
9
10
11
12
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
13
14
use Source\UseCases\Token\Authenticate\AuthenticateUseCase;
use Source\UseCases\Token\Authenticate\TranslationPresenter;
Jacob Priddy's avatar
Jacob Priddy committed
15
16
17
18
19
20
21
22
23

class ApiGuard implements Guard {
    use GuardHelpers;

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

    /**
     * The name of the query string item from the request containing the API token.
     *
     * @var string
     */
31
    protected string $inputKey;
Jacob Priddy's avatar
Jacob Priddy committed
32
33
34
35
36
37

    /**
     * The name of the token "column" in persistent storage.
     *
     * @var string
     */
38
    protected string $storageKey;
Jacob Priddy's avatar
Jacob Priddy committed
39

Jacob Priddy's avatar
Jacob Priddy committed
40
41
    protected AuthenticateUseCase $authenticator;

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

    /**
     * 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.
     *
88
     * @return string|null
Jacob Priddy's avatar
Jacob Priddy committed
89
     */
90
    public function getTokenForRequest(): ?string {
Jacob Priddy's avatar
Jacob Priddy committed
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        $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
     */
    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
144
145
146
147
148
        $presenter = new TranslationPresenter();

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

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