Dafür machst du am besten einen eigenen Eintrag in der Symfony Firewall. Das einfachste Beispiel dazu wäre eine HTTP Basic Authentication, da das keinen zusätzlichen Code erfordert (für einen User Provider) - wenn es ok ist, dass Benutzername & Passwort direkt in PHP hardgecodet ist. In der Contao Managed Edition kannst du die Firewall über ein Contao Manager Plugin anpassen.
Beispiel inklusive Route Definition, Controller & Contao Manager Plugin:
PHP-Code:
# config/routes.yml
app.controller:
resource: ../src/Controller
type: annotation
PHP-Code:
// src/Controller/Api/ExampleApiController.php
namespace App\Controller\Api;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api/example",
* name=ExampleApiController::class,
* methods="GET",
* condition="request.headers.get('Content-Type') matches '|application/json|i'"
* )
*/
class ExampleApiController
{
public function __invoke(Request $request): Response
{
return new JsonResponse('Hello World!');
}
}
PHP-Code:
// src/ContaoManager/Plugin.php
namespace App\ContaoManager;
use Contao\ManagerPlugin\Config\ContainerBuilder;
use Contao\ManagerPlugin\Config\ExtensionPluginInterface;
use Symfony\Component\Security\Core\User\User;
/**
* Plugin for the Contao Manager.
*/
class Plugin implements ExtensionPluginInterface
{
public function getExtensionConfig($extensionName, array $extensionConfigs, ContainerBuilder $container)
{
if ('security' !== $extensionName) {
return $extensionConfigs;
}
foreach ($extensionConfigs as &$extensionConfig) {
if (isset($extensionConfig['firewalls'])) {
// Set the password encoder for our user
$extensionConfig['encoders'][User::class] = 'auto';
// Define a new in-memory user provider
$extensionConfig['providers']['app.api_user_provider'] = [
'memory' => [
'users' => [
'api' => ['password' => '…'],
],
],
];
// Insert a new firewall entry before Contao's for routes starting with /api/* and use our user provider
$offset = (int) array_search('contao_frontend', array_keys($extensionConfig['firewalls']), true);
$extensionConfig['firewalls'] = array_merge(
\array_slice($extensionConfig['firewalls'], 0, $offset, true), [
'app_api' => [
'pattern' => '/api/*',
'http_basic' => ['realm' => 'API'],
'provider' => 'app.api_user_provider',
'stateless' => true,
],
],
\array_slice($extensionConfig['firewalls'], $offset, null, true)
);
break;
}
}
return $extensionConfigs;
}
}
Dieses Contao Manager Plugin fügt vor der Contao Firewall einen weiteren Eintrag für alle Routes ein, die mit /api/ beginnen. Für diese Routes muss man sich dann über HTTP Basic Authentication mit Benutzername und Passwort authentifizieren.
Nach anlegen der Dateien (im speziellen des Contao Manager Plugins) composer install ausführen.
Benutzername und Passwort ist in dieser Zeile festgelegt, wobei das Passwort verschlüsselt sein muss:
PHP-Code:
'api' => ['password' => '…'],
Das verschlüsselte Passwort kannst du dir auf der Konsole folgendermaßen erzeugen lassen: vendor/bin/contao-console security:encode-password
Symfony fragt dich danach, für welche User Klasse du ein Passwort erzeugen möchtest. Hier wählst du in diesem Fall Symfony\Component\Security\Core\User\User (nicht Contao\User), da wir diese Klasse in unserem Firewall Eintrag definiert haben.
PHP-Code:
$ vendor/bin/contao-console security:encode-password
Symfony Password Encoder Utility
================================
For which user class would you like to encode a password? [Contao\User]:
[0] Contao\User
[1] Symfony\Component\Security\Core\User\User
> 1
1
Type in your password to be encoded:
>
------------------ ---------------------------------------------------------------------------------------------------
Key Value
------------------ ---------------------------------------------------------------------------------------------------
Encoder used Symfony\Component\Security\Core\Encoder\MigratingPasswordEncoder
Encoded password $argon2id$v=19$m=65536,t=4,p=1$ckZUelBJSXhEdHNxTTNlZw$6xy6DoIAR624uN7RWWlGh3JvJUU7GGEPjGTUh0hSj4Q
------------------ ---------------------------------------------------------------------------------------------------
! [NOTE] Self-salting encoder used: the encoder generated its own built-in salt.
[OK] Password encoding succeeded
Das Encoded password kopierst du dir dann und fügst es im Contao Manager Plugin an der entsprechenden Stelle ein.
Lesezeichen