<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdessamad MOUHASSINE</title>
    <description>The latest articles on DEV Community by Abdessamad MOUHASSINE (@absolux).</description>
    <link>https://dev.to/absolux</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F80019%2F1bc969c7-6fff-43c3-9207-ddf2187f6d12.png</url>
      <title>DEV Community: Abdessamad MOUHASSINE</title>
      <link>https://dev.to/absolux</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/absolux"/>
    <language>en</language>
    <item>
      <title>From Scratch : User Authentication (Part 2)</title>
      <dc:creator>Abdessamad MOUHASSINE</dc:creator>
      <pubDate>Fri, 09 Nov 2018 16:58:32 +0000</pubDate>
      <link>https://dev.to/absolux/from-scratch--user-authentication-part-2-21mc</link>
      <guid>https://dev.to/absolux/from-scratch--user-authentication-part-2-21mc</guid>
      <description>&lt;p&gt;In my previous &lt;a href="https://dev.to/absolux/from-scratch-user-authentication-37a7"&gt;article&lt;/a&gt;, I've exposed user identification solutions, and how we can make it simple and clean. In this article, I'm just going to talk briefly about the next step after a successful authentication, which is called : &lt;code&gt;User authorization&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To summarize, user authentication is the process to check and retrieve the user object based on its credentials, as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                      ++++++++++++++++++
                      +                +
    Credentials ---&amp;gt;  + Authentication +  ---&amp;gt; User?
                      +                +
                      ++++++++++++++++++
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;But, what should happen after a user has been successfully identified ?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In most cases, we have to check the user's ability to access the resource and reject the incoming request, with a &lt;code&gt;403 Forbidden&lt;/code&gt; error, if not authorized.&lt;/p&gt;

&lt;p&gt;I don't have yet a clear vision about the implementation, but it will follow the same philosophy as authentication, where the &lt;code&gt;boolean&lt;/code&gt; result indicates whether or not the the user is granted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;               +++++++++++++++++
               +               +
    User ---&amp;gt;  + Authorization +  ---&amp;gt; boolean
               +               +
               +++++++++++++++++
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'm open to any suggestion, idea or article, on how to make the implementation as clean as possible. So, don't hesitate to share it in comments.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

</description>
      <category>fromscratch</category>
      <category>authentication</category>
      <category>authorization</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>From Scratch: User Authentication</title>
      <dc:creator>Abdessamad MOUHASSINE</dc:creator>
      <pubDate>Sat, 13 Oct 2018 12:38:04 +0000</pubDate>
      <link>https://dev.to/absolux/from-scratch-user-authentication-37a7</link>
      <guid>https://dev.to/absolux/from-scratch-user-authentication-37a7</guid>
      <description>&lt;h2&gt;
  
  
  Definitions
&lt;/h2&gt;

&lt;p&gt;User Authentication is loosely defined as identifying the user based on his credentials. In other words, it provides access control for systems, by checking to see if the user's credentials match those saved in a database, in a data authentication server, or anywhere else.&lt;/p&gt;

&lt;p&gt;The credentials, in the other hand, could be anything: an identifier, password, pin numbers, certificate, fingerprint, retina, voice. I mean anything that differentiate a user from another.&lt;/p&gt;

&lt;p&gt;You can get more information about the authentication, its types and factors on this wonderful &lt;a href="https://searchsecurity.techtarget.com/definition/authentication"&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;p&gt;Developers and frameworks provide different solutions and visions, simple and complex, to manage user identification.&lt;/p&gt;

&lt;p&gt;Let see some examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  Zend Framework 2
&lt;/h3&gt;

