Protecting Your Drupal 8 Resources
Article
Drupal 8 incorporates a Modular Authentication System which, given a request, attempts to identify a Drupal user by inspecting the HTTP request headers.
Authentication comes in handy when we want to restrict access to a resource in Drupal. It can be applied to any route, although the method to implement it may differ. It is most commonly used to identify requests when we are exposing data through an API from our Drupal site.
Authentication and Authorization
Imagine you are going through airport security. The security agent asks to see your ID – a passport or driver’s license, say. The act of showing your ID is what we call Authentication. In Drupal – as in almost all websites – your authentication credentials are your username and password.
Next, the security agent checks your boarding pass to verify that you are in the right place and have clearance to get on a plane. That’s called Authorization. In Drupal your role (and therefore the permissions assigned to that role) are your Authorization credentials.
To summarize: authentication means who are you?; authorization means may you proceed?.
Enjoy your flight!
Authentication in Drupal 8
In Drupal 8, Authorization is handled by the Access System and won't be covered in this article; there is an internal system to handle Authentication, so let's start with the following statement:
Thanks to the Modular Authentication System, different Authentication Providers may extract a $user
out of a given $request
object.
There are a few keywords in that statement. Let's dissect them briefly: