<?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: Ankit Babbar</title>
    <description>The latest articles on DEV Community by Ankit Babbar (@babbarankit).</description>
    <link>https://dev.to/babbarankit</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%2F462141%2Fee8ede35-b75c-440e-9655-5d2c8cc58a20.png</url>
      <title>DEV Community: Ankit Babbar</title>
      <link>https://dev.to/babbarankit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/babbarankit"/>
    <language>en</language>
    <item>
      <title>Designing OAuth Auth Flow </title>
      <dc:creator>Ankit Babbar</dc:creator>
      <pubDate>Sat, 19 Sep 2020 08:56:53 +0000</pubDate>
      <link>https://dev.to/babbarankit/designing-oauth-auth-flow-96i</link>
      <guid>https://dev.to/babbarankit/designing-oauth-auth-flow-96i</guid>
      <description>&lt;p&gt;This articles explores the design of an OAuth2 Auth Flow in decoupled Frontend and Backend application. Get a high level understanding of how OAuth Auth Flow works in 3 legs (Frontend, Backend and OAuth Provider), then use existing libraries or design custom experience without using any libraries. Frontend could possibly a React Application or Angular Application or any other app, Backend app could be written in NodeJS, Go or any language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.digitalocean.com%2Farticles%2Foauth%2Fauth_code_flow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.digitalocean.com%2Farticles%2Foauth%2Fauth_code_flow.png" alt="OAuth Flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understand the flow
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;User Authorization Request&lt;/strong&gt;: From Client App, on a button click, open the Auth Window either in same window using &lt;code&gt;window.location.assign(oauth_provider_auth_url)&lt;/code&gt; or in another window using &lt;code&gt;window.open(oauth_provider_auth_url)&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The oauth_provider_auth_url will generally carry the client id, scope, redirect_uri and few other parameters&lt;/li&gt;
&lt;li&gt;This url can be either stored in Client Side or for security purpose this can be obtained from server.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User Authorises Application&lt;/strong&gt;: The flow will now be handed to OAuth Provider (like Google, or Facebook), where user will approve application to login and grant permission for scope specified earlier.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization Code Grant&lt;/strong&gt;: After user confirmation, oauth provider issues an OAuth Code, and it redirects to &lt;code&gt;redirect_uri&lt;/code&gt; specified earlier with an auth code. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access Token Request&lt;/strong&gt;: The Redirect Controller/Page, can now send this request to backend to issue you an access token using the auth code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access Token Grant&lt;/strong&gt;: Server issues the Access Token.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Front End Concerns
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Sign In OAuth Button&lt;/li&gt;
&lt;li&gt;Redirect page in your application.

&lt;ul&gt;
&lt;li&gt;In Development Mode you can provide localhost:[port]/&lt;a href="//if%20OAuth%20provider%20support%20this.%20Google%20doesn't%20support%20this"&gt;oauth-redirect-uri&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Ngrok is alternative tool which can be used to port forward your localhost and then redirect uri can be specified [your-ngrok-site-url]/[oauth-redirect-uri].&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Required action on SignIn - global variable update&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Back End Concerns
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Obtain the Acces Token from OAuth Provider and issue custom Access Token.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Handling the Auth Code: Backend Sends the Auth Code Sent from the Oauth Redirect Controller in Front End Application to the OAuth provider Access Token Url (with parameters like client_id, client_secret, scope, etc).&lt;/li&gt;
&lt;li&gt;On success, OAuth Provider sends a payload that has access token of the OAuth Provider and maybe refresh token and other things.&lt;/li&gt;
&lt;li&gt;OAuth Provider Access Token can be wrapped in your custom JWT Token so that you can use it for further request like obtaining profile information.&lt;/li&gt;
&lt;li&gt;JWT Token can be sent to Client App to store in its local storage (which can be insecure). Best is to use Http Only Cookie. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;PS: Check out my other article on &lt;a href="https://dev.to/babbarankit/designing-authentication-system-for-a-ssr-client-application-2p8e"&gt;Designing Authentication System&lt;/a&gt;&lt;/p&gt;

</description>
      <category>oauth</category>
      <category>ngrok</category>
      <category>react</category>
      <category>angular</category>
    </item>
    <item>
      <title>Choosing a Frontend Form Library</title>
      <dc:creator>Ankit Babbar</dc:creator>
      <pubDate>Tue, 15 Sep 2020 05:36:46 +0000</pubDate>
      <link>https://dev.to/babbarankit/choosing-form-library-2hm5</link>
      <guid>https://dev.to/babbarankit/choosing-form-library-2hm5</guid>
      <description>&lt;p&gt;Forms are one of the most complex pieces of Frontend Development. In this article we will explore the concerns while selection of Form Library for React or Vue or Angular App. &lt;/p&gt;

&lt;p&gt;If the form library, is experimented in tool like Storybook independent of your application code, the complexity arising later can be avoided. Learning from the Continuous Delivery practices, “if it hurts, do it more often, and bring the pain forward”. Therefore prepare a checklist after studying this article and plan effectively the roadmap.&lt;/p&gt;

