Skip to content

Commit bedbdab

Browse files
author
Vignesh C
committed
added jwt auth to the routes
1 parent 826d2d9 commit bedbdab

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/GenerateSwaggerDoc.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class GenerateSwaggerDoc extends Command
1313
*/
1414
protected $signature = 'laravel-swagger:generate
1515
{--format=json : The format of the output, current options are json and yaml}
16-
{--filter= : Filter to a specific route prefix, such as /api or /v2/api}';
16+
{--filter= : Filter to a specific route prefix, such as /api or /v2/api}
17+
{--auth= : Authentication to be applied globally}';
1718

1819
/**
1920
* The console command description.
@@ -31,7 +32,7 @@ public function handle()
3132
{
3233
$config = config('laravel-swagger');
3334

34-
$docs = (new Generator($config, $this->option('filter') ?: null))->generate();
35+
$docs = (new Generator($config, $this->option('filter') ?: null, $this->option('auth') ?: null))->generate();
3536

3637
$formattedDocs = (new FormatterManager($docs))
3738
->setFormat($this->option('format'))

src/Generator.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Generator
1313

1414
protected $routeFilter;
1515

16+
protected $auth;
17+
1618
protected $docs;
1719

1820
protected $uri;
@@ -23,10 +25,11 @@ class Generator
2325

2426
protected $action;
2527

26-
public function __construct($config, $routeFilter = null)
28+
public function __construct($config, $routeFilter = null, $auth = null)
2729
{
2830
$this->config = $config;
2931
$this->routeFilter = $routeFilter;
32+
$this->auth = $auth;
3033
}
3134

3235
public function generate()
@@ -39,7 +42,7 @@ public function generate()
3942

4043
if ($this->routeFilter && !preg_match('/^' . preg_quote($this->routeFilter, '/') . '/', $this->uri)) {
4144
continue;
42-
}
45+
}
4346

4447
$this->action = $route->getAction('uses');
4548
$methods = $route->methods();
@@ -48,6 +51,8 @@ public function generate()
4851
$this->docs['paths'][$this->uri] = [];
4952
}
5053

54+
$this->addAuthParameters($route->middleware());
55+
5156
foreach ($methods as $method) {
5257
$this->method = strtolower($method);
5358

@@ -85,6 +90,24 @@ protected function getBaseInfo()
8590
$baseInfo['produces'] = $this->config['produces'];
8691
}
8792

93+
if(!empty($this->auth)) {
94+
switch ($this->auth) {
95+
case 'jwt':
96+
$baseInfo['securityDefinitions'] = [
97+
'api_key' => [
98+
'type' => 'apiKey',
99+
'name' => 'Authorization',
100+
'in' => 'header'
101+
]
102+
];
103+
break;
104+
105+
default:
106+
$baseInfo['securityDefinitions'] = [];
107+
break;
108+
}
109+
}
110+
88111
$baseInfo['paths'] = [];
89112

90113
return $baseInfo;
@@ -156,6 +179,24 @@ protected function getFormRules()
156179
}
157180
}
158181

182+
protected function addAuthParameters($middlewares)
183+
{
184+
if(!empty($this->auth)) {
185+
switch ($this->auth) {
186+
case 'jwt':
187+
$hasAuth = array_filter($middlewares, function ($var) {
188+
return (strpos($var, 'jwt') > -1);
189+
});
190+
if($hasAuth) {
191+
$this->docs['paths']['security'] = [
192+
'api_key' => []
193+
];
194+
}
195+
break;
196+
}
197+
}
198+
}
199+
159200
protected function getParameterGenerator($rules)
160201
{
161202
switch ($this->method) {

0 commit comments

Comments
 (0)