Skip to content

Commit

Permalink
Support swagger-ui's oauth2 redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
gdebrauwer committed Jul 9, 2024
1 parent b87d407 commit 0958daa
Showing 5 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@
@if (!is_null($data["validator_url"]))
validatorUrl: '{{ $data["validator_url"] }}'
@endif
oauth2RedirectUrl: '{{ url("{$data['path']}/oauth2-redirect") }}',
});
ui.initOAuth({
13 changes: 13 additions & 0 deletions src/Http/Controllers/SwaggerOAuth2RedirectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace NextApps\SwaggerUi\Http\Controllers;

use Illuminate\Support\Facades\Http;

class SwaggerOAuth2RedirectController
{
public function __invoke() : string
{
return Http::get('https://unpkg.com/swagger-ui-dist@latest/oauth2-redirect.html')->body();
}
}
3 changes: 2 additions & 1 deletion src/Http/Controllers/SwaggerViewController.php
Original file line number Diff line number Diff line change
@@ -4,10 +4,11 @@

use Illuminate\Http\Request;
use Illuminate\Support\ItemNotFoundException;
use Illuminate\View\View;

class SwaggerViewController
{
public function __invoke(Request $request)
public function __invoke(Request $request) : View
{
try {
$file = collect(config('swagger-ui.files'))->filter(function ($values) use ($request) {
3 changes: 2 additions & 1 deletion src/SwaggerUiServiceProvider.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
use Illuminate\Support\ServiceProvider;
use NextApps\SwaggerUi\Console\InstallCommand;
use NextApps\SwaggerUi\Http\Controllers\OpenApiJsonController;
use NextApps\SwaggerUi\Http\Controllers\SwaggerOAuth2RedirectController;
use NextApps\SwaggerUi\Http\Controllers\SwaggerViewController;

class SwaggerUiServiceProvider extends ServiceProvider
@@ -40,7 +41,7 @@ protected function loadRoutes() : void
Route::middleware($values['middleware'])
->group(function () use ($values) {
Route::get($values['path'], SwaggerViewController::class)->name($values['path'] . '.index');

Route::get($values['path'] . '/oauth2-redirect', SwaggerOAuth2RedirectController::class)->name($values['path'] . '.oauth2-redirect');
Route::get($values['path'] . '/{filename}', OpenApiJsonController::class)->name($values['path'] . '.json');
});
});
41 changes: 41 additions & 0 deletions tests/SwaggerOAuth2RedirectRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace NextApps\SwaggerUi\Tests;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Http;
use NextApps\SwaggerUi\Http\Middleware\EnsureUserIsAuthorized;
use NextApps\SwaggerUi\SwaggerUiServiceProvider;

class SwaggerOAuth2RedirectRouteTest extends TestCase
{
protected function setUp() : void
{
parent::setUp();

config()->set('swagger-ui.files.0.versions', ['v1' => __DIR__ . '/testfiles/openapi.json']);
config()->set('swagger-ui.files.0.oauth', ['client_id' => 1, 'client_secret' => 'foobar']);

Gate::define('viewSwaggerUI', fn (?Authenticatable $user) => true);
}

protected function getPackageProviders($app) : array
{
return [SwaggerUiServiceProvider::class];
}

/** @test */
public function it_returns_swagger_ui_oauth2_redirect_html_file_content()
{
Http::fake([
'https://unpkg.com/swagger-ui-dist@latest/oauth2-redirect.html' => Http::response('foo'),
]);

$this->get('swagger/oauth2-redirect')
->assertStatus(200)
->assertSeeText('foo');

Http::assertSentCount(1);
}
}

0 comments on commit 0958daa

Please sign in to comment.