Skip to content

Commit be4cccb

Browse files
chore: refactor responses (#18)
* chore: refactor responses * chore: update test * chore: update readme
1 parent bcb93c7 commit be4cccb

11 files changed

+115
-151
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ The package provides a pre-configured login route for OpenID Connect authenticat
6060
To enable or disable the login route, you can update the `OIDC_LOGIN_ROUTE_ENABLED` variable in your environment configuration. Set it to true to enable the login route or false to disable it.
6161
To change the URL of the login route, you can update the `OIDC_LOGIN_ROUTE` variable in your environment configuration. The default value is `/oidc/login`.
6262

63-
### Customizing Login Response
64-
The package includes a default LoginResponse class that returns a JSON response containing user information. However, you have the flexibility to customize the login response according to your project's needs.
63+
### Customizing Login Response Handler
64+
The package includes a default LoginResponseHandler class that returns a JSON response containing user information. However, you have the flexibility to customize the login response according to your project's needs.
6565

66-
To bind your own implementation of the LoginResponseInterface, you can use the following code in your Laravel application:
66+
To bind your own implementation of the LoginResponseHandlerInterface, you can use the following code in your Laravel application:
6767
```php
68-
$this->app->bind(LoginResponseInterface::class, YourCustomLoginResponse::class);
68+
$this->app->bind(LoginResponseHandlerInterface::class, YourCustomLoginResponse::class);
6969
```
7070

7171
Replace `YourCustomLoginResponse` with the class name of your custom implementation. By binding your own response class, you can define the desired behavior and format of the login response.
7272

73-
Make sure to implement the `LoginResponseInterface` in your custom response class to ensure compatibility.
73+
Make sure to implement the `LoginResponseHandlerInterface` in your custom response handler class to ensure compatibility.
7474

7575
## Contributing
7676
If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the GitHub repository of this package.

src/Http/Controllers/Controller.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Http/Controllers/LoginController.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
namespace MinVWS\OpenIDConnectLaravel\Http\Controllers;
66

77
use Exception;
8-
use Illuminate\Contracts\Support\Responsable;
98
use Illuminate\Http\Request;
9+
use Illuminate\Routing\Controller;
1010
use Jumbojett\OpenIDConnectClientException;
11-
use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponseInterface;
11+
use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponseHandlerInterface;
1212
use MinVWS\OpenIDConnectLaravel\OpenIDConnectClient;
13-
use MinVWS\OpenIDConnectLaravel\Services\OpenIDConnectExceptionHandlerInterface;
13+
use MinVWS\OpenIDConnectLaravel\Services\ExceptionHandlerInterface;
14+
use Symfony\Component\HttpFoundation\Response;
1415

1516
class LoginController extends Controller
1617
{
1718
public function __construct(
1819
protected OpenIDConnectClient $client,
19-
protected OpenIDConnectExceptionHandlerInterface $exceptionHandler,
20+
protected ExceptionHandlerInterface $exceptionHandler,
21+
protected LoginResponseHandlerInterface $responseHandler,
2022
) {
2123
}
2224

23-
public function __invoke(Request $request): Responsable
25+
public function __invoke(Request $request): Response
2426
{
2527
// This redirects to the client and handles the redirect back
2628
try {
@@ -42,8 +44,8 @@ public function __invoke(Request $request): Responsable
4244
return $this->exceptionHandler->handleException($e);
4345
}
4446

45-
// Return the user information in a response
46-
return app(LoginResponseInterface::class, ['userInfo' => $userInfo]);
47+
// Return the user information to response handler
48+
return $this->responseHandler->handleLoginResponse($userInfo);
4749
}
4850

4951
/**

src/Http/Responses/LoginResponse.php

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MinVWS\OpenIDConnectLaravel\Http\Responses;
6+
7+
use Illuminate\Http\JsonResponse;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class LoginResponseHandler implements LoginResponseHandlerInterface
11+
{
12+
public function handleLoginResponse(object $userInfo): Response
13+
{
14+
return new JsonResponse([
15+
'userInfo' => $userInfo,
16+
]);
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MinVWS\OpenIDConnectLaravel\Http\Responses;
6+
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
interface LoginResponseHandlerInterface
10+
{
11+
public function handleLoginResponse(object $userInfo): Response;
12+
}

src/Http/Responses/LoginResponseInterface.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/OpenIDConnectServiceProvider.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
use Illuminate\Support\Facades\Route;
99
use Illuminate\Support\ServiceProvider;
1010
use Jose\Component\KeyManagement\JWKFactory;
11-
use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponse;
12-
use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponseInterface;
11+
use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponseHandler;
12+
use MinVWS\OpenIDConnectLaravel\Http\Responses\LoginResponseHandlerInterface;
1313
use MinVWS\OpenIDConnectLaravel\OpenIDConfiguration\OpenIDConfigurationLoader;
1414
use MinVWS\OpenIDConnectLaravel\Services\JWE\JweDecryptInterface;
1515
use MinVWS\OpenIDConnectLaravel\Services\JWE\JweDecryptService;
16-
use MinVWS\OpenIDConnectLaravel\Services\OpenIDConnectExceptionHandler;
17-
use MinVWS\OpenIDConnectLaravel\Services\OpenIDConnectExceptionHandlerInterface;
16+
use MinVWS\OpenIDConnectLaravel\Services\ExceptionHandler;
17+
use MinVWS\OpenIDConnectLaravel\Services\ExceptionHandlerInterface;
1818

1919
class OpenIDConnectServiceProvider extends ServiceProvider
2020
{
@@ -26,7 +26,7 @@ public function register(): void
2626
$this->registerConfigurationLoader();
2727
$this->registerClient();
2828
$this->registerExceptionHandler();
29-
$this->registerResponses();
29+
$this->registerResponseHandler();
3030
}
3131

3232
public function boot(): void
@@ -130,10 +130,10 @@ protected function registerJweDecryptInterface(): void
130130

131131
protected function registerExceptionHandler(): void
132132
{
133-
$this->app->bind(OpenIDConnectExceptionHandlerInterface::class, OpenIDConnectExceptionHandler::class);
133+
$this->app->bind(ExceptionHandlerInterface::class, ExceptionHandler::class);
134134
}
135-
protected function registerResponses(): void
135+
protected function registerResponseHandler(): void
136136
{
137-
$this->app->bind(LoginResponseInterface::class, LoginResponse::class);
137+
$this->app->bind(LoginResponseHandlerInterface::class, LoginResponseHandler::class);
138138
}
139139
}

src/Services/OpenIDConnectExceptionHandler.php renamed to src/Services/ExceptionHandler.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace MinVWS\OpenIDConnectLaravel\Services;
66

77
use Exception;
8-
use Illuminate\Contracts\Support\Responsable;
98
use Jumbojett\OpenIDConnectClientException;
9+
use Symfony\Component\HttpFoundation\Response;
1010

11-
class OpenIDConnectExceptionHandler implements OpenIDConnectExceptionHandlerInterface
11+
class ExceptionHandler implements ExceptionHandlerInterface
1212
{
13-
public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $exception): Responsable
13+
public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $exception): Response
1414
{
1515
if (str_starts_with($exception->getMessage(), 'Error: ')) {
1616
return $this->handleRequestError($exception);
@@ -26,14 +26,14 @@ public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $e
2626
/**
2727
* Called when request to userinfo endpoint fails, jwt signature is invalid, or userinfo is not an object.
2828
* @param OpenIDConnectClientException $exception
29-
* @return Responsable
29+
* @return Response
3030
*/
31-
public function handleExceptionWhileRequestUserInfo(OpenIDConnectClientException $exception): Responsable
31+
public function handleExceptionWhileRequestUserInfo(OpenIDConnectClientException $exception): Response
3232
{
3333
return $this->defaultResponse($exception);
3434
}
3535

36-
public function handleException(Exception $exception): Responsable
36+
public function handleException(Exception $exception): Response
3737
{
3838
return $this->defaultResponseGenericException($exception);
3939
}
@@ -42,34 +42,34 @@ public function handleException(Exception $exception): Responsable
4242
* Called when url contains query parameter error.
4343
* For example user is sent back from idp with error=login_cancelled.
4444
* @param OpenIDConnectClientException $exception
45-
* @return Responsable
45+
* @return Response
4646
*/
47-
protected function handleRequestError(OpenIDConnectClientException $exception): Responsable
47+
protected function handleRequestError(OpenIDConnectClientException $exception): Response
4848
{
4949
return $this->default400Response($exception);
5050
}
5151

5252
/**
5353
* Called when url contains query parameter code and state, and state does not match with the value from session.
5454
* @param OpenIDConnectClientException $exception
55-
* @return Responsable
55+
* @return Response
5656
*/
57-
protected function handleUnableToDetermineState(OpenIDConnectClientException $exception): Responsable
57+
protected function handleUnableToDetermineState(OpenIDConnectClientException $exception): Response
5858
{
5959
return $this->default400Response($exception);
6060
}
6161

62-
protected function defaultResponse(OpenIDConnectClientException $exception): Responsable
62+
protected function defaultResponse(OpenIDConnectClientException $exception): Response
6363
{
6464
abort(500, $exception->getMessage());
6565
}
6666

67-
protected function defaultResponseGenericException(Exception $exception): Responsable
67+
protected function defaultResponseGenericException(Exception $exception): Response
6868
{
6969
abort(500, $exception->getMessage());
7070
}
7171

72-
protected function default400Response(OpenIDConnectClientException $exception): Responsable
72+
protected function default400Response(OpenIDConnectClientException $exception): Response
7373
{
7474
abort(400, $exception->getMessage());
7575
}

src/Services/OpenIDConnectExceptionHandlerInterface.php renamed to src/Services/ExceptionHandlerInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace MinVWS\OpenIDConnectLaravel\Services;
66

77
use Exception;
8-
use Illuminate\Contracts\Support\Responsable;
98
use Jumbojett\OpenIDConnectClientException;
9+
use Symfony\Component\HttpFoundation\Response;
1010

11-
interface OpenIDConnectExceptionHandlerInterface
11+
interface ExceptionHandlerInterface
1212
{
13-
public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $exception): Responsable;
14-
public function handleExceptionWhileRequestUserInfo(OpenIDConnectClientException $exception): Responsable;
15-
public function handleException(Exception $exception): Responsable;
13+
public function handleExceptionWhileAuthenticate(OpenIDConnectClientException $exception): Response;
14+
public function handleExceptionWhileRequestUserInfo(OpenIDConnectClientException $exception): Response;
15+
public function handleException(Exception $exception): Response;
1616
}

0 commit comments

Comments
 (0)