&lt;p&gt;Let's do the Separation of concerns first. Building forms have two types of concerns the Logical concerns (validation, multi value fields, debug errors, etc) and the Presentation Concerns (ease of styling, widgets provided). &lt;/p&gt;

&lt;h2&gt;
  
  
  Logical Concerns
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Validation
&lt;/h3&gt;

&lt;p&gt;Validation is an important concern in selection of any form library. Let us attempt to categorise the types of field validations: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simple validation&lt;/strong&gt;: Validation of Field Data Type &amp;amp; RegExp Constrains (like Email, Url, Number, Date, etc)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complex validation&lt;/strong&gt;: Dependent Field Validation (&lt;em&gt;where a field is validated dependent upon on other field value&lt;/em&gt;) or validation of array of collection of fields.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Async validation&lt;/strong&gt;: Remote Validation for use cases like unique username validation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Writing Validation Rules:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Field Level Functions&lt;/strong&gt;: Validation could be defined at level of each field (like the TextInput can define a prop, say &lt;strong&gt;validationFn&lt;/strong&gt; to check Email RegExp).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Form Data Structure Schema&lt;/strong&gt;: Library (like yup or joi) allow you to write a schema that validates the whole data structure, including dependent fields.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Multi Value Field Collections
&lt;/h3&gt;

&lt;p&gt;For a use case like Flight Ticket (with list of passengers), a collection of field is required. Ease for adding, removing, shuffling an item/entity in collection must be considered.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi Step Form
&lt;/h3&gt;

&lt;p&gt;For a complex use case like Flight Ticket Booking, user may have to go through a journey of multiple forms before the final submission. Multi step forms become complex and ease of shifting steps (possible with animation effect, that is a presentation concern) and maintaining the state in multi steps should be studied, you may also like to use LocalStorage to retain state in case of page refresh or if user by mistake close the tab.&lt;/p&gt;

&lt;h3&gt;
  
  
  Field Focus and Scroll to Error
&lt;/h3&gt;

&lt;p&gt;For managing Field Focus, or Scroll on First Error after submission and other important use cases where you might need the reference of input element, experiment how this can be accomplished in the selected library.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependent Fields and States
&lt;/h3&gt;

&lt;p&gt;Field May become hidden depending upon some other field's value. Getting any field value must be easy at the top level (and in a sub form component that becomes part of main form).&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;Needless to say, performance is the most important criteria in complex forms. Jerks in UI when field value changes or field states hide, reflects poor performance in Form, study how best this can be improved. &lt;/p&gt;

&lt;h3&gt;
  
  
  Debug
&lt;/h3&gt;

&lt;p&gt;For a good Developer Experience (DX), it must be easy to debug &lt;strong&gt;field values&lt;/strong&gt;, &lt;strong&gt;fields touched&lt;/strong&gt; and &lt;strong&gt;field errors&lt;/strong&gt;. How easy it is to get these values and display them in UI (in footer of form or drawer) during development phase is important while building complex forms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Presentation Concerns
&lt;/h2&gt;

&lt;p&gt;Choosing Form UI Components via a UI Design Library like Ant Design or Material UI solves the Styling Concern. Alternatively you can create custom components library (which can be styled using Styled Components/css/scss). This is hard, author suggest to use some UI Library unless you have very special Design Concerns and can't be accomplished using common UI Libraries.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Many a times an additional component (say FormItem) act as a wrapper for any kind of Input. FormItem can show the label, error message, help message etc, and an input child element. FormItem + Input, may also provided as a single element.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Widgets
&lt;/h3&gt;

&lt;p&gt;Number can be input via sliders, Text Box; Booleans can be input via Switch, Radio, Checkbox. Make a list of widgets required for your presentation and check availability of each and writing a new one from scratch if required like for Drag-n-Drop.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complex Widgets&lt;/strong&gt;: Rich Text Editor, file uploads via Dropzone, Video Upload, Select with remote search / tag support may be required. Prepare a checklist of what all is required.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Styling Concerns
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Form Item Styling: Styling of label, error message, help text etc in ForItem, maybe you wish to align Label and Input in same line for Desktop and in different lines in Mobile or hide placeholder in Mobile.&lt;/li&gt;
&lt;li&gt;Input Styling: Want a Round Checkbox, Want to Change Select Placeholder Font, Border of Text Input... How easy it is to style these components.&lt;/li&gt;
&lt;li&gt;Animation: For Multi step forms or Dependent Fields, or Change State of Switch/Checbox, your design can have multiple kinds of animation that you will like to support. Please study the ease of such changes required in the libraries choosen.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Accessibility
&lt;/h3&gt;

&lt;p&gt;Widgets must set accessibility props, like  aria-label, aria-required, aria-invalid so as improve Accessibility Support. &lt;/p&gt;

&lt;p&gt;(Note: The author has extensive experience in React. The thought process for other frameworks/libraries may be very similar. Suggestion are welcome to improve the article).&lt;/p&gt;

