1 {% extends "base/class.php.twig" %}
4 \Drupal\{{module}}\Authentication\Provider\{{class}}.
7 {% block namespace_class %}
8 namespace Drupal\{{module}}\Authentication\Provider;
12 use Drupal\Core\Authentication\AuthenticationProviderInterface;
13 use Drupal\Core\Config\ConfigFactoryInterface;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
17 use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
18 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
21 {% block class_declaration %}
25 class {{ class }} implements AuthenticationProviderInterface {% endblock %}
26 {% block class_variables %}
30 * @var \Drupal\Core\Config\ConfigFactoryInterface
32 protected $configFactory;
35 * The entity type manager.
37 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39 protected $entityTypeManager;
42 {% block class_construct %}
45 * Constructs a HTTP basic authentication provider object.
47 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
49 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
50 * The entity type manager service.
52 public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
53 $this->configFactory = $config_factory;
54 $this->entityTypeManager = $entity_type_manager;
58 {% block class_create %}
61 {% block class_methods %}
64 * Checks whether suitable authentication credentials are on the request.
66 * @param \Symfony\Component\HttpFoundation\Request $request
70 * TRUE if authentication credentials suitable for this provider are on the
71 * request, FALSE otherwise.
73 public function applies(Request $request) {
74 // If you return TRUE and the method Authentication logic fails,
75 // you will get out from Drupal navigation if you are logged in.
82 public function authenticate(Request $request) {
83 $consumer_ip = $request->getClientIp();
85 if (in_array($consumer_ip, $ips)) {
86 // Return Anonymous user.
87 return $this->entityTypeManager->getStorage('user')->load(0);
90 throw new AccessDeniedHttpException();
97 public function cleanup(Request $request) {}
102 public function handleException(GetResponseForExceptionEvent $event) {
103 $exception = $event->getException();
104 if ($exception instanceof AccessDeniedHttpException) {
105 $event->setException(
106 new UnauthorizedHttpException('Invalid consumer origin.', $exception)