Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions app/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,47 @@ public function __construct()
$this->client = new \AntiPatternInc\Saasus\Api\Client();
}

public function credentials(Request $request)
{
$request->validate([
'code' => 'required|string'
]);

try {
$authClient = $this->client->getAuthClient();
$res = $authClient->getAuthCredentials([
'code' => $request->code,
'auth-flow' => 'tempCodeAuth',
], $authClient::FETCH_RESPONSE);

$body = json_decode($res->getBody(), true);
return response()->json($body, Response::HTTP_OK);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json(['detail' => 'Error occurred'], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function refresh(Request $request)
{
// リフレッシュトークンを取得
$refreshToken = $request->cookie('SaaSusRefreshToken');
if (!is_string($refreshToken)) {
return response('Refresh token not found', Response::HTTP_BAD_REQUEST);
return response()->json(['detail' => 'Refresh token not found'], Response::HTTP_BAD_REQUEST);
}

try {
$authClient = $this->client->getAuthClient();
$response = $authClient->getAuthCredentials([
'',
'refreshTokenAuth',
$refreshToken
]);

return response()->json($response->getBody());
$res = $authClient->getAuthCredentials([
'auth-flow' => 'refreshTokenAuth',
'refresh-token' => $refreshToken
], $authClient::FETCH_RESPONSE);

$body = json_decode($res->getBody(), true);
return response()->json($body, Response::HTTP_OK);
} catch (\Exception $e) {
return response('Error occurred', Response::HTTP_INTERNAL_SERVER_ERROR);
Log::error($e->getMessage());
return response()->json(['detail' => 'Error occurred'], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

Expand Down
5 changes: 2 additions & 3 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\IndexController;
use App\Http\Controllers\BillingController;
use AntiPatternInc\Saasus\Laravel\Controllers\CallbackApiController;
/*
|--------------------------------------------------------------------------
| API Routes
Expand All @@ -16,8 +15,8 @@
|
*/

// 一時コードからIDトークンなどの認証情報を取得するコントローラを登録
Route::get('/credentials', [CallbackApiController::class, 'index']);
// 一時コードからIDトークンなどの認証情報を取得
Route::get('/credentials', [IndexController::class, 'credentials']);
Route::get('/refresh', [IndexController::class, 'refresh']);

// SaaSus SDK標準のAuth Middlewareを利用する
Expand Down