Skip to content

Commit

Permalink
HTTP auth available on docu sites!
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Janda committed Sep 21, 2016
1 parent 46ad028 commit e3f6c67
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/DI/ApiDocuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class ApiDocuExtension extends Nette\DI\CompilerExtension
{

private $defaults = [
'apiDir' => '%wwwDir%/api'
'apiDir' => '%wwwDir%/api',
'httpAuth' => [
'user' => NULL,
'password' => NULL
]
];

/**
Expand Down Expand Up @@ -42,7 +46,7 @@ public function beforeCompile()

$builder->addDefinition($this->prefix('generator'))
->setClass('Ublaboo\ApiDocu\Generator')
->setArguments([$config['apiDir']]);
->setArguments([$config['apiDir'], $config['httpAuth']]);

$builder->addDefinition($this->prefix('starter'))
->setClass('Ublaboo\ApiDocu\Starter')
Expand Down
42 changes: 38 additions & 4 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,26 @@ class Generator extends Nette\Object
*/
private $api_dir;

/**
* @var array
*/
private $httpAuth;


/**
* @param string $api_dir
* @param array $httpAuth
* @param Nette\Application\UI\ITemplateFactory $templateFactory
* @param Nette\Http\Request $httpRequest
*/
public function __construct(
$api_dir,
array $httpAuth,
Nette\Application\UI\ITemplateFactory $templateFactory,
Nette\Http\Request $httpRequest
) {
$this->api_dir = $api_dir;
$this->httpAuth = $httpAuth;
$this->templateFactory = $templateFactory;
$this->httpRequest = $httpRequest;
}
Expand All @@ -62,12 +70,12 @@ public function generateAll(IRouter $router)
}

/**
* Create index.html
* Create index.php
*/
$this->generateIndex($sections);

/**
* Create *.html for each defined ApiRoute
* Create *.php for each defined ApiRoute
*/
foreach ($sections as $section_name => $routes) {
if (is_array($routes)) {
Expand Down Expand Up @@ -121,7 +129,10 @@ public function generateOne(ApiRoute $route, $sections, $file_name)
'sections' => $sections
]);

file_put_contents("{$this->api_dir}/{$file_name}.html", (string) $template);
file_put_contents(
"{$this->api_dir}/{$file_name}.php",
$this->getHttpAuthSnippet() . $template
);
}


Expand All @@ -137,7 +148,10 @@ public function generateIndex($sections)
'sections' => $sections
]);

file_put_contents("{$this->api_dir}/index.html", (string) $template);
file_put_contents(
"{$this->api_dir}/index.php",
$this->getHttpAuthSnippet() . $template
);
}


Expand Down Expand Up @@ -253,4 +267,24 @@ private function getApiRoutesFromIterator(\IteratorAggregate $i)
return $return;
}


/**
* @return string
*/
private function getHttpAuthSnippet()
{
if (!$this->httpAuth['user'] || !$this->httpAuth['password']) {
return '';
}

$u = $this->httpAuth['user'];
$p = $this->httpAuth['password'];

return "<?php if (!isset(\$_SERVER['PHP_AUTH_USER']) || \$_SERVER['PHP_AUTH_USER'] !== '{$u}' || \$_SERVER['PHP_AUTH_PW'] !== '{$p}') {"
. " header('WWW-Authenticate: Basic realm=\"Api\"');"
. " header('HTTP/1.0 401 Unauthorized');"
. " die('Invalid authentication');"
. "} ?>";
}

}
2 changes: 1 addition & 1 deletion src/templates/api_docu_routes_list.latte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</div>

{define route}
<a href="{ifset $section_name}{$section_name}.{/}{$file_name}.html" class="apiDocu-url">
<a href="{ifset $section_name}{$section_name}.{/}{$file_name}.php" class="apiDocu-url">
{$route->getPath()|routeMaskStyles|noescape}

<div class="apiDocu-url-method">{$route->getMethods()|implode:', '}</div>
Expand Down

0 comments on commit e3f6c67

Please sign in to comment.