From 4a89f167f825dc7e58aaf7b426657531163d82cf Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 18 Jun 2021 23:26:21 +0200 Subject: [PATCH] add nami guard --- composer.json | 7 +- src/Authentication/NamiGuard.php | 105 ++++++++++++++------------ src/NamiUser.php | 10 +-- src/Providers/NamiServiceProvider.php | 2 +- 4 files changed, 66 insertions(+), 58 deletions(-) diff --git a/composer.json b/composer.json index c3e4737..fe6a6b7 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,9 @@ "email": "philipp@zoomyboy.de" } ], - "require": {}, + "require": { + "guzzlehttp/guzzle": "^6.3.1|^7.0" + }, "autoload": { "psr-4": { "Zoomyboy\\LaravelNami\\": "src/" @@ -20,8 +22,7 @@ } }, "require-dev": { - "orchestra/testbench": "^5.3", - "guzzlehttp/guzzle": "^6.3.1|^7.0" + "orchestra/testbench": "^6.0" }, "extra": { "laravel": { diff --git a/src/Authentication/NamiGuard.php b/src/Authentication/NamiGuard.php index f53250d..65051f2 100644 --- a/src/Authentication/NamiGuard.php +++ b/src/Authentication/NamiGuard.php @@ -7,53 +7,23 @@ use Illuminate\Contracts\Auth\Authenticatable; use Zoomyboy\LaravelNami\Nami; use Illuminate\Support\Facades\Cache; use Zoomyboy\LaravelNami\NamiUser; +use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; +use Illuminate\Auth\SessionGuard; +use Illuminate\Auth\GuardHelpers; +use Illuminate\Support\Str; -class NamiGuard implements Guard { +class NamiGuard { - /** - * Determine if the current user is authenticated. - * - * @return bool - */ - public function check() { + use GuardHelpers; - } - - /** - * Determine if the current user is a guest. - * - * @return bool - */ - public function guest() { - - } - - /** - * Get the currently authenticated user. - * - * @return \Illuminate\Contracts\Auth\Authenticatable|null - */ - public function user() { - - } - - /** - * Get the ID for the currently authenticated user. - * - * @return int|string|null - */ - public function id() { - - } - - /** - * Validate a user's credentials. - * - * @param array $credentials - * @return bool - */ - public function validate(array $credentials = []) { + protected $cache; + protected $user; + protected $session; + public function __construct($config, $session, $cache) { + $this->config = $config; + $this->session = $session; + $this->cache = $cache; } /** @@ -62,20 +32,57 @@ class NamiGuard implements Guard { * @param \Illuminate\Contracts\Auth\Authenticatable $user * @return void */ - public function setUser(Authenticatable $user) { - + public function setUser(NamiUser $user) { + $this->user = $user; } - public function attempt($credentials) { + public function user() + { + if (! is_null($this->user)) { + return $this->user; + } + + $cache = $this->resolveCache(); + + if (!$cache) { + return; + } + + return NamiUser::fromCredentials($cache['credentials']); + } + + public function attempt(array $credentials = [], $remember = false) { $api = Nami::login($credentials['mglnr'], $credentials['password']); - Cache::forever('namicookie-'.$credentials['mglnr'], [ - 'user' => NamiUser::fromCredentials($credentials), + $payload = [ 'cookie' => $api->cookie->toArray(), 'credentials' => $credentials - ]); + ]; + + $this->setUser(new NamiUser($payload)); + $key = $this->newCacheKey(); + Cache::forever("namiauth-{$key}", $payload); + $this->updateSession($key); return true; } + protected function updateSession($data) + { + $this->session->put($this->getName(), $data); + $this->session->migrate(true); + } + + public function getName() { + return 'auth_key'; + } + + private function resolveCache() { + return $this->cache->get($this->session->get($this->getName())); + } + + private function newCacheKey() { + return Str::random(16); + } + } diff --git a/src/NamiUser.php b/src/NamiUser.php index 0218a50..1315e4c 100644 --- a/src/NamiUser.php +++ b/src/NamiUser.php @@ -7,12 +7,12 @@ use Illuminate\Contracts\Auth\Authenticatable; class NamiUser implements Authenticatable { public $mglnr; + public $password; - public static function fromCredentials(array $credentials): ?self { - $user = new static(); - $user->mglnr = $credentials['mglnr']; - - return $user; + public function __construct($payload) { + $this->mglnr = data_get($payload, 'credentials.mglnr'); + $this->password = data_get($payload, 'credentials.password'); + $this->cookie = data_get($payload, 'cookie'); } public function getNamiApi() { diff --git a/src/Providers/NamiServiceProvider.php b/src/Providers/NamiServiceProvider.php index 3fd3537..bbcfe9b 100644 --- a/src/Providers/NamiServiceProvider.php +++ b/src/Providers/NamiServiceProvider.php @@ -14,7 +14,7 @@ class NamiServiceProvider extends ServiceProvider public function boot() { Auth::extend('nami', function ($app, $name, array $config) { - return new NamiGuard($config); + return new NamiGuard($config['beforeDriver'], $this->app['session.store'], $this->app['cache.store']); }); }