From eeebce2a1a44ae04446c6c425832850868513aa8 Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Thu, 23 Feb 2023 21:09:36 +0900 Subject: [PATCH 1/9] create TokenRefreshApiController --- .../Controllers/CallbackApiController.php | 14 +++++- .../Controllers/TokenRefreshApiController.php | 48 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/Laravel/Controllers/TokenRefreshApiController.php diff --git a/src/Laravel/Controllers/CallbackApiController.php b/src/Laravel/Controllers/CallbackApiController.php index cbd1a2a..521006b 100644 --- a/src/Laravel/Controllers/CallbackApiController.php +++ b/src/Laravel/Controllers/CallbackApiController.php @@ -30,12 +30,24 @@ public function index(Request $request) $res = $authApi->getAuthCredentials([ 'code' => $request->code, 'auth-flow' => 'tempCodeAuth', ], $authApi::FETCH_RESPONSE); - return json_decode($res->getBody(), true); + $body = json_decode($res->getBody(), true); + if (empty($body['refresh_token'])) { + return response()->json($body, Response::HTTP_OK); + } + $arr_cookie_options = array( + 'expires' => time() + 60 * 60 * 24 * 30, + 'path' => '/api/new-tokens', + 'secure' => true, + 'httponly' => true, + 'samesite' => 'None' + ); + return response()->json($body, Response::HTTP_OK)->cookie('saasus_refresh_token', $body['refresh_token'], $arr_cookie_options); } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { if (get_class($e) == 'GetAuthCredentialsNotFoundException') { Log::info('Type: Not Found, Message: ' . $e->getError()); return response()->json('credentials not found', Response::HTTP_NOT_FOUND); } + return response()->json('internal server error', Response::HTTP_INTERNAL_SERVER_ERROR); } } } diff --git a/src/Laravel/Controllers/TokenRefreshApiController.php b/src/Laravel/Controllers/TokenRefreshApiController.php new file mode 100644 index 0000000..fbeadb2 --- /dev/null +++ b/src/Laravel/Controllers/TokenRefreshApiController.php @@ -0,0 +1,48 @@ +cookie('saasus_refresh_token'); + if (empty($refreshToken)) { + return response()->json('saasus_refresh_token cookie is required', Response::HTTP_BAD_REQUEST); + } + $client = new ApiClient(); + $authApi = $client->getAuthClient(); + try { + $res = $authApi->getAuthCredentials([ + 'refresh-token' => $refreshToken, 'auth-flow' => 'refreshTokenAuth', + ], $authApi::FETCH_RESPONSE); + $body = json_decode($res->getBody(), true); + if (empty($body['id_token']) || empty($body['access_token'])) { + throw new Error('failed to get new credentials'); + } + return response()->json($body, Response::HTTP_OK); + } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { + if (get_class($e) == 'GetAuthCredentialsNotFoundException') { + Log::info('Type: Not Found, Message: ' . $e->getError()); + return response()->json('credentials not found', Response::HTTP_NOT_FOUND); + } + return response()->json('internal server error', Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} From e386444c89f027f2cb5803aeb5ccbb8a28b5361b Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Thu, 23 Feb 2023 21:15:56 +0900 Subject: [PATCH 2/9] use setcookie --- src/Laravel/Controllers/CallbackApiController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Laravel/Controllers/CallbackApiController.php b/src/Laravel/Controllers/CallbackApiController.php index 521006b..8bc0a43 100644 --- a/src/Laravel/Controllers/CallbackApiController.php +++ b/src/Laravel/Controllers/CallbackApiController.php @@ -41,7 +41,8 @@ public function index(Request $request) 'httponly' => true, 'samesite' => 'None' ); - return response()->json($body, Response::HTTP_OK)->cookie('saasus_refresh_token', $body['refresh_token'], $arr_cookie_options); + setcookie('saasus_refresh_token', $body['refresh_token'], $arr_cookie_options); + return response()->json($body, Response::HTTP_OK); } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { if (get_class($e) == 'GetAuthCredentialsNotFoundException') { Log::info('Type: Not Found, Message: ' . $e->getError()); From 0d926eb645a6c486d9b7cbf5adb66abdee525a7a Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Sat, 25 Feb 2023 18:28:33 +0900 Subject: [PATCH 3/9] set refresh token in CallbackController --- .../Controllers/CallbackController.php | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Laravel/Controllers/CallbackController.php b/src/Laravel/Controllers/CallbackController.php index bf76684..a7197cd 100644 --- a/src/Laravel/Controllers/CallbackController.php +++ b/src/Laravel/Controllers/CallbackController.php @@ -23,6 +23,7 @@ public function index(Request $request) return redirect(getenv('SAASUS_LOGIN_URL')); } $idToken = ''; + $refreshToken = ''; $client = new ApiClient; $authApiClient = $client->getAuthClient(); try { @@ -30,6 +31,7 @@ public function index(Request $request) 'code' => $request->code, 'auth-flow' => 'tempCodeAuth', ]); $idToken = $res->getIdToken(); + $refreshToken = $res->getRefreshToken(); } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { if (get_class($e) == 'GetAuthCredentialsNotFoundException') { Log::info('Type: Not Found, Message: ' . $e->getError()); @@ -38,18 +40,28 @@ public function index(Request $request) Log::info('Type: Internal Server Error, Message: ' . $e->getError()); return redirect(getenv('SAASUS_LOGIN_URL')); } - $arr_cookie_options = array( - // 'expires' => time() + 60 * 60 * 24 * 30, + $idTokenCookieOptions = array( 'path' => '/', - // 'domain' => '.example.com', // leading dot for compatibility or use subdomain - 'secure' => false, // or false - 'httponly' => true, // or false - 'samesite' => 'Strict' // None || Lax || Strict + 'secure' => true, + 'httponly' => true, + 'samesite' => 'Strict' ); setcookie( 'SaaSus_idToken', $idToken, - $arr_cookie_options + $idTokenCookieOptions + ); + $refreshTokenCookieOptions = array( + 'expires' => time() + 60 * 60 * 24 * 30, + 'path' => '/new-tokens', + 'secure' => true, + 'httponly' => true, + 'samesite' => 'Strict', + ); + setcookie( + 'SaaSus_refreshToken', + $refreshToken, + $refreshTokenCookieOptions, ); return response()->view('saasus_default_callback', ['idToken' => $idToken]); From 576cd62c9160bf93d8b11b2ce79b677d306f2a45 Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Sat, 25 Feb 2023 20:02:48 +0900 Subject: [PATCH 4/9] change cookie path --- src/Laravel/Controllers/CallbackApiController.php | 2 +- src/Laravel/Controllers/CallbackController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Laravel/Controllers/CallbackApiController.php b/src/Laravel/Controllers/CallbackApiController.php index 8bc0a43..e6c2910 100644 --- a/src/Laravel/Controllers/CallbackApiController.php +++ b/src/Laravel/Controllers/CallbackApiController.php @@ -36,7 +36,7 @@ public function index(Request $request) } $arr_cookie_options = array( 'expires' => time() + 60 * 60 * 24 * 30, - 'path' => '/api/new-tokens', + 'path' => '/api/token/refresh', 'secure' => true, 'httponly' => true, 'samesite' => 'None' diff --git a/src/Laravel/Controllers/CallbackController.php b/src/Laravel/Controllers/CallbackController.php index a7197cd..d4b656d 100644 --- a/src/Laravel/Controllers/CallbackController.php +++ b/src/Laravel/Controllers/CallbackController.php @@ -53,7 +53,7 @@ public function index(Request $request) ); $refreshTokenCookieOptions = array( 'expires' => time() + 60 * 60 * 24 * 30, - 'path' => '/new-tokens', + 'path' => '/token/refresh', 'secure' => true, 'httponly' => true, 'samesite' => 'Strict', From d8b4953eb3ef81ae9fca7a2b94a342691ef0a8f3 Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Fri, 3 Mar 2023 16:05:43 +0900 Subject: [PATCH 5/9] revert changes in CallbackController --- .../Controllers/CallbackController.php | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/src/Laravel/Controllers/CallbackController.php b/src/Laravel/Controllers/CallbackController.php index d4b656d..bf76684 100644 --- a/src/Laravel/Controllers/CallbackController.php +++ b/src/Laravel/Controllers/CallbackController.php @@ -23,7 +23,6 @@ public function index(Request $request) return redirect(getenv('SAASUS_LOGIN_URL')); } $idToken = ''; - $refreshToken = ''; $client = new ApiClient; $authApiClient = $client->getAuthClient(); try { @@ -31,7 +30,6 @@ public function index(Request $request) 'code' => $request->code, 'auth-flow' => 'tempCodeAuth', ]); $idToken = $res->getIdToken(); - $refreshToken = $res->getRefreshToken(); } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { if (get_class($e) == 'GetAuthCredentialsNotFoundException') { Log::info('Type: Not Found, Message: ' . $e->getError()); @@ -40,28 +38,18 @@ public function index(Request $request) Log::info('Type: Internal Server Error, Message: ' . $e->getError()); return redirect(getenv('SAASUS_LOGIN_URL')); } - $idTokenCookieOptions = array( + $arr_cookie_options = array( + // 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', - 'secure' => true, - 'httponly' => true, - 'samesite' => 'Strict' + // 'domain' => '.example.com', // leading dot for compatibility or use subdomain + 'secure' => false, // or false + 'httponly' => true, // or false + 'samesite' => 'Strict' // None || Lax || Strict ); setcookie( 'SaaSus_idToken', $idToken, - $idTokenCookieOptions - ); - $refreshTokenCookieOptions = array( - 'expires' => time() + 60 * 60 * 24 * 30, - 'path' => '/token/refresh', - 'secure' => true, - 'httponly' => true, - 'samesite' => 'Strict', - ); - setcookie( - 'SaaSus_refreshToken', - $refreshToken, - $refreshTokenCookieOptions, + $arr_cookie_options ); return response()->view('saasus_default_callback', ['idToken' => $idToken]); From 87dbc03a7e1336c59e88d34e9d69abe65a3f831f Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Fri, 3 Mar 2023 19:35:53 +0900 Subject: [PATCH 6/9] imple error handling --- .../Controllers/CallbackApiController.php | 16 ++++++++---- .../Controllers/CallbackController.php | 16 +++++++----- .../Controllers/TokenRefreshApiController.php | 18 ++++++++----- src/Laravel/Middleware/Auth.php | 26 ++++++++++--------- 4 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/Laravel/Controllers/CallbackApiController.php b/src/Laravel/Controllers/CallbackApiController.php index e6c2910..c9d78e4 100644 --- a/src/Laravel/Controllers/CallbackApiController.php +++ b/src/Laravel/Controllers/CallbackApiController.php @@ -14,6 +14,7 @@ use AntiPatternInc\Saasus\Api\Client as ApiClient; use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsNotFoundException; use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsInternalServerErrorException; +use Http\Client\Exception\HttpException; class CallbackApiController extends BaseController { @@ -43,12 +44,17 @@ public function index(Request $request) ); setcookie('saasus_refresh_token', $body['refresh_token'], $arr_cookie_options); return response()->json($body, Response::HTTP_OK); - } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { - if (get_class($e) == 'GetAuthCredentialsNotFoundException') { - Log::info('Type: Not Found, Message: ' . $e->getError()); - return response()->json('credentials not found', Response::HTTP_NOT_FOUND); + } catch (\Exception $e) { + if ($e instanceof HttpException) { + $statusCode = $e->getResponse()->getStatusCode(); + if ($statusCode == Response::HTTP_NOT_FOUND) { + Log::info('Type: Not Found, Message: ' . $e->getResponse()); + return response()->json("Credentials Not Found", Response::HTTP_NOT_FOUND); + } + Log::info('Type: Internal Server Error, Message: ' . $e->getResponse()); + return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); } - return response()->json('internal server error', Response::HTTP_INTERNAL_SERVER_ERROR); + return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); } } } diff --git a/src/Laravel/Controllers/CallbackController.php b/src/Laravel/Controllers/CallbackController.php index bf76684..fceb84c 100644 --- a/src/Laravel/Controllers/CallbackController.php +++ b/src/Laravel/Controllers/CallbackController.php @@ -10,8 +10,8 @@ use Illuminate\Support\Facades\Log; use AntiPatternInc\Saasus\Api\Client as ApiClient; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsNotFoundException; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsInternalServerErrorException; +use Http\Client\Exception\HttpException; +use Symfony\Component\HttpFoundation\Response; class CallbackController extends BaseController { @@ -30,12 +30,16 @@ public function index(Request $request) 'code' => $request->code, 'auth-flow' => 'tempCodeAuth', ]); $idToken = $res->getIdToken(); - } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { - if (get_class($e) == 'GetAuthCredentialsNotFoundException') { - Log::info('Type: Not Found, Message: ' . $e->getError()); + } catch (\Exception $e) { + if ($e instanceof HttpException) { + $statusCode = $e->getResponse()->getStatusCode(); + if ($statusCode == Response::HTTP_NOT_FOUND) { + Log::info('Type: Not Found, Message: ' . $e->getResponse()); + return redirect(getenv('SAASUS_LOGIN_URL')); + } + Log::info('Type: Internal Server Error, Message: ' . $e->getResponse()); return redirect(getenv('SAASUS_LOGIN_URL')); } - Log::info('Type: Internal Server Error, Message: ' . $e->getError()); return redirect(getenv('SAASUS_LOGIN_URL')); } $arr_cookie_options = array( diff --git a/src/Laravel/Controllers/TokenRefreshApiController.php b/src/Laravel/Controllers/TokenRefreshApiController.php index fbeadb2..b62030b 100644 --- a/src/Laravel/Controllers/TokenRefreshApiController.php +++ b/src/Laravel/Controllers/TokenRefreshApiController.php @@ -12,9 +12,8 @@ use Symfony\Component\HttpFoundation\Response; use AntiPatternInc\Saasus\Api\Client as ApiClient; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsNotFoundException; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsInternalServerErrorException; use Error; +use Http\Client\Exception\HttpException; class TokenRefreshApiController extends BaseController { @@ -37,12 +36,17 @@ public function index(Request $request) throw new Error('failed to get new credentials'); } return response()->json($body, Response::HTTP_OK); - } catch (GetAuthCredentialsNotFoundException | GetAuthCredentialsInternalServerErrorException $e) { - if (get_class($e) == 'GetAuthCredentialsNotFoundException') { - Log::info('Type: Not Found, Message: ' . $e->getError()); - return response()->json('credentials not found', Response::HTTP_NOT_FOUND); + } catch (\Exception $e) { + if ($e instanceof HttpException) { + $statusCode = $e->getResponse()->getStatusCode(); + if ($statusCode == Response::HTTP_NOT_FOUND) { + Log::info('Type: Not Found, Message: ' . $e->getResponse()); + return response()->json("Credentials Not Found", Response::HTTP_NOT_FOUND); + } + Log::info('Type: Internal Server Error, Message: ' . $e->getResponse()); + return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); } - return response()->json('internal server error', Response::HTTP_INTERNAL_SERVER_ERROR); + return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); } } } diff --git a/src/Laravel/Middleware/Auth.php b/src/Laravel/Middleware/Auth.php index 6cedfcd..cd622b5 100644 --- a/src/Laravel/Middleware/Auth.php +++ b/src/Laravel/Middleware/Auth.php @@ -5,9 +5,7 @@ use AntiPatternInc\Saasus\Api\Client as ApiClient; use Closure; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetUserInfoUnauthorizedException; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetUserInfoInternalServerErrorException; - +use Http\Client\Exception\HttpException; use Symfony\Component\HttpFoundation\Response; use Illuminate\Support\Facades\Log; @@ -43,17 +41,21 @@ public function handle(Request $request, Closure $next) $authApiClient = $client->getAuthClient(); try { $response = $authApiClient->getUserInfo(['token' => $token], $authApiClient::FETCH_RESPONSE); - } catch (GetUserInfoUnauthorizedException | GetUserInfoInternalServerErrorException $e) { - if (get_class($e) == "GetUserInfoUnauthorizedException") { - Log::info('Type: Unauthorized, Message: ' . $e->getError()); - if (getenv('SAASUS_AUTH_MODE') == "api") { - return response()->json('Invalid ID Token.', Response::HTTP_UNAUTHORIZED); - } else { - return redirect(getenv('SAASUS_LOGIN_URL')); + } catch (\Exception $e) { + if ($e instanceof HttpException) { + $statusCode = $e->getResponse()->getStatusCode(); + if ($statusCode == Response::HTTP_UNAUTHORIZED) { + Log::info('Type: Unauthorized, Message: ' . $e->getResponse()); + if (getenv('SAASUS_AUTH_MODE') == "api") { + return response()->json('Invalid ID Token.', Response::HTTP_UNAUTHORIZED); + } else { + return redirect(getenv('SAASUS_LOGIN_URL')); + } } + Log::info('Type: Intenal Server Error, Message: ' . $e->getResponse()); + return response()->json('Unexpected response: ' . $e->getResponse(), Response::HTTP_INTERNAL_SERVER_ERROR); } - Log::info('Type: Intenal Server Error, Message: ' . $e->getError()); - return response()->json('Unexpected response: ' . $e->getError(), Response::HTTP_INTERNAL_SERVER_ERROR); + return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); } $userinfo = $response->getBody(); From 7d0f2f7a551b51363859a5cd99298f3972ef54b5 Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Sat, 4 Mar 2023 23:48:05 +0900 Subject: [PATCH 7/9] use type and message for error response --- src/Laravel/Controllers/CallbackApiController.php | 12 +++++++----- src/Laravel/Controllers/CallbackController.php | 6 ++++-- .../Controllers/TokenRefreshApiController.php | 12 +++++++----- src/Laravel/Middleware/Auth.php | 10 ++++++---- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Laravel/Controllers/CallbackApiController.php b/src/Laravel/Controllers/CallbackApiController.php index c9d78e4..65367c3 100644 --- a/src/Laravel/Controllers/CallbackApiController.php +++ b/src/Laravel/Controllers/CallbackApiController.php @@ -47,14 +47,16 @@ public function index(Request $request) } catch (\Exception $e) { if ($e instanceof HttpException) { $statusCode = $e->getResponse()->getStatusCode(); + $type = json_decode($e->getResponse()->getBody(), true)["type"]; + $message = json_decode($e->getResponse()->getBody(), true)["message"]; if ($statusCode == Response::HTTP_NOT_FOUND) { - Log::info('Type: Not Found, Message: ' . $e->getResponse()); - return response()->json("Credentials Not Found", Response::HTTP_NOT_FOUND); + Log::info('Type: ' . $type . ', Message: ' . $message); + return response()->json(['type' => $type, 'message' => $message], Response::HTTP_NOT_FOUND); } - Log::info('Type: Internal Server Error, Message: ' . $e->getResponse()); - return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); + Log::info('Type: ' . $type . ', Message: ' . $message); + return response()->json(['type' => $type, 'message' => $message], Response::HTTP_INTERNAL_SERVER_ERROR); } - return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); + return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); } } } diff --git a/src/Laravel/Controllers/CallbackController.php b/src/Laravel/Controllers/CallbackController.php index fceb84c..8c9863c 100644 --- a/src/Laravel/Controllers/CallbackController.php +++ b/src/Laravel/Controllers/CallbackController.php @@ -33,11 +33,13 @@ public function index(Request $request) } catch (\Exception $e) { if ($e instanceof HttpException) { $statusCode = $e->getResponse()->getStatusCode(); + $type = json_decode($e->getResponse()->getBody(), true)["type"]; + $message = json_decode($e->getResponse()->getBody(), true)["message"]; if ($statusCode == Response::HTTP_NOT_FOUND) { - Log::info('Type: Not Found, Message: ' . $e->getResponse()); + Log::info('Type: ' . $type . ', Message: ' . $message); return redirect(getenv('SAASUS_LOGIN_URL')); } - Log::info('Type: Internal Server Error, Message: ' . $e->getResponse()); + Log::info('Type: ' . $type . ', Message: ' . $message); return redirect(getenv('SAASUS_LOGIN_URL')); } return redirect(getenv('SAASUS_LOGIN_URL')); diff --git a/src/Laravel/Controllers/TokenRefreshApiController.php b/src/Laravel/Controllers/TokenRefreshApiController.php index b62030b..c2a5a0a 100644 --- a/src/Laravel/Controllers/TokenRefreshApiController.php +++ b/src/Laravel/Controllers/TokenRefreshApiController.php @@ -39,14 +39,16 @@ public function index(Request $request) } catch (\Exception $e) { if ($e instanceof HttpException) { $statusCode = $e->getResponse()->getStatusCode(); + $type = json_decode($e->getResponse()->getBody(), true)["type"]; + $message = json_decode($e->getResponse()->getBody(), true)["message"]; if ($statusCode == Response::HTTP_NOT_FOUND) { - Log::info('Type: Not Found, Message: ' . $e->getResponse()); - return response()->json("Credentials Not Found", Response::HTTP_NOT_FOUND); + Log::info('Type: ' . $type . ', Message: ' . $message); + return response()->json(['type' => $type, 'message' => $message], Response::HTTP_NOT_FOUND); } - Log::info('Type: Internal Server Error, Message: ' . $e->getResponse()); - return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); + Log::info('Type: ' . $type . ', Message: ' . $message); + return response()->json(['type' => $type, 'message' => $message], Response::HTTP_INTERNAL_SERVER_ERROR); } - return response()->json('Internal Server Error', Response::HTTP_INTERNAL_SERVER_ERROR); + return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); } } } diff --git a/src/Laravel/Middleware/Auth.php b/src/Laravel/Middleware/Auth.php index cd622b5..5410856 100644 --- a/src/Laravel/Middleware/Auth.php +++ b/src/Laravel/Middleware/Auth.php @@ -44,16 +44,18 @@ public function handle(Request $request, Closure $next) } catch (\Exception $e) { if ($e instanceof HttpException) { $statusCode = $e->getResponse()->getStatusCode(); + $type = json_decode($e->getResponse()->getBody(), true)["type"]; + $message = json_decode($e->getResponse()->getBody(), true)["message"]; if ($statusCode == Response::HTTP_UNAUTHORIZED) { - Log::info('Type: Unauthorized, Message: ' . $e->getResponse()); + Log::info('Type: ' . $type . ', Message: ' . $message); if (getenv('SAASUS_AUTH_MODE') == "api") { - return response()->json('Invalid ID Token.', Response::HTTP_UNAUTHORIZED); + return response()->json(['type' => $type, 'message' => $message], Response::HTTP_UNAUTHORIZED); } else { return redirect(getenv('SAASUS_LOGIN_URL')); } } - Log::info('Type: Intenal Server Error, Message: ' . $e->getResponse()); - return response()->json('Unexpected response: ' . $e->getResponse(), Response::HTTP_INTERNAL_SERVER_ERROR); + Log::info('Type: ' . $type . ', Message: ' . $message); + return response()->json(['type' => $type, 'message' => $message], Response::HTTP_INTERNAL_SERVER_ERROR); } return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); } From 6d9173ec9ce5a236a9bd0fd4bc7493f1c794961c Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Sat, 4 Mar 2023 23:54:03 +0900 Subject: [PATCH 8/9] remove use statements of unused packages --- src/Laravel/Controllers/CallbackApiController.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Laravel/Controllers/CallbackApiController.php b/src/Laravel/Controllers/CallbackApiController.php index 65367c3..4a52503 100644 --- a/src/Laravel/Controllers/CallbackApiController.php +++ b/src/Laravel/Controllers/CallbackApiController.php @@ -12,8 +12,6 @@ use Symfony\Component\HttpFoundation\Response; use AntiPatternInc\Saasus\Api\Client as ApiClient; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsNotFoundException; -use AntiPatternInc\Saasus\Sdk\Auth\Exception\GetAuthCredentialsInternalServerErrorException; use Http\Client\Exception\HttpException; class CallbackApiController extends BaseController From 3577e8de5881b6b10a43f043ae48dc3cc2f54a58 Mon Sep 17 00:00:00 2001 From: Yuto Moriyasu Date: Mon, 13 Mar 2023 16:04:53 +0900 Subject: [PATCH 9/9] add uncaught error log --- src/Laravel/Controllers/CallbackApiController.php | 1 + src/Laravel/Controllers/CallbackController.php | 1 + src/Laravel/Controllers/TokenRefreshApiController.php | 1 + src/Laravel/Middleware/Auth.php | 1 + 4 files changed, 4 insertions(+) diff --git a/src/Laravel/Controllers/CallbackApiController.php b/src/Laravel/Controllers/CallbackApiController.php index 4a52503..b0a040f 100644 --- a/src/Laravel/Controllers/CallbackApiController.php +++ b/src/Laravel/Controllers/CallbackApiController.php @@ -54,6 +54,7 @@ public function index(Request $request) Log::info('Type: ' . $type . ', Message: ' . $message); return response()->json(['type' => $type, 'message' => $message], Response::HTTP_INTERNAL_SERVER_ERROR); } + Log::info('Uncaught error: ' . $e); return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); } } diff --git a/src/Laravel/Controllers/CallbackController.php b/src/Laravel/Controllers/CallbackController.php index 8c9863c..bc42bfd 100644 --- a/src/Laravel/Controllers/CallbackController.php +++ b/src/Laravel/Controllers/CallbackController.php @@ -42,6 +42,7 @@ public function index(Request $request) Log::info('Type: ' . $type . ', Message: ' . $message); return redirect(getenv('SAASUS_LOGIN_URL')); } + Log::info('Uncaught error: ' . $e); return redirect(getenv('SAASUS_LOGIN_URL')); } $arr_cookie_options = array( diff --git a/src/Laravel/Controllers/TokenRefreshApiController.php b/src/Laravel/Controllers/TokenRefreshApiController.php index c2a5a0a..fee7514 100644 --- a/src/Laravel/Controllers/TokenRefreshApiController.php +++ b/src/Laravel/Controllers/TokenRefreshApiController.php @@ -48,6 +48,7 @@ public function index(Request $request) Log::info('Type: ' . $type . ', Message: ' . $message); return response()->json(['type' => $type, 'message' => $message], Response::HTTP_INTERNAL_SERVER_ERROR); } + Log::info('Uncaught error: ' . $e); return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); } } diff --git a/src/Laravel/Middleware/Auth.php b/src/Laravel/Middleware/Auth.php index 5410856..ae4ec3e 100644 --- a/src/Laravel/Middleware/Auth.php +++ b/src/Laravel/Middleware/Auth.php @@ -57,6 +57,7 @@ public function handle(Request $request, Closure $next) Log::info('Type: ' . $type . ', Message: ' . $message); return response()->json(['type' => $type, 'message' => $message], Response::HTTP_INTERNAL_SERVER_ERROR); } + Log::info('Uncaught error: ' . $e); return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR); }