Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Auth;
4
 
5
use Illuminate\Contracts\Auth\Guard;
6
use Illuminate\Contracts\Auth\UserProvider;
7
use Illuminate\Http\Request;
8
 
9
class TokenGuard implements Guard
10
{
11
    use GuardHelpers;
12
 
13
    /**
14
     * The request instance.
15
     *
16
     * @var \Illuminate\Http\Request
17
     */
18
    protected $request;
19
 
20
    /**
21
     * The name of the query string item from the request containing the API token.
22
     *
23
     * @var string
24
     */
25
    protected $inputKey;
26
 
27
    /**
28
     * The name of the token "column" in persistent storage.
29
     *
30
     * @var string
31
     */
32
    protected $storageKey;
33
 
34
    /**
35
     * Indicates if the API token is hashed in storage.
36
     *
37
     * @var bool
38
     */
39
    protected $hash = false;
40
 
41
    /**
42
     * Create a new authentication guard.
43
     *
44
     * @param  \Illuminate\Contracts\Auth\UserProvider  $provider
45
     * @param  \Illuminate\Http\Request  $request
46
     * @param  string  $inputKey
47
     * @param  string  $storageKey
48
     * @param  bool  $hash
49
     * @return void
50
     */
51
    public function __construct(
52
        UserProvider $provider,
53
        Request $request,
54
        $inputKey = 'api_token',
55
        $storageKey = 'api_token',
56
        $hash = false)
57
    {
58
        $this->hash = $hash;
59
        $this->request = $request;
60
        $this->provider = $provider;
61
        $this->inputKey = $inputKey;
62
        $this->storageKey = $storageKey;
63
    }
64
 
65
    /**
66
     * Get the currently authenticated user.
67
     *
68
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
69
     */
70
    public function user()
71
    {
72
        // If we've already retrieved the user for the current request we can just
73
        // return it back immediately. We do not want to fetch the user data on
74
        // every call to this method because that would be tremendously slow.
75
        if (! is_null($this->user)) {
76
            return $this->user;
77
        }
78
 
79
        $user = null;
80
 
81
        $token = $this->getTokenForRequest();
82
 
83
        if (! empty($token)) {
84
            $user = $this->provider->retrieveByCredentials([
85
                $this->storageKey => $this->hash ? hash('sha256', $token) : $token,
86
            ]);
87
        }
88
 
89
        return $this->user = $user;
90
    }
91
 
92
    /**
93
     * Get the token for the current request.
94
     *
95
     * @return string
96
     */
97
    public function getTokenForRequest()
98
    {
99
        $token = $this->request->query($this->inputKey);
100
 
101
        if (empty($token)) {
102
            $token = $this->request->input($this->inputKey);
103
        }
104
 
105
        if (empty($token)) {
106
            $token = $this->request->bearerToken();
107
        }
108
 
109
        if (empty($token)) {
110
            $token = $this->request->getPassword();
111
        }
112
 
113
        return $token;
114
    }
115
 
116
    /**
117
     * Validate a user's credentials.
118
     *
119
     * @param  array  $credentials
120
     * @return bool
121
     */
122
    public function validate(array $credentials = [])
123
    {
124
        if (empty($credentials[$this->inputKey])) {
125
            return false;
126
        }
127
 
128
        $credentials = [$this->storageKey => $credentials[$this->inputKey]];
129
 
130
        if ($this->provider->retrieveByCredentials($credentials)) {
131
            return true;
132
        }
133
 
134
        return false;
135
    }
136
 
137
    /**
138
     * Set the current request instance.
139
     *
140
     * @param  \Illuminate\Http\Request  $request
141
     * @return $this
142
     */
143
    public function setRequest(Request $request)
144
    {
145
        $this->request = $request;
146
 
147
        return $this;
148
    }
149
}