Catch Response if authentication failed

This commit is contained in:
philipp lang 2021-11-17 00:20:58 +01:00
parent 5472f4eb18
commit d666f78714
1 changed files with 51 additions and 22 deletions

View File

@ -4,11 +4,14 @@ namespace Zoomyboy\LaravelNami\Authentication;
use Illuminate\Auth\GuardHelpers; use Illuminate\Auth\GuardHelpers;
use Illuminate\Auth\SessionGuard; use Illuminate\Auth\SessionGuard;
use Illuminate\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\Guard;
use Illuminate\Session\Store as SessionStore;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Zoomyboy\LaravelNami\LoginException;
use Zoomyboy\LaravelNami\Nami; use Zoomyboy\LaravelNami\Nami;
use Zoomyboy\LaravelNami\NamiUser; use Zoomyboy\LaravelNami\NamiUser;
@ -16,11 +19,18 @@ class NamiGuard {
use GuardHelpers; use GuardHelpers;
protected $cache; protected CacheRepository $cache;
protected $user;
protected $session;
public function __construct($session, $cache) { /**
* The currently authenticated user.
*
* @var ?NamiUser
*/
protected $user;
protected SessionStore $session;
public function __construct(SessionStore $session, CacheRepository $cache) {
$this->session = $session; $this->session = $session;
$this->cache = $cache; $this->cache = $cache;
} }
@ -28,14 +38,15 @@ class NamiGuard {
/** /**
* Set the current user. * Set the current user.
* *
* @param \Illuminate\Contracts\Auth\Authenticatable $user * @param NamiUser|null $user
* @return void * @return void
*/ */
public function setUser(NamiUser $user) { public function setUser(?NamiUser $user): void
{
$this->user = $user; $this->user = $user;
} }
public function user() public function user(): ?NamiUser
{ {
if (! is_null($this->user)) { if (! is_null($this->user)) {
return $this->user; return $this->user;
@ -44,42 +55,60 @@ class NamiGuard {
$cache = $this->resolveCache(); $cache = $this->resolveCache();
if (!$cache) { if (!$cache) {
return; return null;
} }
return NamiUser::fromPayload($cache); return NamiUser::fromPayload($cache);
} }
public function attempt(array $credentials = [], $remember = false) { /**
$api = Nami::login($credentials['mglnr'], $credentials['password']); * @param array<string, string> $credentials
* @param bool $remember
*/
public function attempt(array $credentials = [], bool $remember = false): bool
{
try {
$api = Nami::login($credentials['mglnr'], $credentials['password']);
$payload = [ $payload = [
'credentials' => $credentials 'credentials' => $credentials
]; ];
$this->setUser(NamiUser::fromPayload($payload)); $this->setUser(NamiUser::fromPayload($payload));
$key = $this->newCacheKey(); $key = $this->newCacheKey();
Cache::forever("namiauth-{$key}", $payload); Cache::forever("namiauth-{$key}", $payload);
$this->updateSession($key); $this->updateSession($key);
return true; return true;
} catch (LoginException $e) {
return false;
}
} }
protected function updateSession($data) protected function updateSession(string $data): void
{ {
$this->session->put($this->getName(), $data); $this->session->put($this->getName(), $data);
$this->session->migrate(true); $this->session->migrate(true);
} }
public function getName() { public function getName(): string
{
return 'auth_key'; return 'auth_key';
} }
private function resolveCache() { public function logout(): void
{
$this->session->forget($this->getName());
$this->setUser(null);
}
private function resolveCache(): ?string
{
return $this->cache->get('namiauth-'.$this->session->get($this->getName())); return $this->cache->get('namiauth-'.$this->session->get($this->getName()));
} }
private function newCacheKey() { private function newCacheKey(): string
{
return Str::random(16); return Str::random(16);
} }