Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Error Handling in Functions with API Requests #11

Merged
merged 9 commits into from
Mar 17, 2023
Merged
34 changes: 27 additions & 7 deletions src/Laravel/Controllers/CallbackApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
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
{
Expand All @@ -30,12 +29,33 @@ public function index(Request $request)
$res = $authApi->getAuthCredentials([
'code' => $request->code, 'auth-flow' => 'tempCodeAuth',
], $authApi::FETCH_RESPONSE);
return json_decode($res->getBody(), true);
} 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);
$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/token/refresh',
'secure' => true,
'httponly' => true,
'samesite' => 'None'
);
setcookie('saasus_refresh_token', $body['refresh_token'], $arr_cookie_options);
return response()->json($body, Response::HTTP_OK);
} catch (\Exception $e) {
if ($e instanceof HttpException) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpException以外のケースがわかってないですが
ログ出力なしで良さそうかだけ確認させてください〜

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ログ出すようにしました!3577e8d

$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: ' . $type . ', Message: ' . $message);
return response()->json(['type' => $type, 'message' => $message], Response::HTTP_NOT_FOUND);
}
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);
}
}
}
19 changes: 13 additions & 6 deletions src/Laravel/Controllers/CallbackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -30,12 +30,19 @@ 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();
$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: ' . $type . ', Message: ' . $message);
return redirect(getenv('SAASUS_LOGIN_URL'));
}
Log::info('Type: ' . $type . ', Message: ' . $message);
return redirect(getenv('SAASUS_LOGIN_URL'));
}
Log::info('Type: Internal Server Error, Message: ' . $e->getError());
Log::info('Uncaught error: ' . $e);
return redirect(getenv('SAASUS_LOGIN_URL'));
}
$arr_cookie_options = array(
Expand Down
55 changes: 55 additions & 0 deletions src/Laravel/Controllers/TokenRefreshApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace AntiPatternInc\Saasus\Laravel\Controllers;

use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Log;

use Symfony\Component\HttpFoundation\Response;

use AntiPatternInc\Saasus\Api\Client as ApiClient;
use Error;
use Http\Client\Exception\HttpException;

class TokenRefreshApiController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

public function index(Request $request)
{
$refreshToken = $request->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 (\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: ' . $type . ', Message: ' . $message);
return response()->json(['type' => $type, 'message' => $message], Response::HTTP_NOT_FOUND);
}
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);
}
}
}
29 changes: 17 additions & 12 deletions src/Laravel/Middleware/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,17 +41,24 @@ 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();
$type = json_decode($e->getResponse()->getBody(), true)["type"];
$message = json_decode($e->getResponse()->getBody(), true)["message"];
if ($statusCode == Response::HTTP_UNAUTHORIZED) {
Log::info('Type: ' . $type . ', Message: ' . $message);
if (getenv('SAASUS_AUTH_MODE') == "api") {
return response()->json(['type' => $type, 'message' => $message], Response::HTTP_UNAUTHORIZED);
} else {
return redirect(getenv('SAASUS_LOGIN_URL'));
}
}
Log::info('Type: ' . $type . ', Message: ' . $message);
return response()->json(['type' => $type, 'message' => $message], 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);
Log::info('Uncaught error: ' . $e);
return response()->json('Uncaught error', Response::HTTP_INTERNAL_SERVER_ERROR);
}

$userinfo = $response->getBody();
Expand Down