Skip to content

Instantly share code, notes, and snippets.

@damiankloip
Last active August 29, 2015 14:23
Show Gist options
  • Save damiankloip/b41b920abebb131ca3c6 to your computer and use it in GitHub Desktop.
Save damiankloip/b41b920abebb131ca3c6 to your computer and use it in GitHub Desktop.
Auth collector
diff --git a/core/core.services.yml b/core/core.services.yml
index 5a9cd6e..d89ebc7 100644
--- a/core/core.services.yml
+++ b/core/core.services.yml
@@ -1226,6 +1226,9 @@ services:
- [setThemeManager, ['@theme.manager']]
authentication:
class: Drupal\Core\Authentication\AuthenticationManager
+ arguments: ['@authentication_collector']
+ authentication_collector:
+ class: Drupal\Core\Authentication\AuthenticationCollector
tags:
- { name: service_collector, tag: authentication_provider, call: addProvider }
authentication_subscriber:
diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationCollector.php b/core/lib/Drupal/Core/Authentication/AuthenticationCollector.php
new file mode 100644
index 0000000..3412c5a
--- /dev/null
+++ b/core/lib/Drupal/Core/Authentication/AuthenticationCollector.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Drupal\Core\Authentication;
+
+use Drupal\Core\Authentication\AuthenticationProviderInterface;
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * A collector class for authentication providers.
+ */
+class AuthenticationCollector implements AuthenticationCollectorInterface {
+
+ /**
+ * Array of all registered authentication providers, keyed by ID.
+ *
+ * @var \Drupal\Core\Authentication\AuthenticationProviderInterface[]
+ */
+ protected $providers;
+
+ /**
+ * Array of all providers and their priority.
+ *
+ * @var array
+ */
+ protected $providerOrders = [];
+
+ /**
+ * Sorted list of registered providers.
+ *
+ * @var \Drupal\Core\Authentication\AuthenticationProviderInterface[]
+ */
+ protected $sortedProviders;
+
+ /**
+ * List of providers which are allowed on routes with no _auth option.
+ *
+ * @var string[]
+ */
+ protected $globalProviders;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function addProvider(AuthenticationProviderInterface $provider, $provider_id, $priority = 0, $global = FALSE) {
+ $this->providers[$provider_id] = $provider;
+ $this->providerOrders[$priority][$provider_id] = $provider;
+ // Force the builders to be re-sorted.
+ $this->sortedProviders = NULL;
+
+ if ($global) {
+ $this->globalProviders[$provider_id] = TRUE;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function isGlobal($provider_id) {
+ return isset($this->globalProviders[$provider_id]);
+ }
+
+ /**
+ * Returns the id of the authentication provider for a request.
+ *
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * The incoming request.
+ *
+ * @return string|NULL
+ * The id of the first authentication provider which applies to the request.
+ * If no application detects appropriate credentials, then NULL is returned.
+ */
+ public function getProvider($provider_id) {
+ return isset($this->providers[$provider_id]) ? $this->providers[$provider_id] : NULL;
+ }
+
+ /**
+ * Returns the sorted array of authentication providers.
+ *
+ * @todo Replace with a list of providers sorted during compile time in
+ * https://www.drupal.org/node/2432585.
+ *
+ * @return \Drupal\Core\Authentication\AuthenticationProviderInterface[]
+ * An array of authentication provider objects.
+ */
+ public function getSortedProviders() {
+ if (!isset($this->sortedProviders)) {
+ // Sort the builders according to priority.
+ krsort($this->providerOrders);
+
+ // Merge nested providers from $this->providers into $this->sortedProviders.
+ $this->sortedProviders = [];
+ foreach ($this->providerOrders as $providers) {
+ $this->sortedProviders = array_merge($this->sortedProviders, $providers);
+ }
+ }
+
+ return $this->sortedProviders;
+ }
+}
diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationCollectorInterface.php b/core/lib/Drupal/Core/Authentication/AuthenticationCollectorInterface.php
new file mode 100644
index 0000000..fc2170e
--- /dev/null
+++ b/core/lib/Drupal/Core/Authentication/AuthenticationCollectorInterface.php
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Authentication\AuthenticationCollectorInterface
+ */
+
+namespace Drupal\Core\Authentication;
+
+/**
+ * A collector class for authentication providers.
+ */
+interface AuthenticationCollectorInterface {
+
+ /**
+ * Adds a provider to the array of registered providers.
+ *
+ * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $provider
+ * The provider object.
+ * @param string $provider_id
+ * Identifier of the provider.
+ * @param int $priority
+ * (optional) The provider's priority.
+ * @param bool $global
+ * (optional) TRUE if the provider is to be applied globally on all routes.
+ * Defaults to FALSE.
+ */
+ public function addProvider(AuthenticationProviderInterface $provider, $provider_id, $priority = 0, $global = FALSE);
+
+ /**
+ * @todo
+ */
+ public function isGlobal($provider_id);
+
+ /**
+ * Returns an authentication provider.
+ *
+ * @param string $provider_id
+ * THe provider ID.
+ *
+ * @return \Drupal\Core\Authentication\AuthenticationProviderInterface|NULL
+ * The authentication provider which matches the ID.
+ */
+ public function getProvider($provider_id);
+
+ /**
+ * Returns the sorted array of authentication providers.
+ *
+ * @todo Replace with a list of providers sorted during compile time in
+ * https://www.drupal.org/node/2432585.
+ *
+ * @return \Drupal\Core\Authentication\AuthenticationProviderInterface[]
+ * An array of authentication provider objects.
+ */
+ public function getSortedProviders();
+}
diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php
index ac50ae3..455e5a6 100644
--- a/core/lib/Drupal/Core/Authentication/AuthenticationManager.php
+++ b/core/lib/Drupal/Core/Authentication/AuthenticationManager.php
@@ -58,30 +58,20 @@ class AuthenticationManager implements AuthenticationProviderInterface, Authenti
protected $challengers;
/**
- * List of providers which are allowed on routes with no _auth option.
- *
- * @var string[]
+ * @var \Drupal\Core\Authentication\AuthenticationCollectorInterface
*/
- protected $globalProviders;
+ protected $authCollector;
- /**
- * {@inheritdoc}
- */
- public function addProvider(AuthenticationProviderInterface $provider, $provider_id, $priority = 0, $global = FALSE) {
- $this->providers[$provider_id] = $provider;
- $this->providerOrders[$priority][$provider_id] = $provider;
- // Force the builders to be re-sorted.
- $this->sortedProviders = NULL;
-
- if ($provider instanceof AuthenticationProviderFilterInterface) {
- $this->filters[$provider_id] = $provider;
- }
- if ($provider instanceof AuthenticationProviderChallengeInterface) {
- $this->challengers[$provider_id] = $provider;
- }
+ public function __construct(AuthenticationCollectorInterface $auth_collector) {
+ $this->authCollector = $auth_collector;
- if ($global) {
- $this->globalProviders[$provider_id] = TRUE;
+ foreach ($this->authCollector->getSortedProviders() as $provider_id => $provider) {
+ if ($provider instanceof AuthenticationProviderFilterInterface) {
+ $this->filters[$provider_id] = $provider;
+ }
+ if ($provider instanceof AuthenticationProviderChallengeInterface) {
+ $this->challengers[$provider_id] = $provider;
+ }
}
}
@@ -97,7 +87,8 @@ public function applies(Request $request) {
*/
public function authenticate(Request $request) {
$provider_id = $this->getProvider($request);
- return $this->providers[$provider_id]->authenticate($request);
+ $provider = $this->authCollector->getProvider($provider_id);
+ return $provider->authenticate($request);
}
/**
@@ -110,7 +101,7 @@ public function appliesToRoutedRequest(Request $request, $authenticated) {
$result = $this->applyFilter($request, $authenticated, $this->getProvider($request));
}
else {
- foreach ($this->getSortedProviders() as $provider_id => $provider) {
+ foreach ($this->authCollector->getSortedProviders() as $provider_id => $provider) {
if ($this->applyFilter($request, $authenticated, $provider_id)) {
$result = TRUE;
break;
@@ -142,7 +133,7 @@ public function challengeException(Request $request, \Exception $previous) {
* If no application detects appropriate credentials, then NULL is returned.
*/
protected function getProvider(Request $request) {
- foreach ($this->getSortedProviders() as $provider_id => $provider) {
+ foreach ($this->authCollector->getSortedProviders() as $provider_id => $provider) {
if ($provider->applies($request)) {
return $provider_id;
}
@@ -161,7 +152,7 @@ protected function getProvider(Request $request) {
*/
protected function getChallenger(Request $request) {
if (!empty($this->challengers)) {
- foreach ($this->getSortedProviders($request, FALSE) as $provider_id => $provider) {
+ foreach ($this->authCollector->getSortedProviders() as $provider_id => $provider) {
if (isset($this->challengers[$provider_id]) && !$provider->applies($request) && $this->applyFilter($request, FALSE, $provider_id)) {
return $provider_id;
}
@@ -222,27 +213,8 @@ protected function defaultFilter(Request $request, $provider_id) {
return in_array($provider_id, $route->getOption('_auth'));
}
else {
- return isset($this->globalProviders[$provider_id]);
- }
- }
-
- /**
- * Returns the sorted array of authentication providers.
- *
- * @return \Drupal\Core\Authentication\AuthenticationProviderInterface[]
- * An array of authentication provider objects.
- */
- protected function getSortedProviders() {
- if (!isset($this->sortedProviders)) {
- // Sort the builders according to priority.
- krsort($this->providerOrders);
- // Merge nested providers from $this->providers into $this->sortedProviders.
- $this->sortedProviders = array();
- foreach ($this->providerOrders as $providers) {
- $this->sortedProviders = array_merge($this->sortedProviders, $providers);
- }
+ return $this->authCollector->isGlobal($provider_id);
}
- return $this->sortedProviders;
}
}
diff --git a/core/lib/Drupal/Core/Authentication/AuthenticationManagerInterface.php b/core/lib/Drupal/Core/Authentication/AuthenticationManagerInterface.php
index 8477fc3..1ca28b9 100644
--- a/core/lib/Drupal/Core/Authentication/AuthenticationManagerInterface.php
+++ b/core/lib/Drupal/Core/Authentication/AuthenticationManagerInterface.php
@@ -12,19 +12,4 @@
*/
interface AuthenticationManagerInterface {
- /**
- * Adds a provider to the array of registered providers.
- *
- * @param \Drupal\Core\Authentication\AuthenticationProviderInterface $provider
- * The provider object.
- * @param string $provider_id
- * Identifier of the provider.
- * @param int $priority
- * (optional) The provider's priority.
- * @param bool $global
- * (optional) TRUE if the provider is to be applied globally on all routes.
- * Defaults to FALSE.
- */
- public function addProvider(AuthenticationProviderInterface $provider, $provider_id, $priority = 0, $global = FALSE);
-
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment