From b6eb0eeb3a992fb6e803b3414de1d0728c69c2a0 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 18 Jun 2021 19:29:21 +0200 Subject: [PATCH] Add nami guard --- src/AuthenticatesNamiUsers.php | 6 -- src/Authentication/NamiGuard.php | 81 +++++++++++++++++++++++++++ src/Backend/FakeBackend.php | 18 +++++- src/NamiUser.php | 20 +------ src/Providers/NamiServiceProvider.php | 5 +- src/Providers/NamiUserProvider.php | 57 ------------------- 6 files changed, 102 insertions(+), 85 deletions(-) create mode 100644 src/Authentication/NamiGuard.php delete mode 100644 src/Providers/NamiUserProvider.php diff --git a/src/AuthenticatesNamiUsers.php b/src/AuthenticatesNamiUsers.php index b753065..f826da6 100644 --- a/src/AuthenticatesNamiUsers.php +++ b/src/AuthenticatesNamiUsers.php @@ -14,7 +14,6 @@ trait AuthenticatesNamiUsers { $request->validate([ $this->username() => 'required|numeric', 'password' => 'required|string', - 'groupid' => 'required|numeric' ]); } @@ -23,9 +22,4 @@ trait AuthenticatesNamiUsers { return 'mglnr'; } - protected function credentials(Request $request) - { - return $request->only($this->username(), 'password', 'groupid'); - } - } diff --git a/src/Authentication/NamiGuard.php b/src/Authentication/NamiGuard.php new file mode 100644 index 0000000..f53250d --- /dev/null +++ b/src/Authentication/NamiGuard.php @@ -0,0 +1,81 @@ + NamiUser::fromCredentials($credentials), + 'cookie' => $api->cookie->toArray(), + 'credentials' => $credentials + ]); + + return true; + } + +} diff --git a/src/Backend/FakeBackend.php b/src/Backend/FakeBackend.php index 5c919a3..325f0ed 100644 --- a/src/Backend/FakeBackend.php +++ b/src/Backend/FakeBackend.php @@ -28,6 +28,10 @@ class FakeBackend { } public function put($url, $data) { + if (is_null($this->cookie->getCookieByName('JSESSIONID'))) { + return $this->notAuthorizedResponse(); + } + if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/([0-9]+)|', $url, $matches)) { list($url, $groupId, $memberId) = $matches; $existing = $this->members->search(function($m) use ($groupId, $memberId) { @@ -37,7 +41,9 @@ class FakeBackend { $this->members[$existing] = $data; } - return; + return $this->response([ + 'id' => $memberId + ]); } $this->urlNotFoundException($url); @@ -135,6 +141,16 @@ class FakeBackend { ]); } + private function notAuthorizedResponse() { + return $this->response([ + 'success' => true, + 'data' => null, + 'responseType' => 'ERROR', + 'message' => 'Session expired', + 'title' => 'Exception', + ]); + } + public function response($data) { return new Response(new GuzzleResponse(200, [], json_encode($data))); } diff --git a/src/NamiUser.php b/src/NamiUser.php index 6176f57..0218a50 100644 --- a/src/NamiUser.php +++ b/src/NamiUser.php @@ -6,15 +6,11 @@ use Illuminate\Contracts\Auth\Authenticatable; class NamiUser implements Authenticatable { - private $mglnr; - private $groupid; - public $name = 'DDD'; - public $email = 'III'; + public $mglnr; public static function fromCredentials(array $credentials): ?self { $user = new static(); $user->mglnr = $credentials['mglnr']; - $user->groupid = $credentials['groupid']; return $user; } @@ -23,24 +19,10 @@ class NamiUser implements Authenticatable { return $this->attemptNamiLogin(cache('member.'.$this->mglnr)['credentials']['password']); } - public function attemptNamiLogin($password) { - return Nami::login($this->mglnr, $password, $this->groupid); - } - public function getNamiGroupId() { return $this->groupid; } - public static function fromId($id) { - list($mglnr, $groupid) = explode('-', $id); - - $user = new static(); - $user->mglnr = $mglnr; - $user->groupid = $groupid; - - return $user; - } - public function getAuthIdentifierName() { return 'mglnr'; } diff --git a/src/Providers/NamiServiceProvider.php b/src/Providers/NamiServiceProvider.php index e935f1f..3fd3537 100644 --- a/src/Providers/NamiServiceProvider.php +++ b/src/Providers/NamiServiceProvider.php @@ -7,13 +7,14 @@ use GuzzleHttp\Client as GuzzleClient; use Illuminate\Support\ServiceProvider; use Zoomyboy\LaravelNami\Backend\LiveBackend; use Zoomyboy\LaravelNami\Api; +use Zoomyboy\LaravelNami\Authentication\NamiGuard; class NamiServiceProvider extends ServiceProvider { public function boot() { - Auth::provider('nami', function ($app, array $config) { - return new NamiUserProvider($config['model']); + Auth::extend('nami', function ($app, $name, array $config) { + return new NamiGuard($config); }); } diff --git a/src/Providers/NamiUserProvider.php b/src/Providers/NamiUserProvider.php deleted file mode 100644 index a1f836f..0000000 --- a/src/Providers/NamiUserProvider.php +++ /dev/null @@ -1,57 +0,0 @@ -model = $model; - } - - public function retrieveById($identifier) { - return $this->model::fromId($identifier); - } - - public function retrieveByToken($identifier, $token) { - - } - - public function updateRememberToken(Authenticatable $user, $token) { - - } - - public function retrieveByCredentials(array $credentials) { - return $this->model::fromCredentials($credentials); - } - - public function validateCredentials(Authenticatable $user, array $credentials) { - try { - $api = $user->attemptNamiLogin($credentials['password']); - - $group = $api->group($credentials['groupid']); - $data = $group->memberOverview()->first(function($member) use ($credentials) { - return $member->mitgliedsnr == $credentials['mglnr']; - }); - - if (!$data) { - return false; - } - - Cache::forever('namicookie-'.$credentials['mglnr'], [ - 'data' => $data->toArray(), - 'cookie' => $api->cookie->toArray(), - 'credentials' => $credentials - ]); - - return true; - } catch (NamiException $e) { - return false; - } - } -}