From c9f939fc66d4b6819c67cc88db8cd22fff8cf032 Mon Sep 17 00:00:00 2001 From: MilesChou Date: Tue, 27 Sep 2022 13:08:05 +0800 Subject: [PATCH] day12 --- app/Http/Controllers/Hydra/AcceptConsent.php | 49 +++++++++++++++++++ app/Http/Controllers/Hydra/Consent.php | 14 ------ .../Controllers/Hydra/ConsentProvider.php | 22 ++++++++- app/Http/Controllers/Hydra/RejectConsent.php | 40 +++++++++++++++ resources/views/auth/consent.blade.php | 29 +++++++---- routes/web.php | 6 ++- 6 files changed, 132 insertions(+), 28 deletions(-) create mode 100644 app/Http/Controllers/Hydra/AcceptConsent.php delete mode 100644 app/Http/Controllers/Hydra/Consent.php create mode 100644 app/Http/Controllers/Hydra/RejectConsent.php diff --git a/app/Http/Controllers/Hydra/AcceptConsent.php b/app/Http/Controllers/Hydra/AcceptConsent.php new file mode 100644 index 0000000..e3fe02c --- /dev/null +++ b/app/Http/Controllers/Hydra/AcceptConsent.php @@ -0,0 +1,49 @@ +input('challenge'); + + if (empty($consentChallenge)) { + throw new RuntimeException('No consent_challenge'); + } + + $scopes = $request->input('scopes'); + + if (empty($scopes)) { + // 沒填 scope 就請回去按 reject 的按鈕 + return Redirect::back(); + } + + $acceptConsentRequest = new AcceptConsentRequest([ + 'grantScope' => array_keys($scopes), + 'remember' => true, + 'rememberFor' => 120, + ]); + + Log::debug('Accept consent Request', json_decode((string)$acceptConsentRequest, true)); + + try { + $completedRequest = $adminApi->acceptConsentRequest($consentChallenge, $acceptConsentRequest); + } catch (\Throwable $e) { + dd($e); + throw new RuntimeException('Hydra Server error: ' . $e->getMessage()); + } + + Log::debug('Consent Completed Request', json_decode((string)$completedRequest, true)); + + return Redirect::away($completedRequest->getRedirectTo()); + } +} diff --git a/app/Http/Controllers/Hydra/Consent.php b/app/Http/Controllers/Hydra/Consent.php deleted file mode 100644 index ccc9792..0000000 --- a/app/Http/Controllers/Hydra/Consent.php +++ /dev/null @@ -1,14 +0,0 @@ -all()); - return 'OAuth 2.0 授權完成'; - } -} diff --git a/app/Http/Controllers/Hydra/ConsentProvider.php b/app/Http/Controllers/Hydra/ConsentProvider.php index e2ad24d..1632dfd 100644 --- a/app/Http/Controllers/Hydra/ConsentProvider.php +++ b/app/Http/Controllers/Hydra/ConsentProvider.php @@ -2,10 +2,28 @@ namespace App\Http\Controllers\Hydra; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Log; +use Ory\Hydra\Client\Api\AdminApi; +use RuntimeException; + class ConsentProvider { - public function __invoke() + public function __invoke(Request $request, AdminApi $adminApi) { - return view('auth.consent'); + $consentChallenge = $request->input('consent_challenge'); + + if (empty($consentChallenge)) { + throw new RuntimeException('No consent_challenge'); + } + + $consentRequest = $adminApi->getConsentRequest($consentChallenge); + + Log::debug('Get consent Request', json_decode((string)$consentRequest, true)); + + return view('auth.consent', [ + 'challenge' => $consentChallenge, + 'scopes' => $consentRequest->getRequestedScope(), + ]); } } diff --git a/app/Http/Controllers/Hydra/RejectConsent.php b/app/Http/Controllers/Hydra/RejectConsent.php new file mode 100644 index 0000000..afd744e --- /dev/null +++ b/app/Http/Controllers/Hydra/RejectConsent.php @@ -0,0 +1,40 @@ +input('challenge'); + + if (empty($consentChallenge)) { + throw new RuntimeException('No consent_challenge'); + } + + $rejectRequest = new RejectRequest([ + 'error' => 'access_denied', + 'errorDescription' => 'The request was rejected by end-user', + ]); + + Log::debug('Reject consent Request', json_decode((string)$rejectRequest, true)); + + try { + $completedRequest = $adminApi->rejectConsentRequest($consentChallenge, $rejectRequest); + } catch (\Throwable $e) { + throw new RuntimeException('Hydra Server error: ' . $e->getMessage()); + } + + Log::debug('Consent Completed Request', json_decode((string)$completedRequest, true)); + + return Redirect::away($completedRequest->getRedirectTo()); + } +} diff --git a/resources/views/auth/consent.blade.php b/resources/views/auth/consent.blade.php index b225061..44277da 100644 --- a/resources/views/auth/consent.blade.php +++ b/resources/views/auth/consent.blade.php @@ -12,22 +12,20 @@ -
+ @csrf +
- - - + @foreach($scopes as $scope) + + @endforeach
@@ -44,5 +42,16 @@
+ +
+ @csrf + + +
+ + {{ __('Reject Permission') }} + +
+
diff --git a/routes/web.php b/routes/web.php index d3bae1e..65c0917 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,9 +1,10 @@ name('oauth2.consent'); -Route::post('/oauth2/consent', Consent::class); +Route::post('/oauth2/consent/accept', AcceptConsent::class)->name('oauth2.consent.accept'); +Route::post('/oauth2/consent/reject', RejectConsent::class)->name('oauth2.consent.reject'); require __DIR__.'/auth.php';