Skip to content

Commit 9f24cfe

Browse files
authored
Merge pull request #60 from Martincic/master
Added basic auth option
2 parents fad2c17 + 7a4bee4 commit 9f24cfe

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Laravel API to Postman
77

8-
This package allows you to automatically generate a Postman collection based on your API routes. It also provides basic configuration and support for bearer auth tokens for routes behind an auth middleware.
8+
This package allows you to automatically generate a Postman collection based on your API routes. It also provides basic configuration and support for bearer auth tokens and basic auth for routes behind an auth middleware.
99

1010
## Postman Schema
1111

@@ -47,6 +47,14 @@ The following usage will generate routes with the bearer token specified.
4747
php artisan export:postman --bearer="1|XXNKXXqJjfzG8XXSvXX1Q4pxxnkXmp8tT8TXXKXX"
4848
```
4949

50+
The following usage will generate routes with the basic auth specified.
51+
52+
```bash
53+
php artisan export:postman --basic="username:password123"
54+
```
55+
56+
If both auths are specified, bearer will be favored.
57+
5058
## Examples
5159

5260
This is with the default configuration and a bearer token passed in:

src/Commands/ExportPostmanCommand.php

+39-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
class ExportPostmanCommand extends Command
1616
{
1717
/** @var string */
18-
protected $signature = 'export:postman {--bearer= : The bearer token to use on your endpoints}';
18+
protected $signature = 'export:postman
19+
{--bearer= : The bearer token to use on your endpoints}
20+
{--basic= : The basic auth to use on your endpoints}';
1921

2022
/** @var string */
2123
protected $description = 'Automatically generate a Postman collection for your API routes';
@@ -33,7 +35,16 @@ class ExportPostmanCommand extends Command
3335
protected $filename;
3436

3537
/** @var string */
36-
private $bearer;
38+
private $token;
39+
40+
/** @var string */
41+
private $authType;
42+
43+
/** @var array */
44+
private const AUTH_OPTIONS = [
45+
'bearer',
46+
'basic',
47+
];
3748

3849
public function __construct(Router $router, Repository $config)
3950
{
@@ -46,7 +57,7 @@ public function __construct(Router $router, Repository $config)
4657
public function handle(): void
4758
{
4859
$this->setFilename();
49-
$this->setBearerToken();
60+
$this->setAuthToken();
5061
$this->initializeStructure();
5162

5263
foreach ($this->router->getRoutes() as $route) {
@@ -106,11 +117,22 @@ public function handle(): void
106117

107118
$routeHeaders = $this->config['headers'];
108119

109-
if ($this->bearer && in_array($this->config['auth_middleware'], $middlewares)) {
110-
$routeHeaders[] = [
111-
'key' => 'Authorization',
112-
'value' => 'Bearer {{token}}',
113-
];
120+
if ($this->token && in_array($this->config['auth_middleware'], $middlewares)) {
121+
switch ($this->authType) {
122+
case 'bearer':
123+
$routeHeaders[] = [
124+
'key' => 'Authorization',
125+
'value' => 'Bearer {{token}}',
126+
];
127+
break;
128+
129+
case 'basic':
130+
$routeHeaders[] = [
131+
'key' => 'Authorization',
132+
'value' => 'Basic {{token}}',
133+
];
134+
break;
135+
}
114136
}
115137

116138
$request = $this->makeRequest($route, $method, $routeHeaders, $requestRules);
@@ -267,10 +289,10 @@ protected function initializeStructure(): void
267289
'item' => [],
268290
];
269291

270-
if ($this->bearer) {
292+
if ($this->token) {
271293
$this->structure['variable'][] = [
272294
'key' => 'token',
273-
'value' => $this->bearer,
295+
'value' => $this->token,
274296
];
275297
}
276298
}
@@ -284,9 +306,14 @@ protected function setFilename()
284306
);
285307
}
286308

287-
protected function setBearerToken()
309+
protected function setAuthToken()
288310
{
289-
$this->bearer = $this->option('bearer') ?? null;
311+
foreach (self::AUTH_OPTIONS as $option) {
312+
if ($token = $this->option($option)) {
313+
$this->token = $token ?? null;
314+
$this->authType = $option;
315+
}
316+
}
290317
}
291318

292319
protected function isStructured()

tests/Feature/ExportPostmanTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,45 @@ public function test_bearer_export_works(bool $formDataEnabled)
8383
}
8484
}
8585

86+
/**
87+
* @dataProvider providerFormDataEnabled
88+
*/
89+
public function test_basic_export_works(bool $formDataEnabled)
90+
{
91+
config()->set('api-postman.enable_formdata', $formDataEnabled);
92+
93+
$this->artisan('export:postman --basic=username:password1234')->assertExitCode(0);
94+
95+
$collection = json_decode(Storage::get('postman/'.config('api-postman.filename')), true);
96+
97+
$routes = $this->app['router']->getRoutes();
98+
99+
$collectionVariables = $collection['variable'];
100+
101+
foreach ($collectionVariables as $variable) {
102+
if ($variable['key'] != 'token') {
103+
continue;
104+
}
105+
106+
$this->assertEquals($variable['value'], 'username:password1234');
107+
}
108+
109+
$this->assertCount(2, $collectionVariables);
110+
111+
$collectionItems = $collection['item'];
112+
113+
$this->assertCount(count($routes), $collectionItems);
114+
115+
foreach ($routes as $route) {
116+
$collectionRoute = Arr::first($collectionItems, function ($item) use ($route) {
117+
return $item['name'] == $route->uri();
118+
});
119+
120+
$this->assertNotNull($collectionRoute);
121+
$this->assertTrue(in_array($collectionRoute['request']['method'], $route->methods()));
122+
}
123+
}
124+
86125
/**
87126
* @dataProvider providerFormDataEnabled
88127
*/

0 commit comments

Comments
 (0)