Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Commit e92d34c

Browse files
committed
Initial commit
0 parents  commit e92d34c

13 files changed

+867
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
composer.lock

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "barchart/laravel-remember-all",
3+
"description": "A Laravel session driver to remember all devices a user has logged in with.",
4+
"keywords": [
5+
"laravel",
6+
"session",
7+
"remember",
8+
"remember all",
9+
"remember many"
10+
],
11+
"homepage": "https://www.barchart.com",
12+
"support": {
13+
"issues": "https://github.com/barchart/laravel-remember-all/issues",
14+
"source": "https://github.com/barchart/laravel-remember-all"
15+
},
16+
"license": "MIT",
17+
"authors": [
18+
{
19+
"name": "Tom Sisk",
20+
"email": "tom.sisk@barchart.com",
21+
"homepage": "https://www.barchart.com",
22+
"role": "Developer"
23+
}
24+
],
25+
"require": {
26+
"illuminate/auth": "~5.7"
27+
},
28+
"require-dev": {
29+
"mockery/mockery": "~1.0"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"Barchart\\Laravel\\RememberAll\\": "src"
34+
}
35+
},
36+
"minimum-stability": "dev",
37+
"prefer-stable": true
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateRememberTokensTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('remember_tokens', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('token', 100);
19+
$table->integer('user_id');
20+
$table->timestamps();
21+
$table->dateTime('expires_at');
22+
23+
$table->unique(['token', 'user_id']);
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('remember_tokens');
35+
}
36+
}

phpunit.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="false"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
syntaxCheck="false"
13+
>
14+
<testsuites>
15+
<testsuite name="Passport Test Suite">
16+
<directory suffix=".php">./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist processUncoveredFilesFromWhitelist="true">
21+
<directory suffix=".php">./src/</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/Contracts/Authenticatable.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Barchart\Laravel\RememberAll\Contracts;
4+
5+
use Illuminate\Contracts\Auth\Authenticatable as BaseAuthenticatable;
6+
7+
interface Authenticatable extends BaseAuthenticatable
8+
{
9+
/**
10+
* Get the name of the unique identifier for the user.
11+
*
12+
* @return string
13+
*/
14+
public function getAuthIdentifierName();
15+
16+
/**
17+
* Get the unique identifier for the user.
18+
*
19+
* @return mixed
20+
*/
21+
public function getAuthIdentifier();
22+
23+
/**
24+
* Get the password for the user.
25+
*
26+
* @return string
27+
*/
28+
public function getAuthPassword();
29+
30+
/**
31+
* Get the "remember me" session tokens for the user.
32+
*
33+
* @return string
34+
*/
35+
public function rememberTokens();
36+
}

src/DatabaseUserProvider.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Barchart\Laravel\RememberAll;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Auth\DatabaseUserProvider as BaseUserProvider;
7+
use Barchart\Laravel\RememberAll\Contracts\Authenticatable as UserContract;
8+
9+
class DatabaseUserProvider extends BaseUserProvider implements UserProvider
10+
{
11+
/**
12+
* Retrieve a user by their unique identifier and "remember me" token.
13+
*
14+
* @param mixed $identifier
15+
* @param string $token
16+
* @return \Illuminate\Contracts\Auth\Authenticatable|null
17+
*/
18+
public function retrieveByToken($identifier, $token)
19+
{
20+
$user = $this->conn->table($this->table)
21+
->select($this->table.'.*')
22+
->leftJoin('remember_tokens', 'remember_tokens.user', '=', $this->table.'.'.$user->getAuthIdentifierName())
23+
->where($this->table.'.'.$user->getAuthIdentifierName(), $identifier)
24+
->where('remember_tokens.token', $token)
25+
->where('remember_tokens.expires_at', '<', Carbon::now())
26+
->first();
27+
28+
return $user ? $this->getGenericUser($user) : null;
29+
}
30+
31+
/**
32+
* Add a token value for the "remember me" session.
33+
*
34+
* @param string $value
35+
* @param int $expire
36+
* @return void
37+
*/
38+
public function addRememberToken($identifier, $value, $expire)
39+
{
40+
$this->conn->table('remember_tokens')->create([
41+
'token' => $value,
42+
'user_id' => $identifier,
43+
'expires_at' => Carbon::now()->addMinutes($expire),
44+
]);
45+
}
46+
47+
/**
48+
* Replace "remember me" token with new token.
49+
*
50+
* @param string $token
51+
* @param string $newToken
52+
* @param int $expire
53+
*
54+
* @return void
55+
*/
56+
public function replaceRememberToken($identifier, $token, $newToken, $expire)
57+
{
58+
$model = $this->getModelByIdentifier($identifier);
59+
60+
$this->conn->table('remember_tokens')
61+
->where('user_id', $identifier)
62+
->where('token', $token)
63+
->update([
64+
'token' => $newToken,
65+
'expires_at' => Carbon::now()->addMinutes($expire);
66+
]);
67+
}
68+
69+
/**
70+
* Delete the specified "remember me" token for the given user.
71+
*
72+
* @param mixed $identifier
73+
* @param string $token
74+
* @return null
75+
*/
76+
public function deleteRememberToken($identifier, $token)
77+
{
78+
$this->conn->table('remember_tokens')
79+
->where('user_id', $identifier)
80+
->where('token', $token)
81+
->delete();
82+
}
83+
84+
/**
85+
* Purge old or expired "remember me" tokens.
86+
*
87+
* @param mixed $identifier
88+
* @param bool $expired
89+
* @return null
90+
*/
91+
public function purgeRememberTokens($identifier, $expired = false)
92+
{
93+
$query = $this->conn->table('remember_tokens')
94+
->where('user_id', $identifier)
95+
->where('remember_tokens.token', $token);
96+
97+
if ($expired) {
98+
$query->where('expires_at', '<', Carbon::now());
99+
}
100+
101+
$query->delete();
102+
}
103+
}

