add nami guard
This commit is contained in:
parent
b6eb0eeb3a
commit
4a89f167f8
|
@ -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": {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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']);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue