Skip to content

Commit 0958daa

Browse files
committed
Support swagger-ui's oauth2 redirect
1 parent b87d407 commit 0958daa

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

resources/views/index.blade.php

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
@if (!is_null($data["validator_url"]))
5656
validatorUrl: '{{ $data["validator_url"] }}'
5757
@endif
58+
oauth2RedirectUrl: '{{ url("{$data['path']}/oauth2-redirect") }}',
5859
});
5960
6061
ui.initOAuth({
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace NextApps\SwaggerUi\Http\Controllers;
4+
5+
use Illuminate\Support\Facades\Http;
6+
7+
class SwaggerOAuth2RedirectController
8+
{
9+
public function __invoke() : string
10+
{
11+
return Http::get('https://unpkg.com/swagger-ui-dist@latest/oauth2-redirect.html')->body();
12+
}
13+
}

src/Http/Controllers/SwaggerViewController.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Illuminate\Http\Request;
66
use Illuminate\Support\ItemNotFoundException;
7+
use Illuminate\View\View;
78

89
class SwaggerViewController
910
{
10-
public function __invoke(Request $request)
11+
public function __invoke(Request $request) : View
1112
{
1213
try {
1314
$file = collect(config('swagger-ui.files'))->filter(function ($values) use ($request) {

src/SwaggerUiServiceProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\ServiceProvider;
77
use NextApps\SwaggerUi\Console\InstallCommand;
88
use NextApps\SwaggerUi\Http\Controllers\OpenApiJsonController;
9+
use NextApps\SwaggerUi\Http\Controllers\SwaggerOAuth2RedirectController;
910
use NextApps\SwaggerUi\Http\Controllers\SwaggerViewController;
1011

1112
class SwaggerUiServiceProvider extends ServiceProvider
@@ -40,7 +41,7 @@ protected function loadRoutes() : void
4041
Route::middleware($values['middleware'])
4142
->group(function () use ($values) {
4243
Route::get($values['path'], SwaggerViewController::class)->name($values['path'] . '.index');
43-
44+
Route::get($values['path'] . '/oauth2-redirect', SwaggerOAuth2RedirectController::class)->name($values['path'] . '.oauth2-redirect');
4445
Route::get($values['path'] . '/{filename}', OpenApiJsonController::class)->name($values['path'] . '.json');
4546
});
4647
});
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace NextApps\SwaggerUi\Tests;
4+
5+
use Illuminate\Contracts\Auth\Authenticatable;
6+
use Illuminate\Support\Facades\Gate;
7+
use Illuminate\Support\Facades\Http;
8+
use NextApps\SwaggerUi\Http\Middleware\EnsureUserIsAuthorized;
9+
use NextApps\SwaggerUi\SwaggerUiServiceProvider;
10+
11+
class SwaggerOAuth2RedirectRouteTest extends TestCase
12+
{
13+
protected function setUp() : void
14+
{
15+
parent::setUp();
16+
17+
config()->set('swagger-ui.files.0.versions', ['v1' => __DIR__ . '/testfiles/openapi.json']);
18+
config()->set('swagger-ui.files.0.oauth', ['client_id' => 1, 'client_secret' => 'foobar']);
19+
20+
Gate::define('viewSwaggerUI', fn (?Authenticatable $user) => true);
21+
}
22+
23+
protected function getPackageProviders($app) : array
24+
{
25+
return [SwaggerUiServiceProvider::class];
26+
}
27+
28+
/** @test */
29+
public function it_returns_swagger_ui_oauth2_redirect_html_file_content()
30+
{
31+
Http::fake([
32+
'https://unpkg.com/swagger-ui-dist@latest/oauth2-redirect.html' => Http::response('foo'),
33+
]);
34+
35+
$this->get('swagger/oauth2-redirect')
36+
->assertStatus(200)
37+
->assertSeeText('foo');
38+
39+
Http::assertSentCount(1);
40+
}
41+
}

0 commit comments

Comments
 (0)