add nami guard

This commit is contained in:
philipp lang 2021-06-18 23:26:21 +02:00
parent b6eb0eeb3a
commit 4a89f167f8
4 changed files with 66 additions and 58 deletions

View File

@ -8,7 +8,9 @@
"email": "philipp@zoomyboy.de" "email": "philipp@zoomyboy.de"
} }
], ],
"require": {}, "require": {
"guzzlehttp/guzzle": "^6.3.1|^7.0"
},
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"Zoomyboy\\LaravelNami\\": "src/" "Zoomyboy\\LaravelNami\\": "src/"
@ -20,8 +22,7 @@
} }
}, },
"require-dev": { "require-dev": {
"orchestra/testbench": "^5.3", "orchestra/testbench": "^6.0"
"guzzlehttp/guzzle": "^6.3.1|^7.0"
}, },
"extra": { "extra": {
"laravel": { "laravel": {

View File

@ -7,53 +7,23 @@ use Illuminate\Contracts\Auth\Authenticatable;
use Zoomyboy\LaravelNami\Nami; use Zoomyboy\LaravelNami\Nami;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Zoomyboy\LaravelNami\NamiUser; 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 {
/** use GuardHelpers;
* Determine if the current user is authenticated.
*
* @return bool
*/
public function check() {
} protected $cache;
protected $user;
/** protected $session;
* 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 = []) {
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 * @param \Illuminate\Contracts\Auth\Authenticatable $user
* @return void * @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']); $api = Nami::login($credentials['mglnr'], $credentials['password']);
Cache::forever('namicookie-'.$credentials['mglnr'], [ $payload = [
'user' => NamiUser::fromCredentials($credentials),
'cookie' => $api->cookie->toArray(), 'cookie' => $api->cookie->toArray(),
'credentials' => $credentials 'credentials' => $credentials
]); ];
$this->setUser(new NamiUser($payload));
$key = $this->newCacheKey();
Cache::forever("namiauth-{$key}", $payload);
$this->updateSession($key);
return true; 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);
}
} }

View File

@ -7,12 +7,12 @@ use Illuminate\Contracts\Auth\Authenticatable;
class NamiUser implements Authenticatable { class NamiUser implements Authenticatable {
public $mglnr; public $mglnr;
public $password;
public static function fromCredentials(array $credentials): ?self { public function __construct($payload) {
$user = new static(); $this->mglnr = data_get($payload, 'credentials.mglnr');
$user->mglnr = $credentials['mglnr']; $this->password = data_get($payload, 'credentials.password');
$this->cookie = data_get($payload, 'cookie');
return $user;
} }
public function getNamiApi() { public function getNamiApi() {

View File

@ -14,7 +14,7 @@ class NamiServiceProvider extends ServiceProvider
public function boot() public function boot()
{ {
Auth::extend('nami', function ($app, $name, array $config) { 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']);
}); });
} }