diff --git a/application/Espo/Core/Api/Auth.php b/application/Espo/Core/Api/Auth.php index f329c28a86..9266725163 100644 --- a/application/Espo/Core/Api/Auth.php +++ b/application/Espo/Core/Api/Auth.php @@ -197,6 +197,7 @@ class Auth 'message' => $result->getMessage(), 'view' => $result->getView(), 'token' => $result->getToken(), + 'data' => $result->getData(), ]; $response->writeBody(json_encode($bodyData)); @@ -254,6 +255,7 @@ class Auth if ($request->hasHeader('X-Hmac-Authorization')) { return 'Hmac'; } + if ($request->hasHeader('X-Api-Key')) { return 'ApiKey'; } @@ -276,7 +278,8 @@ class Auth } if ( - $request->getServerParam('PHP_AUTH_USER') && $request->getServerParam('PHP_AUTH_PW') + $request->getServerParam('PHP_AUTH_USER') && + $request->getServerParam('PHP_AUTH_PW') ) { $username = $request->getServerParam('PHP_AUTH_USER'); $password = $request->getServerParam('PHP_AUTH_PW'); @@ -285,7 +288,8 @@ class Auth } if ( - $request->getCookieParam('auth-username') && $request->getCookieParam('auth-token') + $request->getCookieParam('auth-username') && + $request->getCookieParam('auth-token') ) { $username = $request->getCookieParam('auth-username'); diff --git a/application/Espo/Core/Authentication/Result.php b/application/Espo/Core/Authentication/Result.php index f917a3a871..32c6a8b11b 100644 --- a/application/Espo/Core/Authentication/Result.php +++ b/application/Espo/Core/Authentication/Result.php @@ -31,6 +31,8 @@ namespace Espo\Core\Authentication; use Espo\Entities\User; +use stdClass; + /** * An authentication result. */ @@ -56,11 +58,15 @@ class Result private $failReason = null; + private $data = null; + private function __construct(string $status, ?User $user = null, ?ResultData $data = null) { $this->user = $user; $this->status = $status; + $this->data = $data; + if ($data) { $this->message = $data->getMessage(); $this->token = $data->getToken(); @@ -171,6 +177,14 @@ class Result return $this->token; } + /** + * Additional data that can be needed to a second step. + */ + public function getData(): ?stdClass + { + return $this->data ? $this->data->getData() : null; + } + /** * A fail reason. */ diff --git a/application/Espo/Core/Authentication/ResultData.php b/application/Espo/Core/Authentication/ResultData.php index f3ea06182b..3fc5100f8f 100644 --- a/application/Espo/Core/Authentication/ResultData.php +++ b/application/Espo/Core/Authentication/ResultData.php @@ -31,6 +31,8 @@ namespace Espo\Core\Authentication; use Espo\Entities\User; +use stdClass; + class ResultData { private $message = null; @@ -43,6 +45,8 @@ class ResultData private $failReason = null; + private $data = []; + private function __construct( ?string $message = null, ?string $failReason = null, @@ -72,7 +76,7 @@ class ResultData return new self($message); } - public static function fromArray(array $data): self + /*public static function fromArray(array $data): self { return new self( $data['message'] ?? null, @@ -81,7 +85,7 @@ class ResultData $data['view'] ?? null, $data['loggedUser'] ?? null ); - } + }*/ public function getLoggedUser(): ?User { @@ -113,10 +117,14 @@ class ResultData return $this->failReason; } + public function getData(): stdClass + { + return (object) $this->data; + } + public function withMessage(?string $message): self { $obj = clone $this; - $obj->message = $message; return $obj; @@ -125,7 +133,6 @@ class ResultData public function withFailReason(?string $failReason): self { $obj = clone $this; - $obj->failReason = $failReason; return $obj; @@ -134,7 +141,6 @@ class ResultData public function withToken(?string $token): self { $obj = clone $this; - $obj->token = $token; return $obj; @@ -143,7 +149,6 @@ class ResultData public function withView(?string $view): self { $obj = clone $this; - $obj->view = $view; return $obj; @@ -152,9 +157,19 @@ class ResultData public function withLoggedUser(?User $loggedUser): self { $obj = clone $this; - $obj->loggedUser = $loggedUser; return $obj; } + + /** + * @param mixed $value + */ + public function withDataItem(string $name, $value): self + { + $obj = clone $this; + $obj->data[$name] = $value; + + return $obj; + } } diff --git a/tests/unit/Espo/Core/Authentication/ResultDataTest.php b/tests/unit/Espo/Core/Authentication/ResultDataTest.php new file mode 100644 index 0000000000..967e3bcad0 --- /dev/null +++ b/tests/unit/Espo/Core/Authentication/ResultDataTest.php @@ -0,0 +1,53 @@ +withMessage('hello') + ->withDataItem('test1', '1') + ->withDataItem('test2', '2'); + + $this->assertEquals( + (object) [ + 'test1' => '1', + 'test2' => '2', + ], + $data->getData() + ); + + $this->assertEquals('hello', $data->getMessage()); + } +}