Add nami guard
This commit is contained in:
parent
78860cb529
commit
b6eb0eeb3a
|
@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami\Authentication;
|
||||
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Zoomyboy\LaravelNami\Nami;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Zoomyboy\LaravelNami\NamiUser;
|
||||
|
||||
class NamiGuard implements Guard {
|
||||
|
||||
/**
|
||||
* Determine if the current user is authenticated.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = []) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the current user.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
||||
* @return void
|
||||
*/
|
||||
public function setUser(Authenticatable $user) {
|
||||
|
||||
}
|
||||
|
||||
public function attempt($credentials) {
|
||||
$api = Nami::login($credentials['mglnr'], $credentials['password']);
|
||||
|
||||
Cache::forever('namicookie-'.$credentials['mglnr'], [
|
||||
'user' => NamiUser::fromCredentials($credentials),
|
||||
'cookie' => $api->cookie->toArray(),
|
||||
'credentials' => $credentials
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -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)));
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami\Providers;
|
||||
|
||||
use Cache;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class NamiUserProvider implements UserProvider {
|
||||
|
||||
private $model;
|
||||
|
||||
public function __construct($model) {
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue