Skip to content

Commit

Permalink
Merge pull request #2 from classid/feature/authenticate-service
Browse files Browse the repository at this point in the history
authenticate service for login
  • Loading branch information
iqbalatma authored Oct 3, 2023
2 parents 38be4b2 + e44c3fe commit 8a64e1d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
38 changes: 38 additions & 0 deletions src/AuthService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Classid\SsoDriver;

use Classid\SsoDriver\Exceptions\InvalidClientCredentials;
use Classid\SsoDriver\Exceptions\InvalidRetryGenerateException;
use Classid\SsoDriver\Exceptions\SSODriverException;
use Classid\SsoDriver\Exceptions\UnknownErrorHandlerException;
use Illuminate\Support\Facades\Http;

class AuthService
{
public MumtazSSOService $mumtazSSOService;
public function __construct()
{
$this->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
]);
});
}
}
12 changes: 9 additions & 3 deletions src/Exceptions/SSODriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 4 additions & 1 deletion src/MumtazSSOService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/SSOServiceErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 8a64e1d

Please sign in to comment.