<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nosherwan Ghazanfar on Medium]]></title>
        <description><![CDATA[Stories by Nosherwan Ghazanfar on Medium]]></description>
        <link>https://medium.com/@nosherwan?source=rss-ca7c9ad7eac3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*jKDbVh6OMvTdZBm3.jpg</url>
            <title>Stories by Nosherwan Ghazanfar on Medium</title>
            <link>https://medium.com/@nosherwan?source=rss-ca7c9ad7eac3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 11:27:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nosherwan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing GraphQL Server & client app using a private OAuth 2.0 Server]]></title>
            <link>https://medium.com/@nosherwan/implementing-graphql-server-client-app-using-a-private-oauth-2-0-server-63209f1613e2?source=rss-ca7c9ad7eac3------2</link>
            <guid isPermaLink="false">https://medium.com/p/63209f1613e2</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[oauth2]]></category>
            <dc:creator><![CDATA[Nosherwan Ghazanfar]]></dc:creator>
            <pubDate>Tue, 23 Apr 2019 03:52:04 GMT</pubDate>
            <atom:updated>2019-04-23T03:52:04.484Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I started learning about graphql as I had to implement a solution containing graphql data server utilising apollo, mysql &amp; react.js.</p><p>After going through a few samples &amp; tutorials it became clear graphql servers are supposed to provide data &amp; are not meant to implement security for particular applications querying the data, as this responsibility lies logically with another part of the solution architecture. This is regardless of the fact that for smaller applications the same server app may implement both functionalities.</p><p><em>For reference look at the link below for enforcing graphql security:</em></p><p><a href="https://www.apollographql.com/docs/tutorial/resolvers#authentication">https://www.apollographql.com/docs/tutorial/resolvers#authentication</a></p><p>However there is some confusion around this particularly for people new to graphql &amp; implementing security for both a server based application as well as client applications being served via them. The reasons for this are:</p><ul><li><strong>Popular solutions have integrated samples</strong><br> Apollo graphql is the most popular graphql implementation and they offer Apollo graphql package integration with web servers such as express.js and Koa.js. This gives the impression that graphql should be used within such applications, which if used as REST applications are supposed to implement their own security. However logically this does not imply that the authentication and authorisation of the REST application should somehow be integrated with graphql server.</li><li><strong>Only basic authentication is widely understood</strong><br> In simple applications basic authentication is used with username and passwords, in such applications same database will be used if additionally graphql implementation is provided potentially using the same database to store and retrieve authentication and authorisation details.</li><li><strong>Oauth and openID strategies are complicated and confusing<br> </strong>This can also be particularly confusing because nowadays application providers want to implement well defined security &amp; identity management solutions such as oauth 2.0 etc.</li></ul><p>After understanding the above challenges and reading through oauth &amp; graphql documentation I prescribe the following parts for a descent size end to end solution:</p><ol><li><strong>Graphql Server app:</strong> A server side application that will access a sql database or the likes, and serve it via a graphql package such as Apollo graphql server.<br><a href="https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server">https://github.com/apollographql/apollo-server/tree/master/packages/apollo-server</a></li><li><strong>Independent server app</strong>: The server app is independent of the client applications that it will serve. This means it should only concern itself will providing the data.</li><li><strong>SPAs or mobile apps:</strong> The client applications are either going to be single page applications or mobile apps, that will negotiate authentication &amp; authorisation with an oauth 2.0 provider. This could be a react application or a react-native application for example.</li><li><strong>Use Open source Oauth Server</strong>: A oauth 2.0 server that will take care of security and identity management.<br>Surprisingly there are a few open source and free products out there that can be privately installed and provide this functionality without writing code. Examples are below:</li></ol><p><a href="https://fusionauth.io/"><em>https://fusionauth.io/</em></a> (FusionAuth adds authentication, authorisation, user management, reporting, analytics and much more in minutes not months.)</p><p><a href="https://www.keycloak.org/index.html"><em>https://www.keycloak.org/index.html</em></a> (Add authentication to applications and secure services with minimum fuss. No need to deal with storing users or authenticating users. It’s all available out of the box.)</p><p><strong>Simplified Oauth 2:<br></strong>The following link describes in simple terms oath 2.0 and various strategies out of which two are the most popular ones that I would follow namely, authorisation code &amp; password. <br> <a href="https://aaronparecki.com/oauth-2-simplified/">https://aaronparecki.com/oauth-2-simplified/</a><br> The post is written by Aaron Parecki who is a oauth consultant and and written W3C specs himself. Please review this post before progressing below as it will help understand further details.</p><p>Implementation details for the solution are as follows:</p><p><strong>Oauth Server Installation:</strong></p><p>We have decided to use fusionauth as that is extensive and can pretty much be installed out of the box as a private instance and used for authentication and identity management. It allows registering multiple applications and their credentials such as appIds, redirect urls and tokens etc for individual front end applications.</p><p>It also provides RESTfull endpoints that can be used later by server side applications to validate the tokens issued by the oauth server to client applications to check that they are accessing secure applications.</p><p>So this server will stand in the middle of both the client and server applications. On one end it will register and authorise users for front-end apps. On the other end it will server an introspection endpoint for server apps that will query for with the credentials of the querying front-end apps.</p><p><strong>Front End Application:</strong></p><p>Any front end application that is already registered with the oath server such as fusionauth will basically re-direct the user to fusionauth interface for login or registration with a request. Keeping in mind the ouath 2.0 implementation as it is not possible for spa or mobile apps to keep the client secret safe, hence the request will only use clientId, redirect_url &amp; scope in the request that is allowed to be public.</p><p>This process requires a redirect_url to be provided in the request process that is also already registered with fusionauth. This callback will be returned with an authorisation code, which the front end app will again exchange for access and refresh tokens.</p><p>These tokens will be stored in local storage by these apps, and will be later used to query data from the graphql server application for additional data. The front end apps will send the token as a bearer token within the header of the request to the graphql server as below:</p><p><em>Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42</em></p><p><strong>Server graphql application:</strong></p><p>This can be a node.js application that primarily uses garphql-server and garphql pakages. This application will connect to an sql database to serve the data for the queries via graphql schema and resolvers.</p><p>However for security it will implement a context function on the graphql-server that will get the access token as a bearer token via the request header from either a front end web or mobile app. It will then verify that token via an introspection endpoint on the fusionauth server that is a RESTfull endpoint and will return details about the access token that will include the related info that will include expired date, email address etc.</p><p><em>Following links explains introspection endpoint details:</em> <br> <a href="https://www.oauth.com/oauth2-servers/token-introspection-endpoint/">https://www.oauth.com/oauth2-servers/token-introspection-endpoint/</a><br> <a href="https://oauth.net/2/token-introspection/">https://oauth.net/2/token-introspection/</a></p><p>In order to reduce the load on the oauth server this information including access &amp; refresh tokens can be stored in the local database and accessed first to check if the access token that has been provided exists for that email and if it has not expired. In case of expiration first it can be checked by introspection endpoint, or an error can be generated, or if a refresh token is available it can be used to get a new access token for the client based on individual approach.</p><p>The above explanation is one possible but practical approach to dealing with a scenario where oauth 2 server, client &amp; server app authentication and authorisation has to be implemented with respect to graphql.</p><p>Hope it helps clarify the implementation of such a solution. Feel free to provide comments and describe your own experiences in such scenarios.</p><p>Thank you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63209f1613e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Analyse “this” …]]></title>
            <link>https://medium.com/@nosherwan/analyse-this-5f370cba643?source=rss-ca7c9ad7eac3------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f370cba643</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[this-keyword]]></category>
            <category><![CDATA[this]]></category>
            <dc:creator><![CDATA[Nosherwan Ghazanfar]]></dc:creator>
            <pubDate>Fri, 30 Jun 2017 12:11:10 GMT</pubDate>
            <atom:updated>2017-06-30T23:57:26.842Z</atom:updated>
            <content:encoded><![CDATA[<p>Yes we are going to analyse the <em>“this”</em> keyword in <em>javascript</em> &amp; not talk about the movie with the same name.</p><p>The past few weeks in my conversations with peers I was surprised to note that <em>“this”</em> keyword is still looked upon with a dreaded eye.</p><p>A quick google search also failed to provide easy to understand explanations, so I decided to refresh my own memory &amp; provide a simple set of rules regarding the elusive <em>“this”</em> keyword &amp; its quirks.</p><p>If kept in mind these steps will allow<em> javascript</em> learners &amp; experienced devs alike to tame the lion.</p><p>The best way to understand the examples is to open up your web browser, open developer tools &amp; copy paste each snippet in the console.</p><h4><strong>1. Only Use “this” Inside Functions</strong></h4><p>The keyword<em> “this”</em> is meant to be a reference for an <em>object</em> in <em>javascript</em>. It can only be used inside functions, if used outside it won’t work reliably.</p><pre>// this will work in case this is assigned a value<br>function myPrecious(){ this.name = &#39;the ring&#39;};</pre><pre>//won’t give a reliable result<br>var fellows = {frodo: this.name}</pre><h4>2. A Tale Of Two Contexts</h4><p>There is one scenario where <em>“this”</em> keyword can mean two different things, and that is if it is used in an environment such as client side browser.<br>Firstly when a function is used as an event handler, its <em>“this”</em> is set to the <em>element</em> the event fired from.</p><pre>function changeColor(e) {<br>//this condition will be true if attached as event handler to an element<br> if(this === e.target){<br>  this.style.color = &#39;green&#39;;<br> }<br>}</pre><pre>// Get an element in the document<br>var _element = document.getElementById(&#39;my_label&#39;);</pre><pre>_element.addEventListener(&#39;click&#39;, changeColor, false);</pre><p>In all other cases it means the object to which a function is bound. The rest of the rules are discussed in this context.</p><h4><strong>3. Global Domination</strong></h4><p>Lets see how <em>“this”</em> keyword behaves in global context.</p><p>a. When used outside of a function in javascript refers to the <em>global</em> object, in case of the browser that is the <em>window</em> object.</p><p>b. Inside a standalone function it also refers to the <em>global</em> object.</p><pre>function checkMaterial(){<br> //if this function is declared as is this would mean window<br> this.material = &#39;wood&#39;;<br>}</pre><pre>checkMaterial();</pre><pre>//should display wood in the console.<br>window.material;</pre><h4>4. Think About The Parent</h4><p>The <em>“this”</em> keyword should always be used inside functions &amp; those functions should have a defined context such as an <em>object literal</em>, or the function should be a <em>constructor function</em>.</p><p><strong><em>i. Object Literal:</em></strong></p><p>Simply create an object, and attach a method.</p><pre>var ring = {<br> name: &#39;&#39;,<br> myPrecious: function(){<br> this.name = &#39;the ring&#39;;<br> }<br>};</pre><pre>ring.myPrecious();</pre><pre>//should print the name of the ring object in console<br>ring.name;</pre><p><strong><em>ii. Constructor Function:</em></strong></p><p>A <em>constructor function</em> is used to create new objects with the <em>new</em> keyword.</p><pre>//A constructor function, by convention is named with a Capital Letter<br>function FellowShip(){<br> this.item = &#39;the ring&#39;;<br>}</pre><pre>//add a method to a constructor function accessing item property and returning it<br>FellowShip.prototype.getTheOneRing = function(){<br> return this.item;<br>}</pre><pre>//always use a new keyword with a constructor function<br>var fellowship = new FellowShip();</pre><pre>console.log(&#39;Fellowship has an item to protect&#39;, fellowship.getTheOneRing());</pre><h4>4. Invocation Not Definition</h4><p>The <em>“this”</em> keyword is always resolved when a function is called &amp; not where it is defined, so when in doubt always think about the place of call; it is not assigned a value until an object invokes the function where <em>“this”</em> is defined.</p><h4>5. Let Go Of Doubt</h4><p>When in doubt use <em>call</em>, <em>apply </em>or <em>bind</em> to bind a function to another object and in turn defining the <em>“this”</em> object for it.</p><p><strong><em>i. call:</em></strong></p><pre>var fakeRing = {item: &#39;fake ring&#39;};</pre><pre>fellowship.getTheOneRing.call(fakeRing);</pre><p><strong><em>ii. apply:</em></strong></p><pre>fellowship.getTheOneRing.apply(fakeRing);</pre><p><strong><em>iii. bind:</em></strong></p><pre>//Use a function not used as constructor function, otherwise bind is ignored</pre><pre>function getTheOneRing(){<br> return this.item;<br>}</pre><pre>var getTheRing = getTheOneRing.bind(fakeRing);<br>getTheRing();</pre><h4>6. Deal With “this” When Things Get Complicated</h4><p>In nested functions use a named variable such as <em>self</em> to keep track of <em>“this” </em>keyword.</p><pre>var fellowShip = {<br> name: &#39;the band of brothers&#39;,<br> companion: {<br> name: &#39;frodo&#39;,<br> keepTheRing: function(){<br> var self = this;</pre><pre>function meltTheRing(){<br> console.log(self.name + &#39; will melt the ring &#39;)<br> }<br> <br> meltTheRing();<br> },<br> }<br>}</pre><pre>//should display name property attached to self<br>fellowShip.companion.keepTheRing();</pre><p>I hope this with clarify some of the confusion surrounding <em>“this”</em> keyword.</p><p>If more context is required then I suggest you search for the following concepts on Mozilla Developer Network (MDN) website.</p><ul><li>Object Literal</li><li>Constructor Function</li><li>this keyword</li><li>call, apply, bind</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f370cba643" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flux Stores with ES6 Syntax]]></title>
            <link>https://medium.com/@nosherwan/flux-stores-with-es6-syntax-d2182552091e?source=rss-ca7c9ad7eac3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2182552091e</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[flux]]></category>
            <dc:creator><![CDATA[Nosherwan Ghazanfar]]></dc:creator>
            <pubDate>Sun, 01 Nov 2015 22:35:07 GMT</pubDate>
            <atom:updated>2015-11-01T22:39:47.156Z</atom:updated>
            <content:encoded><![CDATA[<p>Flux Stores with ES6 Syntax</p><p>Its just a coincidence that since I started learning react+flux the ES6 class syntax had just become available. Facebook definitely seem to be encouraging it. There are allot of discussions about the benefits of such syntax and whether it loses the benefits of composition in JavaScript.</p><p>This blog post will totally by pass that discussion and will provide you with one potential solution to a small challenge that I saw when I started using ES6 syntax in flux.</p><p>(I am assuming here that the reader is already familiar with the flux pattern used in React and you are using the flux npm package provided by Facebook)</p><p>My practice so far is as follows if it makes sense to use ES6 class syntax I do it, otherwise if simple object construction or composition (using Object.assign) makes more sense then I do that.</p><p>An example of this practice is that when I have to inherit from classes such as React.Component or Dispatcher I would use class syntax.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1911d14fc989c22ac7d0658b701e306d/href">https://medium.com/media/1911d14fc989c22ac7d0658b701e306d/href</a></iframe><p>when I feel the need for static classes or objects then I would just create a singleton object and export that out of the file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bf6df93e13099aa999d69891bd6149c/href">https://medium.com/media/7bf6df93e13099aa999d69891bd6149c/href</a></iframe><p>Now comes the interesting bit, what if you require a singleton and still have to use class syntax because you are inheriting from another class. I found this to be the case where I was implementing stores in flux. I know that a basic store class can be extended from EventEmitter and then the rest of the stores can extend from that store, however the purpose here is not to show inheritance hence I will simply extend my store implementation directly from EventEmitter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36948e8d1aa50b2a6425302b88c8326e/href">https://medium.com/media/36948e8d1aa50b2a6425302b88c8326e/href</a></iframe><p>So the key point here is that the stores in flux are singletons as they are created once and then reused. So I am extending the store from EventEmitter and then I just export the store as a new instance so that it remains a singleton for the rest of the life cycle.</p><p>Another thing to notice is that I am using the constructor to register the store callback with the AppDispatcher. I have seen some implementations where the instance is created first then the dispatchToken variable is assigned the dispatch token after calling AppDispatcher.register method. I don’t feel the need for that as it does not matter at which point during the instantiating the object the registration is done; hence the constructor feels like a natural place to do so.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2182552091e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>