src/EloquentAuthenticatable.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Barchart\Laravel\RememberAll;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Auth\Authenticatable as BaseAuthenticatable;
7+
8+
trait EloquentAuthenticatable
9+
{
10+
use BaseAuthenticatable;
11+
12+
/**
13+
* Get the "remember me" session tokens for the user.
14+
*
15+
* @return Illuminate\Database\Eloquent\Relations\HasMany
16+
*/
17+
public function rememberTokens()
18+
{
19+
return $this->hasMany(RememberToken::class);
20+
}
21+
}

src/EloquentUserProvider.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Barchart\Laravel\RememberAll;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Auth\EloquentUserProvider as BaseUserProvider;
7+
use Barchart\Laravel\RememberAll\Contracts\Authenticatable as UserContract;
8+
9+
class EloquentUserProvider extends BaseUserProvider implements UserProvider
10+
{
11+
/**
12+
* Retrieve a user by their unique identifier and "remember me" token.
13+
*
14+
* @param mixed $identifier
15+
* @param string $token
16+
* @return \Illuminate\Contracts\Auth\Authenticatable|null
17+
*/
18+
public function retrieveByToken($identifier, $token)
19+
{
20+
if (! $model = $this->getModelByIdentifier($identifier)) {
21+
return null;
22+
}
23+
24+
$rememberTokens = $model->rememberTokens()->where('expires_at', '>', Carbon::now())->get();
25+
26+
foreach ($rememberTokens as $rememberToken) {
27+
if (hash_equals($rememberToken->token, $token)) {
28+
return $model;
29+
}
30+
}
31+
}
32+
33+
/**
34+
* Add a token value for the "remember me" session.
35+
*
36+
* @param string $value
37+
* @param int $expire
38+
* @return void
39+
*/
40+
public function addRememberToken($identifier, $value, $expire)
41+
{
42+
$model = $this->getModelByIdentifier($identifier);
43+
44+
if ($model) {
45+
$model->rememberTokens()->create([
46+
'token' => $value,
47+
'expires_at' => Carbon::now()->addMinutes($expire),
48+
]);
49+
}
50+
}
51+
52+
/**
53+
* Replace "remember me" token with new token.
54+
*
55+
* @param string $token
56+
* @param string $newToken
57+
* @param int $expire
58+
*
59+
* @return void
60+
*/
61+
public function replaceRememberToken($identifier, $token, $newToken, $expire)
62+
{
63+
$model = $this->getModelByIdentifier($identifier);
64+
65+
if ($model) {
66+
$model->rememberTokens()->where('token', $token)->update([
67+
'token' => $newToken,
68+
'expires_at' => Carbon::now()->addMinutes($expire),
69+
]);
70+
}
71+
}
72+
73+
/**
74+
* Delete the specified "remember me" token for the given user.
75+
*
76+
* @param mixed $identifier
77+
* @param string $token
78+
* @return null
79+
*/
80+
public function deleteRememberToken($identifier, $token)
81+
{
82+
$model = $this->getModelByIdentifier($identifier);
83+
84+
if ($model && $token = $model->rememberTokens()->where('token', $token)->first()) {
85+
$token->delete();
86+
}
87+
}
88+
89+
/**
90+
* Purge old or expired "remember me" tokens.
91+
*
92+
* @param mixed $identifier
93+
* @param bool $expired
94+
* @return null
95+
*/
96+
public function purgeRememberTokens($identifier, $expired = false)
97+
{
98+
$model = $this->getModelByIdentifier($identifier);
99+
100+
if ($model) {
101+
$query = $model->rememberTokens();
102+
103+
if ($expired) {
104+
$query->where('expires_at', '<', Carbon::now());
105+
}
106+
107+
$query->delete();
108+
}
109+
}
110+
111+
/**
112+
* Gets the user based on their unique identifier.
113+
*
114+
* @param mixed $identifier
115+
* @return \Illuminate\Contracts\Auth\Authenticatable|null
116+
*/
117+
protected function getModelByIdentifier($identifier)
118+
{
119+
$model = $this->createModel();
120+
121+
return $model->where($model->getAuthIdentifierName(), $identifier)->first();
122+
}
123+
}

0 commit comments

Comments
 (0)