&lt;p&gt;Zend framework provides a dedicated module &lt;a href="https://docs.zendframework.com/zend-authentication/"&gt;zend-authentication&lt;/a&gt;, which includes &lt;code&gt;adapters&lt;/code&gt; for different authentication methods, and an &lt;code&gt;AuthenticationService&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Zend\Authentication\AuthenticationService&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Instantiate the authentication service&lt;/span&gt;
&lt;span class="nv"&gt;$auth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AuthenticationService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Instantiate a dummy authentication adapter&lt;/span&gt;
&lt;span class="nv"&gt;$authAdapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Adapter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Attempt authentication, saving the result&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$auth&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$authAdapter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After a successful authentication attempt, subsequent requests can query the authentication service to determine if an identity is present, and, if so, retrieve it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$auth&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;hasIdentity&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Identity exists; get it&lt;/span&gt;
  &lt;span class="nv"&gt;$identity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$auth&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;getIdentity&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Laravel 5
&lt;/h3&gt;

&lt;p&gt;Laravel is a huge framework in fact, offering also different solutions and tools to handle authentication.&lt;/p&gt;

&lt;p&gt;Laravel's authentication services could be accessed via the &lt;code&gt;Auth&lt;/code&gt; &lt;a href="https://laravel.com/docs/5.6/facades"&gt;facade&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Support\Facades\Auth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;attempt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$credentials&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Authentication passed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Authentication failed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;attempt&lt;/code&gt; method accepts an array of key/value pairs as its first argument, and will return a &lt;code&gt;boolean&lt;/code&gt; if the user is found in the database using the values in the array.&lt;/p&gt;

&lt;p&gt;The authenticated user can be retrieved again via the &lt;code&gt;Auth&lt;/code&gt; facade.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Illuminate\Support\Facades\Auth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Get the currently authenticated user...&lt;/span&gt;
&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Get the currently authenticated user's ID...&lt;/span&gt;
&lt;span class="nv"&gt;$id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Auth&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Symfony 4
&lt;/h3&gt;

&lt;p&gt;The Symfony's solution is quite complicated. The &lt;code&gt;security&lt;/code&gt; module provides multiple classes and interfaces to handle authentication.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Symfony\Component\Security\Core\Exception\AuthenticationException&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// instances of Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface&lt;/span&gt;
&lt;span class="nv"&gt;$providers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Instantiate the authentication manager&lt;/span&gt;
&lt;span class="nv"&gt;$authenticationManager&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AuthenticationProviderManager&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$providers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// create a token, containing the user's credentials&lt;/span&gt;
&lt;span class="nv"&gt;$unauthenticatedToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;UsernamePasswordToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$providerKey&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// validate the given token, and return an authenticated token&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$authenticatedToken&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$authenticationManager&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$unauthenticatedToken&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AuthenticationException&lt;/span&gt; &lt;span class="nv"&gt;$exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// authentication failed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After authentication, the User object of the current user can be accessed via the &lt;code&gt;getUser()&lt;/code&gt; from inside a controller, this will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or via the &lt;code&gt;Security&lt;/code&gt; class, available from version 3.4:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;Symfony\Component\Security\Core\Security&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;indexAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Security&lt;/span&gt; &lt;span class="nv"&gt;$security&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$security&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Django 2
&lt;/h3&gt;

&lt;p&gt;Django uses, by default, sessions and middlewares to hook the authentication system into request objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.contrib.auth&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;authenticate&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'john'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'secret'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# A backend authenticated the credentials
&lt;/span&gt;  &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# No backend authenticated the credentials
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The user, authenticated or not, is always accessible within the &lt;code&gt;request&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_authenticated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# Do something for authenticated users.
&lt;/span&gt;  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# Do something for anonymous users.
&lt;/span&gt;  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Passport.js
&lt;/h3&gt;

&lt;p&gt;Authenticating requests is as simple as calling &lt;code&gt;passport.authenticate()&lt;/code&gt; and specifying which strategy to employ. &lt;code&gt;authenticate()&lt;/code&gt;'s function signature is a standard &lt;a href="http://www.senchalabs.org/connect/"&gt;Connect&lt;/a&gt; middleware, which makes it convenient to use as route middleware in &lt;a href="http://expressjs.com/"&gt;Express&lt;/a&gt; applications.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;strategy-name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// If this function gets called, authentication was successful.&lt;/span&gt;
  &lt;span class="c1"&gt;// `req.user` contains the authenticated user.&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/users/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Unfortunately, &lt;code&gt;passport&lt;/code&gt; can only be used in an express-like applications, since &lt;code&gt;authenticate()&lt;/code&gt; returns a middleware function. It doesn't provide any API to use independently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In my opinion, the Django &lt;a href="https://docs.djangoproject.com/en/2.1/topics/auth/default/#authenticating-users"&gt;solution&lt;/a&gt; is the best one, because, unlike the others, it separates the &lt;code&gt;authentication&lt;/code&gt; from &lt;code&gt;login&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;a href="https://docs.djangoproject.com/en/2.1/topics/auth/default/#django.contrib.auth.authenticate"&gt;authenticate&lt;/a&gt; function checks the user credentials against each authentication backend, and returns a &lt;code&gt;User&lt;/code&gt; object if the credentials are valid for a backend.&lt;/li&gt;
&lt;li&gt;but the &lt;a href="https://docs.djangoproject.com/en/2.1/topics/auth/default/#django.contrib.auth.login"&gt;login&lt;/a&gt; function saves the user’s ID in the session, making it persistent for several requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Signing in users is application specific. But user identification is shared between apps and systems, only the &lt;code&gt;backend&lt;/code&gt; differs.&lt;/p&gt;

&lt;p&gt;From the above, and other examples not cited here, I ended up with a simple, stupid authentication manager, that dispatches the user credentials to a list of handlers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;AuthManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * Check the credentials and return the user
   */&lt;/span&gt;
  &lt;span class="nx"&gt;attempt&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Register an authenticator
   */&lt;/span&gt;
  &lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Authenticator&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The manager is an implementation of &lt;code&gt;chain of responsibility&lt;/code&gt; design pattern, and internally uses authenticators with the following contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Authenticator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * Try to handle the credentials, or delegate to the next handler.
   */&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I'm still working on this implementation to make it clean and framework-agnostic.&lt;br&gt;
You may take a look at the &lt;a href="https://github.com/aldojs/authentication/tree/develop"&gt;develop&lt;/a&gt; branch.&lt;/p&gt;

&lt;p&gt;What do you think guys ?&lt;/p&gt;

</description>
      <category>fromscratch</category>
      <category>cleancode</category>
      <category>authentication</category>
    </item>
    <item>
      <title>From Scratch: Web Sessions</title>
      <dc:creator>Abdessamad MOUHASSINE</dc:creator>
      <pubDate>Sat, 25 Aug 2018 16:00:39 +0000</pubDate>
      <link>https://dev.to/absolux/from-scratch-web-sessions-48n0</link>
      <guid>https://dev.to/absolux/from-scratch-web-sessions-48n0</guid>
      <description>

&lt;p&gt;Hello folks,&lt;/p&gt;

&lt;p&gt;As a first contribution, I want to share with you my thoughts about web sessions. Let's start by defining what are server-side sessions ? then I will show you an example of an abstract class that manages sessions in a simple and elegant way.&lt;/p&gt;

&lt;p&gt;Web sessions is an industry standard feature, that allows server applications to maintain and store user-specific data, during multiple request/response interactions between the client application, mainly the browser, and the server.&lt;/p&gt;

&lt;p&gt;State management is the main purpose of web sessions, they have no relationship with  cookies, requests, responses, programming language or framework. They all share the same members and behavior and could be written in any language.&lt;/p&gt;

&lt;h2&gt;
  
  
  Properties
&lt;/h2&gt;

&lt;p&gt;The basic, and required, session properties could be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;state&lt;/code&gt; which is the main attribute in the session class. it can be a simple &lt;code&gt;Map&lt;/code&gt; object that holds the key/value pairs.&lt;/li&gt;
&lt;li&gt;A unique &lt;code&gt;identifier&lt;/code&gt; or &lt;code&gt;token&lt;/code&gt; to distinguish a session from another.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;lifetime&lt;/code&gt;, or &lt;code&gt;time to live&lt;/code&gt; (aka TTL) property to calculate an expiry time after a moment of inactivity. it correspond to the number of seconds a session can live before expiration.&lt;/li&gt;
&lt;li&gt;Finally, a &lt;code&gt;storage&lt;/code&gt;, or whatever property name, to store a persistence adapter. It will be responsible of saving and retrieving the session's data from the storage (a text file in the file system, a database or the internal memory of the program executing the application).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Methods
&lt;/h2&gt;

