diff --git a/src/AuthService.php b/src/AuthService.php new file mode 100644 index 0000000..a232ad6 --- /dev/null +++ b/src/AuthService.php @@ -0,0 +1,38 @@ +mumtazSSOService = new MumtazSSOService(); + } + + /** + * @param array $credentials + * @return mixed + * @throws InvalidClientCredentials + * @throws InvalidRetryGenerateException + * @throws SSODriverException + * @throws UnknownErrorHandlerException + */ + public function authenticate(array $credentials): mixed + { + return $this->mumtazSSOService->setAuthorizationToken() + ->getResponse(function (MumtazSSOService $service) use ($credentials) { + return Http::withHeaders($service->getHeaders())->post($service->getBaseUrl() . "/api/v1/auth", [ + "username" => $credentials["username"], + "password" => $credentials["password"], + "institution_id" => $credentials["institution_id"], //todo : hardcode for integration testing + ]); + }); + } +} diff --git a/src/Exceptions/SSODriverException.php b/src/Exceptions/SSODriverException.php index e5eb7e8..b146a7c 100644 --- a/src/Exceptions/SSODriverException.php +++ b/src/Exceptions/SSODriverException.php @@ -8,14 +8,20 @@ class SSODriverException extends Exception { - public array|null $data; + public array|null $errorResponse; + public function __construct(string $message = "Something went wrong !", - ?array $data = [], + ?array $errorResponse = [], int $code = Response::HTTP_UNAUTHORIZED, ?Throwable $previous = null ) { - $this->data = $data; + $this->errorResponse = $errorResponse; parent::__construct($message, $code, $previous); } + + public function getErrorResponse():?array + { + return $this->errorResponse; + } } diff --git a/src/MumtazSSOService.php b/src/MumtazSSOService.php index 529c345..c0f1701 100644 --- a/src/MumtazSSOService.php +++ b/src/MumtazSSOService.php @@ -116,7 +116,10 @@ public function getResponse(callable $request, callable $customErrorHandler = nu continue; } - $customErrorHandler($this, $response); + + if ($customErrorHandler){ + $customErrorHandler($this, $response); + } self::mappingErrorHandler($response->json()); //for default response } diff --git a/src/Traits/SSOServiceErrorHandler.php b/src/Traits/SSOServiceErrorHandler.php index 2dd4a47..393b6dd 100644 --- a/src/Traits/SSOServiceErrorHandler.php +++ b/src/Traits/SSOServiceErrorHandler.php @@ -32,6 +32,6 @@ public static function unknownResponseErrorHandler(?array $errorResponse): void */ public static function mappingErrorHandler(?array $errorResponse): void { - throw new SSODriverException("Something went wrong", $errorResponse); + throw new SSODriverException($errorResponse["message"], $errorResponse); } }