<?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: PrakadAlpha</title>
    <description>The latest articles on DEV Community by PrakadAlpha (@prakadalpha).</description>
    <link>https://dev.to/prakadalpha</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%2F118070%2F671acfe7-4366-47d4-8bcf-9446a473338d.jpeg</url>
      <title>DEV Community: PrakadAlpha</title>
      <link>https://dev.to/prakadalpha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prakadalpha"/>
    <language>en</language>
    <item>
      <title>Sessions Auth &amp; Auth</title>
      <dc:creator>PrakadAlpha</dc:creator>
      <pubDate>Sat, 25 Apr 2020 14:18:21 +0000</pubDate>
      <link>https://dev.to/prakadalpha/sessions-auth-auth-5bfc</link>
      <guid>https://dev.to/prakadalpha/sessions-auth-auth-5bfc</guid>
      <description>&lt;p&gt;When i started to work with authentication i was confused to hell and went through some articles and here is the concise explanation on managing sessions particularly in NodeJS or any backend, lets get started..!&lt;/p&gt;

&lt;h2&gt;
  
  
  🔥Methods Used For Auth in Node🔥
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;express-session&lt;/code&gt; &lt;a href="https://www.npmjs.com/package/express-session" rel="noopener noreferrer"&gt;npm package&lt;/a&gt; provides an extensive support for session management with external memory adapters&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;jsonwebtoken&lt;/code&gt; &lt;a href="https://www.npmjs.com/package/jsonwebtoken" rel="noopener noreferrer"&gt;npm package&lt;/a&gt; provides methods to implement JWT token based authentication.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;passportjs&lt;/code&gt; &lt;a href="http://www.passportjs.org/" rel="noopener noreferrer"&gt;library&lt;/a&gt; has different types of strategies and also facebook, google and other OAuth support, can also be combined with &lt;code&gt;express-session&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;A basic clarification on Authentication and Authorization&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication&lt;/strong&gt;: Verifying the &lt;em&gt;Identity&lt;/em&gt; (&lt;code&gt;401 Unauthorized&lt;/code&gt;)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization&lt;/strong&gt;: Handling the &lt;em&gt;Permissions&lt;/em&gt; (&lt;code&gt;403 Forbidden&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are two ways to handle these in web applications.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stateful (Using &lt;code&gt;session&lt;/code&gt; and &lt;code&gt;cookies&lt;/code&gt;)method &lt;/li&gt;
&lt;li&gt;Stateless (&lt;code&gt;jwt&lt;/code&gt; tokens and &lt;code&gt;OAuth&lt;/code&gt;). &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Stateful
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;User sessions are managed by the server using a session store like Redis or MongoDB&lt;/li&gt;
&lt;li&gt;Each user has own Session Id to be verified.&lt;/li&gt;
&lt;li&gt;Client and Server are dependent(stateful)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔀 Flow
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;User submits login credentials&lt;/li&gt;
&lt;li&gt;Server verifies the user against the DB and creates a temporary user session&lt;/li&gt;
&lt;li&gt;Server issues a cookie with a session ID&lt;/li&gt;
&lt;li&gt;User sends the cookie with each request&lt;/li&gt;
&lt;li&gt;Server validates it with the session store and provides access&lt;/li&gt;
&lt;li&gt;When user logs out, server destroys the session and clears the cookie&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  🍪 Cookies
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Cookies are list of key value pairs and flags for protection&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cookies are set in the server on the login request and sent to the client using the &lt;code&gt;Set-Cookie&lt;/code&gt; header in the response and there after &lt;br&gt;
sent from the client for all the requests to identify the user.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Server ➡ Client&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          HTTP/2.0 200 OK
          Content-type: text/html
          Set-Cookie: sessionId=12randomid45
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Client ➡ Server&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          GET /sample_page.html HTTP/2.0
          Host: www.example.org
          Cookie: sessionId=12randomid45;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  Options used in the cookies are as follows:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Secure&lt;/code&gt; ➡ Used to tell the browser to send cookies over https only.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HttpOnly&lt;/code&gt; ➡ Makes the cookies accesible only in the server, client side js cannot access it using document.cookie.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SameSite&lt;/code&gt; ➡ Blocks cross origin request(none, strict, lax)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Domain and Path&lt;/code&gt; ➡ These can be changes accordingly for security&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Expires or Max-Age&lt;/code&gt; ➡ This is used to persist the cookie for the specified time&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;h3&gt;
  
  
  Pros &amp;amp; Cons of Cookies
&lt;/h3&gt;

&lt;p&gt;Cons&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cookies are vulnerable to CSRF(Cross Site Request Forgery) but can be protected.&lt;/li&gt;
&lt;li&gt;Server must store each user session in memory&lt;/li&gt;
&lt;li&gt;Horizontal scaling is more challenging risk of single point failure, need sticky sessions with load balancing
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Pros&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Session IDs are vague and has no meaningful data&lt;/li&gt;
&lt;li&gt;Cookies can be secured with relativly new flags (SameSite, http-only)&lt;/li&gt;
&lt;li&gt;HttpOnly cookies mitigate XSS exploits&lt;/li&gt;
&lt;li&gt;SameSite cookies protects against CSRF&lt;/li&gt;
&lt;li&gt;20+ years of usage &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Stateless
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Session is managed on the client side by storing the signed token in the browser  &lt;code&gt;localStorage&lt;/code&gt; or &lt;code&gt;sessionStorage&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Client and Server are decoupled(stateless)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔀 Flow
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;User submits login credentials&lt;/li&gt;
&lt;li&gt;Server verifies the user against the DB&lt;/li&gt;
&lt;li&gt;Sever generates a signed token with a secret and embeds user data and send the token in header(Usually sent in &lt;code&gt;Authorization&lt;/code&gt; header)&lt;/li&gt;
&lt;li&gt;User stores the token in client storage(&lt;code&gt;localStorage&lt;/code&gt; or &lt;code&gt;sessionStorage&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;User sends the token along with each request and server verifies the token and provides access.&lt;/li&gt;
&lt;li&gt;When user logs out, token is cleared from the "client"&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🎟 JSON Web Token
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Consist of three parts&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Header(Algorithm &amp;amp; token type)
&lt;/li&gt;
&lt;li&gt;Payload(data)
&lt;/li&gt;
&lt;li&gt;Signature(Verification Sign)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Server doesn't keep track of the user session, it only needs the token for providing access&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Example:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          HTTP/1.1 200 OK
          Content-type: application/json
          Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1YmQ2MWFhMWJiNDNmNzI0M2EyOTMxNmQiLCJuYW1lIjoiSm9obiBTbWl0aCIsImlhdCI6MTU0MTI3NjA2MH0.WDKey8WGO6LENkHWJRy8S0QOCbdGwFFoH5XCAR49g4k 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;h3&gt;
  
  
  Stateful JWT
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Only user reference (userId) is embedded in the token&lt;/li&gt;
&lt;li&gt;Server uses ref in the token to retrieve user from the DB&lt;/li&gt;
&lt;li&gt;Sent as an HTTP-only cookie (Set-Cookie header)&lt;/li&gt;
&lt;li&gt;Sent along with X-CSRF-TOKEN cookie&lt;/li&gt;
&lt;li&gt;No user sessions stored on the server&lt;/li&gt;
&lt;li&gt;Revoked tokens still needs to be persisted

&lt;ul&gt;
&lt;li&gt;### Pros &amp;amp; Cons of JWT Auth
Cons&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Server has to maintain blacklist of revoked tokens (whitelist of active user tokens is secure).  &lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Tokens stored in client storage are vulnerable to XSS&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;While scaling secret's must be shared across server's&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Pros&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server does not need to keep track of user sessions&lt;/li&gt;
&lt;li&gt;Horizontal scaling is easy possible&lt;/li&gt;
&lt;li&gt;FrontEnd and BackEnd are decoupled&lt;/li&gt;
&lt;li&gt;Operational even if cookies are disabled&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  🔐 Security Handling
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;XXS   &lt;/p&gt;

&lt;p&gt;➡ Do not store tokens in clientStorage&lt;br&gt;&lt;br&gt;
➡ Use &lt;code&gt;httpOnly&lt;/code&gt; on cookie to mitigate &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CSRF
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;➡ Use X-CSRF-TOKEN header&lt;br&gt;&lt;br&gt;
➡ Use &lt;code&gt;sameSite&lt;/code&gt; on cookie with &lt;code&gt;lax&lt;/code&gt; to mitigate(relativly new, less browser support)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;helmet&lt;/code&gt; &lt;a href="https://www.npmjs.com/package/helmet" rel="noopener noreferrer"&gt;npm package&lt;/a&gt; provides inbuilt headers for increased protection&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;cors&lt;/code&gt; &lt;a href="https://www.npmjs.com/package/cors" rel="noopener noreferrer"&gt;npm package&lt;/a&gt; for handling cors issues&lt;/li&gt;
&lt;li&gt;Always use double round and strong encryption mechanism for token and cookies(&lt;em&gt;More on this soon..&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  🤔My Thought
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;" Sessions are probably better suited for web applications.."&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not JWT?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Server state needs to be maintained regardless of jwt.&lt;/li&gt;
&lt;li&gt;Sessions can be easily extended and invalidated.&lt;/li&gt;
&lt;li&gt;Data is secured server side &amp;amp; doesn't leak through XSS.&lt;/li&gt;
&lt;li&gt;CSRF is easier to mitigate than XSS.&lt;/li&gt;
&lt;li&gt;Data never goes stale (always in sync with DB)&lt;/li&gt;
&lt;li&gt;Sessions are generally easier to set up &amp;amp; manage.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🗒&lt;strong&gt;&lt;em&gt;Methods on overcoming the issues in JWT using &lt;code&gt;refresh_tokens&lt;/code&gt; will be blogged soon..!!&lt;/em&gt;&lt;/strong&gt;🗒&lt;/p&gt;

&lt;h1&gt;
  
  
  📓&lt;em&gt;References&lt;/em&gt;
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies" rel="noopener noreferrer"&gt;Cookies&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/CSRF" rel="noopener noreferrer"&gt;CSRF&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Cross-site_scripting" rel="noopener noreferrer"&gt;XXS&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>authentication</category>
      <category>cookies</category>
      <category>session</category>
      <category>jwt</category>
    </item>
    <item>
      <title>MERN BOILER PLATE</title>
      <dc:creator>PrakadAlpha</dc:creator>
      <pubDate>Fri, 24 Apr 2020 12:19:31 +0000</pubDate>
      <link>https://dev.to/prakadalpha/mern-boiler-plate-2e7</link>
      <guid>https://dev.to/prakadalpha/mern-boiler-plate-2e7</guid>
      <description>&lt;p&gt;" A well structured and commented boiler plate code for devs "&lt;/p&gt;

&lt;p&gt;Link for the code on &lt;strong&gt;&lt;em&gt;&lt;a href="https://github.com/PrakadAlpha/mern-boiler-plate"&gt;Github&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  👷‍♂️ Contents of the backend
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Node and Express server&lt;/li&gt;
&lt;li&gt;Simple &lt;em&gt;User authentication&lt;/em&gt; mechanism&lt;/li&gt;
&lt;li&gt;Basic &lt;em&gt;Route protection&lt;/em&gt; also enabled&lt;/li&gt;
&lt;li&gt;Implemented &lt;em&gt;JWT&lt;/em&gt;, token based authentication &lt;/li&gt;
&lt;li&gt;Also added &lt;em&gt;Role Based Authentication&lt;/em&gt; sample code&lt;/li&gt;
&lt;li&gt;Express &lt;em&gt;asyncHandler&lt;/em&gt; wrapping mechanism for handling async errors &lt;/li&gt;
&lt;li&gt;Routes and Middleware details are well &lt;em&gt;commented&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Used dotenv for &lt;em&gt;Environment Variables&lt;/em&gt;(config.env)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Mongoose&lt;/em&gt; for Db connection in config folder(db.js)&lt;/li&gt;
&lt;li&gt;Powerpacked express middlewares(helmet etc..) for preventing basic attacks (XXS, NoSQL injection)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  👨‍💻 Contents of the frontend
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Built with React and Redux&lt;/li&gt;
&lt;li&gt;Updated &lt;strong&gt;&lt;em&gt;Redux Hooks&lt;/em&gt;&lt;/strong&gt; usage(useSelector and useDispatch)&lt;/li&gt;
&lt;li&gt;Usage of &lt;em&gt;&lt;strong&gt;redux-thunk&lt;/strong&gt;&lt;/em&gt; for handling the async code&lt;/li&gt;
&lt;li&gt;Added basic &lt;em&gt;customizable css&lt;/em&gt; for easy usage &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Axios&lt;/em&gt; for api requests&lt;/li&gt;
&lt;li&gt;Removed tests and extra files &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🛠 Project Setup Instructions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;To install the Client and Server dependencies.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run setup
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Development mode scripts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;To run the both end's using concurrently.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To run the backend in development mode using Nodemon.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run server
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To run the frontend in development mode.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run client
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Production mode script
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;To build the client and run the server&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run prod
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  💖 Download &amp;amp; Share
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Absoultely! Feel free to share the repo and the code,&lt;br&gt;&lt;br&gt;
Give a ⭐ &lt;strong&gt;star&lt;/strong&gt; if it was helpful, please do share!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  ➕ Contribution
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Anyone is welcome to contribute. Please feel free to submit a Pull Request for typo fixes, spelling corrections and improvements etc..&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Link for the code on &lt;strong&gt;&lt;em&gt;&lt;a href="https://github.com/PrakadAlpha/mern-boiler-plate"&gt;Github&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>mongodb</category>
      <category>reactredux</category>
      <category>mernboilerplate</category>
    </item>
    <item>
      <title>How to plan a Web app project from start to production ?</title>
      <dc:creator>PrakadAlpha</dc:creator>
      <pubDate>Fri, 07 Feb 2020 04:49:18 +0000</pubDate>
      <link>https://dev.to/prakadalpha/how-to-plan-a-web-app-project-from-start-to-production-i3k</link>
      <guid>https://dev.to/prakadalpha/how-to-plan-a-web-app-project-from-start-to-production-i3k</guid>
      <description>&lt;p&gt;How to plan a Web app project from start to production ?&lt;/p&gt;

</description>
      <category>web</category>
      <category>project</category>
      <category>discuss</category>
      <category>help</category>
    </item>
  </channel>
</rss>