</description>
      <category>react</category>
      <category>vue</category>
      <category>angular</category>
    </item>
    <item>
      <title>Designing Secure Stateless Authentication</title>
      <dc:creator>Ankit Babbar</dc:creator>
      <pubDate>Wed, 02 Sep 2020 11:20:08 +0000</pubDate>
      <link>https://dev.to/babbarankit/designing-authentication-system-for-a-ssr-client-application-2p8e</link>
      <guid>https://dev.to/babbarankit/designing-authentication-system-for-a-ssr-client-application-2p8e</guid>
      <description>&lt;p&gt;We will discuss the high level design for a secure authentication, with important concerns for both backend and frontend. Client Side Application could be written in React or Angular or Vue or any other modern JS Libraries/Frameworks. Backend could be in NodeJS or Go or Php or any language/frameworks your prefer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selection of Appropriate Technology
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Local Storage and Session Storage are vulnerable to XSS attacks and can be easily read by Injected Scripts, they are also only available in Browser Environment.&lt;/li&gt;
&lt;li&gt;In-Memory  (i.e. storing it as a global variable like React Context in React App), but it is useful only if the  application wants to persist Authentication in a single tab (which will be useful for Banking/Financial Websites). &lt;/li&gt;
&lt;li&gt;Cookie Based Authentication Systems  are equally vulnerable to XSS Attack or CSRF. But since Cookies are Universal therefore can be chosen for a persistent login which can be made secure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Security Concerns
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HttpOnly Cookie Attribute&lt;/strong&gt;: HTTP only Cookie prevents the XSS or CSRF Attack.  A Http Only Cookie cannot be read by Javascript but it is passed along with your request to your server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Same-Site Cookie Attribute&lt;/strong&gt;:  For additional Security of Cookie you can specify the Same Site as ‘strict’ which will make sure that Cookie is only passed when the User Hit Your Website from Browser Address Bar. Cookie is no longer available in IFrame.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secure Cookie Attribute&lt;/strong&gt;: Cookies in Production Environment must be sent through Https Only. 
Content-Type Header: By allowing application/json in the content type, prevents CSRF attack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limiting DDoS Attack&lt;/strong&gt;: Backend application must restrict number of login attempts from an IP to prevent the DDoS attack.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Authentication Tokens
&lt;/h2&gt;

&lt;p&gt;A Stateless Authentication generally have two tokens, i.e. Access Token and Refresh Token. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Access Token&lt;/strong&gt;: Access Token must be short lived and it must be validated in the backend application before proceeding on any operation or data access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Refresh Token&lt;/strong&gt;: Refresh Token might be issued to support automatic re-issuing of access tokens. 

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Validation&lt;/strong&gt;: While issuing a refresh token,  a record in Database can be created with an expiry, and before issuing a fresh access token, refresh token must be validated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Single Time Use&lt;/strong&gt;: After a single use, refresh token must be deleted and a fresh refresh token should be issued to user for further usage. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attack Prevention&lt;/strong&gt;: An additional mechanism to delete all users tokens or user specific Refresh Token must be available in case of security theft to block the user(s).&lt;/li&gt;
&lt;/ul&gt;


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

&lt;h2&gt;
  
  
  How to know if Access Token is expired in Frontend?
&lt;/h2&gt;

&lt;p&gt;While designing the authentication system, either a third cookie &lt;em&gt;(HttpOnly: false)&lt;/em&gt; or send response which can be stored in LocalStorage. It can then be read by client side application to decide whether a token must be refreshed before sending the next request. Cookie has the advantage that it is universal and can also be read in SSR Applications. LocalStorage will be restricted to Browser only Environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logout From Application
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;(Frontend Concern)&lt;/em&gt; For implementing a better Logout on the Client Side, it is advisable that a local storage event is fired on Logout and all Tabs Automatically Update their Global Variable and expire Sensitive Information available to a particular user. &lt;/p&gt;

&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://web.dev/samesite-cookies-explained"&gt;https://web.dev/samesite-cookies-explained&lt;/a&gt;&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies"&gt;https://stackoverflow.com/questions/19867599/what-is-the-difference-between-localstorage-sessionstorage-session-and-cookies&lt;/a&gt;&lt;br&gt;
&lt;a href="https://security.stackexchange.com/questions/166724/should-i-use-csrf-protection-on-rest-api-endpoints"&gt;https://security.stackexchange.com/questions/166724/should-i-use-csrf-protection-on-rest-api-endpoints&lt;/a&gt;&lt;br&gt;
&lt;a href="https://security.stackexchange.com/questions/170477/csrf-with-json-post-when-content-type-must-be-application-json"&gt;https://security.stackexchange.com/questions/170477/csrf-with-json-post-when-content-type-must-be-application-json&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(HTML forms generated from Backend Application are not part of scope of this article and therefore we don't expand on CSRF Token which is an important security concern for HTML Forms).&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>node</category>
      <category>authentication</category>
      <category>react</category>
    </item>
  </channel>
</rss>