&lt;p&gt;In object oriented programming, both members and methods reside in the same class. To be autonomous, a session instance have to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;save its state for further use,&lt;/li&gt;
&lt;li&gt;destroy the data if the session is expired,&lt;/li&gt;
&lt;li&gt;retrieve the state from storage on initialization,&lt;/li&gt;
&lt;li&gt;and regenerate a new identifier when it's necessary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, our session class can have the below structure:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Session&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * A Map object to store key/value pairs
   */&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * A string that uniquely identifies the session
   */&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * A number of seconds, milliseconds or even minutes to get the expiry time
   */&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;lifetime&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Storage driver is responsible of the saving/retrieving the session state
   */&lt;/span&gt;
  &lt;span class="kr"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;StorageInterface&lt;/span&gt;


  &lt;span class="cm"&gt;/**
   * Start the session, loading the state from the storage
   */&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;start&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;boolean&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * End the session, saving the state in the storage
   */&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;commit&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;boolean&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Regenerate a new session ID, maintaining the current state
   */&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;regenerate&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;boolean&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Invalidate the session, removing or flagging the session as expired
   */&lt;/span&gt;
  &lt;span class="kr"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;invalidate&lt;/span&gt; &lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;boolean&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Methods like &lt;code&gt;get()&lt;/code&gt;, &lt;code&gt;set()&lt;/code&gt; and &lt;code&gt;remove()&lt;/code&gt; ... etc, are necessary to manipulate the session state, but we'll ignore them for simplicity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I've used &lt;code&gt;TypeScript&lt;/code&gt; notation for the code example, and wrote a full implementation on &lt;a href="https://github.com/aldojs/session"&gt;Github&lt;/a&gt;, if you would like see it in action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Storage
&lt;/h2&gt;

&lt;p&gt;The persistence driver, that encapsulates the storage, is also thin and optimized. It has one, and only one, responsibility: &lt;code&gt;Persistence&lt;/code&gt;.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;StorageInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * Retrieve an entry by its key
   */&lt;/span&gt;
  &lt;span class="nx"&gt;read&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Remove an entry by its key
   */&lt;/span&gt;
  &lt;span class="nx"&gt;remove&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Save an entry's data for the given time
   */&lt;/span&gt;
  &lt;span class="nx"&gt;write&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;any&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;This is the general concept of web sessions and their reason to exist. They offer the &lt;code&gt;functionality&lt;/code&gt; (state management) not the &lt;code&gt;usability&lt;/code&gt; (creation, transfer, encoding, or persistence).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Decorator&lt;/code&gt; design pattern can be used to add more features to sessions without the need to modify or rewrite it from scratch. Unfortunately, web framework developers, repeat themselves, and re-implement the same session logic, over and over, with a different API.&lt;/p&gt;




&lt;p&gt;Links&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://machinesaredigging.com/2013/10/29/how-does-a-web-session-work/"&gt;https://machinesaredigging.com/2013/10/29/how-does-a-web-session-work/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://f5.com/resources/white-papers/cookies-sessions-and-persistence"&gt;https://f5.com/resources/white-papers/cookies-sessions-and-persistence&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.owasp.org/index.php/Session_Management_Cheat_Sheet"&gt;https://www.owasp.org/index.php/Session_Management_Cheat_Sheet&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Session_(computer_science)"&gt;https://en.wikipedia.org/wiki/Session_(computer_science)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


</description>
      <category>http</category>
      <category>session</category>
      <category>fromscratch</category>
      <category>cleancode</category>
    </item>
  </channel>
</rss>
