-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added storage handlers, remove unused code, added listeners
- Loading branch information
1 parent
9ccdf66
commit 99445c8
Showing
17 changed files
with
247 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia\Listeners; | ||
|
||
use Dentro\Paranoia\Paranoia; | ||
use Illuminate\Auth\Events\Login; | ||
|
||
class StoreSessionIP | ||
{ | ||
public function handle(Login $event): void | ||
{ | ||
/** @var Paranoia $driver */ | ||
$driver = app('paranoia'); | ||
if ($driver->eligibleForIPRestriction()) { | ||
$driver->saveSessionIpAddress(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia\Listeners; | ||
|
||
use Dentro\Paranoia\Paranoia; | ||
use Illuminate\Auth\Events\Login; | ||
|
||
class StoreSessionUserAgent | ||
{ | ||
public function handle(Login $event): void | ||
{ | ||
/** @var Paranoia $driver */ | ||
$driver = app('paranoia'); | ||
if ($driver->eligibleForUserAgentRestriction()) { | ||
$driver->saveSessionUserAgent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia\Middlewares; | ||
|
||
use Illuminate\Http\Request; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
use Dentro\Paranoia\Storage\Contracts\SessionStorageContract; | ||
use Dentro\Paranoia\Storage\SessionStorageHandler; | ||
|
||
class Paranoia | ||
{ | ||
protected function getSessionTable(): string | ||
public function __construct( | ||
protected SessionStorageContract $storage | ||
) {} | ||
|
||
public function eligibleForGeoRestriction(): bool | ||
{ | ||
return auth()->guard()->check(); | ||
} | ||
|
||
public function eligibleForIPRestriction(): bool | ||
{ | ||
/** @var string */ | ||
return config('session.table', 'sessions'); | ||
return auth()->guard()->check(); | ||
} | ||
|
||
public function shouldCheckGeoRestriction(): bool | ||
public function eligibleForUserAgentRestriction(): bool | ||
{ | ||
return auth()->guard()->check(); | ||
} | ||
|
||
public function shouldCheckIPRestriction(): bool | ||
public function isUsingBaseDriver(): bool | ||
{ | ||
return $this->storage instanceof SessionStorageHandler; | ||
} | ||
|
||
public function saveSessionIpAddress(): void | ||
{ | ||
return session()->getDefaultDriver() === 'database' && $this->getSessionTable() !== null && auth()->guard()->check(); | ||
$this->storage->saveSessionIpAddress(session()->id()); | ||
} | ||
|
||
public function shouldCheckUserAgentRestriction(): bool | ||
public function saveSessionUserAgent(): void | ||
{ | ||
return session()->getDefaultDriver() === 'database' && $this->getSessionTable() !== null && auth()->guard()->check(); | ||
$this->storage->saveSessionUserAgent(session()->id()); | ||
} | ||
|
||
public function getSessionIpAddress(): ?string | ||
{ | ||
/** @var string|null */ | ||
return DB::table($this->getSessionTable()) | ||
->where('id', session()->getId()) | ||
->value('ip_address'); | ||
return $this->storage->getSavedIpAddress(session()->id()); | ||
} | ||
|
||
public function getSessionUserAgent(): ?string | ||
{ | ||
/** @var string|null */ | ||
return DB::table($this->getSessionTable()) | ||
->where('id', session()->getId()) | ||
->value('user_agent'); | ||
return $this->storage->getSavedUserAgent(session()->id()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia\Storage\Contracts; | ||
|
||
interface SessionStorageContract | ||
{ | ||
public function saveSessionIpAddress(string $sessionId): void; | ||
|
||
public function saveSessionUserAgent(string $sessionId): void; | ||
|
||
public function getSavedIpAddress(string $sessionId): ?string; | ||
|
||
public function getSavedUserAgent(string $sessionId): ?string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia\Storage; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
class SessionStorageDatabaseHandler implements Contracts\SessionStorageContract | ||
{ | ||
public function __construct( | ||
protected string $sessionTableName | ||
) {} | ||
|
||
public function saveSessionIpAddress(string $sessionId): void | ||
{ | ||
// already handled by laravel's session handler | ||
} | ||
|
||
public function saveSessionUserAgent(string $sessionId): void | ||
{ | ||
// already handled by laravel's session handler | ||
} | ||
|
||
public function getSavedIpAddress(string $sessionId): ?string | ||
{ | ||
/** @var string|null */ | ||
return DB::table($this->sessionTableName) | ||
->where('id', $sessionId) | ||
->value('ip_address'); | ||
} | ||
|
||
public function getSavedUserAgent(string $sessionId): ?string | ||
{ | ||
/** @var string|null */ | ||
return DB::table($this->sessionTableName) | ||
->where('id', $sessionId) | ||
->value('user_agent'); | ||
} | ||
|
||
/** | ||
* @throws \Throwable | ||
*/ | ||
public static function make(?string $tableName): SessionStorageDatabaseHandler | ||
{ | ||
throw_if($tableName === null || $tableName === '' || $tableName === '0', new \InvalidArgumentException('Session table name is required for database driver')); | ||
|
||
/** @var string $tableName */ | ||
return new self($tableName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia\Storage; | ||
|
||
class SessionStorageFactory | ||
{ | ||
protected string $sessionTableName = 'sessions'; | ||
|
||
public function __construct(protected string $driver) {} | ||
|
||
/** | ||
* @throws \Throwable | ||
*/ | ||
public function build(): Contracts\SessionStorageContract | ||
{ | ||
return match ($this->driver) { | ||
'database' => SessionStorageDatabaseHandler::make($this->sessionTableName), | ||
default => SessionStorageHandler::make(), | ||
}; | ||
} | ||
|
||
public function setSessionTableName(string $sessionTableName): static | ||
{ | ||
$this->sessionTableName = $sessionTableName; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dentro\Paranoia\Storage; | ||
|
||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
class SessionStorageHandler implements Contracts\SessionStorageContract | ||
{ | ||
public function saveSessionIpAddress(string $sessionId): void | ||
{ | ||
session()->put('ip_address', request()->ip()); | ||
} | ||
|
||
public function saveSessionUserAgent(string $sessionId): void | ||
{ | ||
session()->put('user_agent', request()->ip()); | ||
} | ||
|
||
/** | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
*/ | ||
public function getSavedIpAddress(string $sessionId): ?string | ||
{ | ||
return session()->get('ip_address') ?? null; | ||
} | ||
|
||
/** | ||
* @throws ContainerExceptionInterface | ||
* @throws NotFoundExceptionInterface | ||
*/ | ||
public function getSavedUserAgent(string $sessionId): ?string | ||
{ | ||
return session()->get('user_agent') ?? null; | ||
} | ||
|
||
/** | ||
* @throws \Throwable | ||
*/ | ||
public static function make(): SessionStorageHandler | ||
{ | ||
return new self; | ||
} | ||
} |
Oops, something went wrong.