Skip to content

Commit

Permalink
Merge pull request #15 from aspirepress/random-api-token
Browse files Browse the repository at this point in the history
Adding random generation of an API token for the repository
  • Loading branch information
sarah-savage authored Oct 14, 2024
2 parents a07dac6 + 9c282e2 commit c5d0b4f
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"laminas/laminas-servicemanager": "^3.4",
"laminas/laminas-stdlib": "^3.6",
"mezzio/mezzio": "^3.7",
"mezzio/mezzio-cors": "^1.12",
"mezzio/mezzio-fastroute": "^3.0.3",
"mezzio/mezzio-helpers": "^5.7",
"monolog/monolog": "^3.2",
Expand Down
74 changes: 72 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions config/autoload/cors.global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Mezzio\Cors\Configuration\ConfigurationInterface;

return [
ConfigurationInterface::CONFIGURATION_IDENTIFIER => [
'allowed_origins' => ['*'],
],
];
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
];

$aggregator = new ConfigAggregator([
\Mezzio\Cors\ConfigProvider::class,
\Mezzio\ConfigProvider::class,
\Mezzio\Tooling\ConfigProvider::class,
\Mezzio\Helper\ConfigProvider::class,
Expand Down
3 changes: 3 additions & 0 deletions config/pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Laminas\Stratigility\Middleware\ErrorHandler;
use Mezzio\Application;
use Mezzio\Cors\Middleware\CorsMiddleware;
use Mezzio\Handler\NotFoundHandler;
use Mezzio\Helper\BodyParams\BodyParamsMiddleware;
use Mezzio\Helper\ServerUrlMiddleware;
Expand Down Expand Up @@ -44,6 +45,8 @@
// - $app->pipe('/docs', $apiDocMiddleware);
// - $app->pipe('/files', $filesMiddleware);

$app->pipe(CorsMiddleware::class);

// Register the routing middleware in the middleware pipeline.
// This middleware registers the Mezzio\Router\RouteResult request attribute.
$app->pipe(RouteMiddleware::class);
Expand Down
2 changes: 2 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use AspirePress\AspireCloud\Repository\Api\V1\ApiTokenIssuanceHandler;
use AspirePress\AspireCloud\V1\CatchAll\Handlers\CatchAllHandler;
use Mezzio\Application;
use Mezzio\MiddlewareFactory;
Expand All @@ -26,5 +27,6 @@
*/

return static function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
$app->post('/repository/api/v1/apitoken', ApiTokenIssuanceHandler::class, 'repository.api.v1.apitoken');
$app->route('/{path:.*}', CatchAllHandler::class, ['GET', 'POST'], 'app.home');
};
4 changes: 4 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use AspirePress\AspireCloud\Data\Factories\PluginRepositoryFactory;
use AspirePress\AspireCloud\Data\Repositories\PluginRepository;
use AspirePress\AspireCloud\Repository\Api\V1\ApiTokenIssuanceHandler;
use Aura\Sql\ExtendedPdoInterface;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Laminas\Stratigility\Middleware\ErrorHandler;
Expand All @@ -20,6 +21,9 @@ public function __invoke(): array
{
return [
'dependencies' => [
'invokables' => [
ApiTokenIssuanceHandler::class => ApiTokenIssuanceHandler::class,
],
'delegators' => [
ErrorHandler::class => [LoggingListenerDelegatorFactory::class],
],
Expand Down
18 changes: 18 additions & 0 deletions src/Repository/Api/V1/ApiTokenIssuanceHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace AspirePress\AspireCloud\Repository\Api\V1;

use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ApiTokenIssuanceHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse(['api_key' => bin2hex(random_bytes(16))]);
}
}

0 comments on commit c5d0b4f

Please sign in to comment.