Fake nami login

This commit is contained in:
philipp lang 2021-06-13 15:43:49 +02:00
parent b3368fbd34
commit b390c3fb44
4 changed files with 112 additions and 8 deletions

View File

@ -42,17 +42,17 @@ class Api {
$password = $password ?: config('nami.auth.password'); $password = $password ?: config('nami.auth.password');
$groupid = $groupid ?: config('nami.auth.groupid'); $groupid = $groupid ?: config('nami.auth.groupid');
Http::withOptions(['cookies' => $this->cookie])->get(self::$url.'/ica/pages/login.jsp'); $this->http()->get(self::$url.'/ica/pages/login.jsp');
$response = Http::asForm()->withOptions(['cookies' => $this->cookie])->post(self::$url.'/ica/rest/nami/auth/manual/sessionStartup', [ $response = $this->http()->asForm()->post(self::$url.'/ica/rest/nami/auth/manual/sessionStartup', [
'Login' => 'API', 'Login' => 'API',
'redirectTo' => './app.jsp', 'redirectTo' => './app.jsp',
'username' => $mglnr, 'username' => $mglnr,
'password' => $password 'password' => $password
])->json(); ]);
if ($response['statusCode'] !== 0) { if ($response->json()['statusCode'] !== 0) {
$e = new LoginException(); $e = new LoginException();
$e->setResponse($response); $e->setResponse($response->json());
throw $e; throw $e;
} }

View File

@ -5,10 +5,14 @@ namespace Zoomyboy\LaravelNami\Backend;
use Illuminate\Http\Client\Response; use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use GuzzleHttp\Psr7\Response as GuzzleResponse; use GuzzleHttp\Psr7\Response as GuzzleResponse;
use GuzzleHttp\Cookie\SetCookie;
class FakeBackend { class FakeBackend {
private $members; private $members;
private $passwords;
public $groups = [];
public $loggedInAs = null;
public function __construct() { public function __construct() {
$this->members = collect([]); $this->members = collect([]);
@ -19,6 +23,7 @@ class FakeBackend {
} }
public function cookie($cookie) { public function cookie($cookie) {
$this->cookie = $cookie;
return $this; return $this;
} }
@ -35,10 +40,30 @@ class FakeBackend {
return; return;
} }
throw new \Exception('no handler found for URL '.$url); $this->urlNotFoundException($url);
} }
public function get($url) { public function get($url) {
if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/flist|', $url, $matches)) {
list($url, $groupId) = $matches;
$members = $this->members->filter(function($m) use ($groupId) {
return $m['gruppierungId'] == $groupId;
})->map(function($member) {
return [
"entries_id" => $member['id'],
"id" => $member['id'],
"entries_mitgliedsNummer" => $member['id'],
];
});
return $this->response([
"success" => true,
'data' => $members,
"responseType" => "OK",
"metaData" => []
]);
}
if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/([0-9]+)|', $url, $matches)) { if (preg_match('|/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/([0-9]+)/([0-9]+)|', $url, $matches)) {
list($url, $groupId, $memberId) = $matches; list($url, $groupId, $memberId) = $matches;
@ -51,6 +76,80 @@ class FakeBackend {
'data' => $member 'data' => $member
]))); ])));
} }
if ($url === 'https://nami.dpsg.de/ica/pages/login.jsp') {
return;
}
if ($url === 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root') {
$groups = collect(data_get($this->groups, $this->loggedInAs))->map(function($group) {
return [
"descriptor" => "Solingen-Wald, Silva 100105",
"name" => "",
"representedClass" => "de.iconcept.nami.entity.org.Gruppierung",
"id" => $group
];
})->toArray();
return $this->response([
"success" => true,
"data" => $groups,
"responseType" => "OK"
]);
}
$this->urlNotFoundException($url);
}
public function post($url, $data) {
if ($url === 'https://nami.dpsg.de/ica/rest/nami/auth/manual/sessionStartup') {
if (!data_get($data, 'username') || !data_get($data, 'password')) {
return $this->wrongLoginResponse();
}
if ($this->passwords[data_get($data, 'username')] === data_get($data, 'password')) {
$this->loggedInAs = data_get($data, 'username');
$this->cookie->setCookie(tap(SetCookie::fromString('JSESSIONID=rZMBv1McDAJ-KukQ6BboJBTq.srv-nami06; path=/ica'), function($cookie) {
$cookie->setDomain('nami.dpsg.de');
}));
return $this->response([
"statusCode" => 0,
]);
}
}
$this->urlNotFoundException($url);
}
private function wrongLoginResponse() {
return $this->response([
"servicePrefix" => null,
"methodCall" => null,
"response" => null,
"statusCode" => 3000,
"statusMessage" => "Benutzer nicht gefunden oder Passwort falsch.",
"apiSessionName" => "JSESSIONID",
"apiSessionToken" => "qrjlt_YFVhtRPU-epc-58AB1",
"minorNumber" => 0,
"majorNumber" => 0,
]);
}
public function response($data) {
return new Response(new GuzzleResponse(200, [], json_encode($data)));
}
public function fakeNamiPassword($mglnr, $password, $groups) {
$this->passwords[$mglnr] = $password;
$this->groups[$mglnr] = $groups;
}
public function asForm() {
return $this;
}
public function urlNotFoundException($url) {
throw new \Exception('no handler found for URL '.$url);
} }
} }

View File

@ -25,4 +25,8 @@ trait FakesNami {
} }
} }
public function fakeNamiPassword($mglnr, $password, $groups) {
Backend::fakeNamiPassword($mglnr, $password, $groups);
}
} }

View File

@ -43,8 +43,9 @@ class NamiUserProvider implements UserProvider {
return false; return false;
} }
Cache::forever('member.'.$credentials['mglnr'], [ Cache::forever('namicookie-'.$credentials['mglnr'], [
'data' => $data, 'data' => $data->toArray(),
'cookie' => $api->cookie->toArray(),
'credentials' => $credentials 'credentials' => $credentials
]); ]);