2FAuth/app/Exceptions/Handler.php

105 lines
3.5 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
class Handler extends ExceptionHandler
{
2022-11-14 17:10:47 +01:00
/**
2023-08-01 16:33:55 +02:00
* The list of the inputs that are never flashed to the session on validation exceptions.
2019-05-20 07:37:41 +02:00
*
* @var array<int, string>
2019-05-20 07:37:41 +02:00
*/
protected $dontFlash = [
2021-12-02 13:15:53 +01:00
'current_password',
2019-05-20 07:37:41 +02:00
'password',
'password_confirmation',
];
/**
2021-12-02 13:15:53 +01:00
* Register the exception handling callbacks for the application.
2019-05-20 07:37:41 +02:00
*
* @return void
*/
2021-12-02 13:15:53 +01:00
public function register()
2019-05-20 07:37:41 +02:00
{
2021-12-02 13:15:53 +01:00
$this->renderable(function (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $request) {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => 'not found',
], 404);
2021-12-02 13:15:53 +01:00
});
$this->renderable(function (\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException $exception, $request) {
return response()->json([
'message' => 'unauthorized',
], 403);
});
2021-12-02 13:15:53 +01:00
$this->renderable(function (InvalidOtpParameterException $exception, $request) {
2021-09-17 23:45:08 +02:00
return response()->json([
'message' => 'invalid OTP parameters',
2022-11-22 15:15:52 +01:00
'reason' => [$exception->getMessage()],
2021-09-17 23:45:08 +02:00
], 400);
2021-12-02 13:15:53 +01:00
});
$this->renderable(function (InvalidQrCodeException $exception, $request) {
return response()->json([
'message' => $exception->getMessage(),
], 400);
2021-12-02 13:15:53 +01:00
});
$this->renderable(function (InvalidSecretException $exception, $request) {
2021-09-17 23:45:08 +02:00
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => 'not a valid base32 encoded secret', ], 400);
2021-12-02 13:15:53 +01:00
});
$this->renderable(function (DbEncryptionException $exception, $request) {
2021-11-26 11:21:57 +01:00
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => $exception->getMessage(), ], 400);
2021-12-02 13:15:53 +01:00
});
$this->renderable(function (InvalidMigrationDataException $exception, $request) {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => __('errors.invalid_x_migration', ['appname' => $exception->getMessage()]),
], 400);
});
$this->renderable(function (UnsupportedMigrationException $exception, $request) {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => __('errors.unsupported_migration'),
], 400);
});
$this->renderable(function (EncryptedMigrationException $exception, $request) {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => __('errors.encrypted_migration'),
], 400);
});
$this->renderable(function (UndecipherableException $exception, $request) {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => __('errors.cannot_decipher_secret'),
], 400);
});
$this->renderable(function (UnsupportedOtpTypeException $exception, $request) {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => __('errors.unsupported_otp_type'),
], 400);
});
$this->renderable(function (\Illuminate\Auth\AuthenticationException $exception, $request) {
if ($exception->guards() === ['reverse-proxy-guard']) {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => $exception->getMessage(),
], 407);
} else {
return response()->json([
2022-11-22 15:15:52 +01:00
'message' => $exception->getMessage(),
], 401);
}
});
2019-05-26 16:44:46 +02:00
}
2022-11-22 15:15:52 +01:00
}