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\Passwords;
4
 
5
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
6
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
7
use Illuminate\Database\ConnectionInterface;
8
use Illuminate\Support\Carbon;
9
use Illuminate\Support\Str;
10
 
11
class DatabaseTokenRepository implements TokenRepositoryInterface
12
{
13
    /**
14
     * The database connection instance.
15
     *
16
     * @var \Illuminate\Database\ConnectionInterface
17
     */
18
    protected $connection;
19
 
20
    /**
21
     * The Hasher implementation.
22
     *
23
     * @var \Illuminate\Contracts\Hashing\Hasher
24
     */
25
    protected $hasher;
26
 
27
    /**
28
     * The token database table.
29
     *
30
     * @var string
31
     */
32
    protected $table;
33
 
34
    /**
35
     * The hashing key.
36
     *
37
     * @var string
38
     */
39
    protected $hashKey;
40
 
41
    /**
42
     * The number of seconds a token should last.
43
     *
44
     * @var int
45
     */
46
    protected $expires;
47
 
48
    /**
49
     * Minimum number of seconds before re-redefining the token.
50
     *
51
     * @var int
52
     */
53
    protected $throttle;
54
 
55
    /**
56
     * Create a new token repository instance.
57
     *
58
     * @param  \Illuminate\Database\ConnectionInterface  $connection
59
     * @param  \Illuminate\Contracts\Hashing\Hasher  $hasher
60
     * @param  string  $table
61
     * @param  string  $hashKey
62
     * @param  int  $expires
63
     * @param  int  $throttle
64
     * @return void
65
     */
66
    public function __construct(ConnectionInterface $connection, HasherContract $hasher,
67
                                $table, $hashKey, $expires = 60,
68
                                $throttle = 60)
69
    {
70
        $this->table = $table;
71
        $this->hasher = $hasher;
72
        $this->hashKey = $hashKey;
73
        $this->expires = $expires * 60;
74
        $this->connection = $connection;
75
        $this->throttle = $throttle;
76
    }
77
 
78
    /**
79
     * Create a new token record.
80
     *
81
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
82
     * @return string
83
     */
84
    public function create(CanResetPasswordContract $user)
85
    {
86
        $email = $user->getEmailForPasswordReset();
87
 
88
        $this->deleteExisting($user);
89
 
90
        // We will create a new, random token for the user so that we can e-mail them
91
        // a safe link to the password reset form. Then we will insert a record in
92
        // the database so that we can verify the token within the actual reset.
93
        $token = $this->createNewToken();
94
 
95
        $this->getTable()->insert($this->getPayload($email, $token));
96
 
97
        return $token;
98
    }
99
 
100
    /**
101
     * Delete all existing reset tokens from the database.
102
     *
103
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
104
     * @return int
105
     */
106
    protected function deleteExisting(CanResetPasswordContract $user)
107
    {
108
        return $this->getTable()->where('email', $user->getEmailForPasswordReset())->delete();
109
    }
110
 
111
    /**
112
     * Build the record payload for the table.
113
     *
114
     * @param  string  $email
115
     * @param  string  $token
116
     * @return array
117
     */
118
    protected function getPayload($email, $token)
119
    {
120
        return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new Carbon];
121
    }
122
 
123
    /**
124
     * Determine if a token record exists and is valid.
125
     *
126
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
127
     * @param  string  $token
128
     * @return bool
129
     */
130
    public function exists(CanResetPasswordContract $user, $token)
131
    {
132
        $record = (array) $this->getTable()->where(
133
            'email', $user->getEmailForPasswordReset()
134
        )->first();
135
 
136
        return $record &&
137
               ! $this->tokenExpired($record['created_at']) &&
138
                 $this->hasher->check($token, $record['token']);
139
    }
140
 
141
    /**
142
     * Determine if the token has expired.
143
     *
144
     * @param  string  $createdAt
145
     * @return bool
146
     */
147
    protected function tokenExpired($createdAt)
148
    {
149
        return Carbon::parse($createdAt)->addSeconds($this->expires)->isPast();
150
    }
151
 
152
    /**
153
     * Determine if the given user recently created a password reset token.
154
     *
155
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
156
     * @return bool
157
     */
158
    public function recentlyCreatedToken(CanResetPasswordContract $user)
159
    {
160
        $record = (array) $this->getTable()->where(
161
            'email', $user->getEmailForPasswordReset()
162
        )->first();
163
 
164
        return $record && $this->tokenRecentlyCreated($record['created_at']);
165
    }
166
 
167
    /**
168
     * Determine if the token was recently created.
169
     *
170
     * @param  string  $createdAt
171
     * @return bool
172
     */
173
    protected function tokenRecentlyCreated($createdAt)
174
    {
175
        if ($this->throttle <= 0) {
176
            return false;
177
        }
178
 
179
        return Carbon::parse($createdAt)->addSeconds(
180
            $this->throttle
181
        )->isFuture();
182
    }
183
 
184
    /**
185
     * Delete a token record by user.
186
     *
187
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
188
     * @return void
189
     */
190
    public function delete(CanResetPasswordContract $user)
191
    {
192
        $this->deleteExisting($user);
193
    }
194
 
195
    /**
196
     * Delete expired tokens.
197
     *
198
     * @return void
199
     */
200
    public function deleteExpired()
201
    {
202
        $expiredAt = Carbon::now()->subSeconds($this->expires);
203
 
204
        $this->getTable()->where('created_at', '<', $expiredAt)->delete();
205
    }
206
 
207
    /**
208
     * Create a new token for the user.
209
     *
210
     * @return string
211
     */
212
    public function createNewToken()
213
    {
214
        return hash_hmac('sha256', Str::random(40), $this->hashKey);
215
    }
216
 
217
    /**
218
     * Get the database connection instance.
219
     *
220
     * @return \Illuminate\Database\ConnectionInterface
221
     */
222
    public function getConnection()
223
    {
224
        return $this->connection;
225
    }
226
 
227
    /**
228
     * Begin a new database query against the table.
229
     *
230
     * @return \Illuminate\Database\Query\Builder
231
     */
232
    protected function getTable()
233
    {
234
        return $this->connection->table($this->table);
235
    }
236
 
237
    /**
238
     * Get the hasher instance.
239
     *
240
     * @return \Illuminate\Contracts\Hashing\Hasher
241
     */
242
    public function getHasher()
243
    {
244
        return $this->hasher;
245
    }
246
}