avatarKevin Horton

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5810

Abstract

div id="2fc2"><pre>npm install express express-session keycloak-connect nodemon</pre></div><h2 id="592f">Define main execution file — app.js</h2><p id="f128">Add a file named app.js at the root level of your secure-express-service project.</p><p id="8d46">Define imports and express app</p><div id="54ca"><pre><span class="hljs-comment">// file - app.js</span>

<span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>); <span class="hljs-keyword">const</span> session = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express-session"</span>); <span class="hljs-keyword">const</span> <span class="hljs-title class_">Keycloak</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">"keycloak-connect"</span>);

<span class="hljs-keyword">const</span> app = <span class="hljs-title function_">express</span>(); <span class="hljs-keyword">const</span> <span class="hljs-variable constant_">PORT</span> = <span class="hljs-number">3000</span>;

...</pre></div><p id="ccec">Setup Keycloak Middleware</p><div id="cefb"><pre><span class="hljs-comment">// file - app.js</span>

...

<span class="hljs-keyword">const</span> <span class="hljs-variable constant_">USER_ROLE</span> = process.<span class="hljs-property">env</span>.<span class="hljs-property">USER_ROLE</span> || <span class="hljs-string">'express-user'</span>; <span class="hljs-keyword">const</span> <span class="hljs-variable constant_">ADMIN_ROLE</span> = process.<span class="hljs-property">env</span>.<span class="hljs-property">ADMIN_ROLE</span> || <span class="hljs-string">'express-admin'</span>;

<span class="hljs-keyword">const</span> kcConfig = { <span class="hljs-attr">clientId</span>: process.<span class="hljs-property">env</span>.<span class="hljs-property">AUTH_CLIENT_ID</span> || <span class="hljs-string">'secure-express-service'</span>, <span class="hljs-attr">bearerOnly</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">serverUrl</span>: process.<span class="hljs-property">env</span>.<span class="hljs-property">AUTH_SERVER</span> || <span class="hljs-string">'http://localhost:8080'</span>, <span class="hljs-attr">realm</span>: process.<span class="hljs-property">env</span>.<span class="hljs-property">AUTH_REALM</span> || <span class="hljs-string">'master'</span> };

<span class="hljs-keyword">const</span> memoryStore = <span class="hljs-keyword">new</span> session.<span class="hljs-title class_">MemoryStore</span>();

<span class="hljs-title class_">Keycloak</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">accessDenied</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">request, response</span>) { response.<span class="hljs-title function_">status</span>(<span class="hljs-number">401</span>) response.<span class="hljs-title function_">setHeader</span>(<span class="hljs-string">'Content-Type'</span>, <span class="hljs-string">'application/json'</span>) response.<span class="hljs-title function_">end</span>(<span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({ <span class="hljs-attr">status</span>: <span class="hljs-number">401</span>, <span class="hljs-attr">message</span>: <span class="hljs-string">'Unauthorized/Forbidden'</span>, <span class="hljs-attr">result</span>: { <span class="hljs-attr">errorCode</span>: <span class="hljs-string">'ERR-401'</span>, <span class="hljs-attr">errorMessage</span>: <span class="hljs-string">'Unauthorized/Forbidden'</span> } })) }

<span class="hljs-keyword">const</span> keycloak = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Keycloak</span>({ <span class="hljs-attr">store</span>: memoryStore }, kcConfig);

<span class="hljs-keyword">function</span> <span class="hljs-title function_">adminOnly</span>(<span class="hljs-params">token, request</span>) { <span class="hljs-keyword">return</span> token.<span class="hljs-title function_">hasRole</span>(<span class="hljs-string">realm:<span class="hljs-subst">${ADMIN_ROLE}</span></span>); }

<span class="hljs-keyword">function</span> <span class="hljs-title function_">isAuthenticated</span>(<span class="hljs-params">token, request</span>) { <span class="hljs-keyword">return</span> token.<span class="hljs-title function_">hasRole</span>(<span class="hljs-string">realm:<span class="hljs-subst">${ADMIN_ROLE}</span></span>) || token.<span class="hljs-title function_">hasRole</span>(<span class="hljs-string">realm:<span class="hljs-subst">${USER_ROLE}</span></span>); }

app.<span class="hljs-title function_">use</span>(<span class="hljs-title function_">session</span>({ <span class="hljs-attr">secret</span>: process.<span class="hljs-property">env</span>.<span class="hljs-property">APP_SECRET</span> || <span class="hljs-string">'BV&%R*BD66JH'</span>, <span class="hljs-attr">resave</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">saveUninitialized</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">store</span>: memoryStore }));

app.<span class="hljs-title function_">use</span>( keycloak.<span class="hljs-title function_">middleware</span>() );

...</pre></div><p id="bfe2">Setup REST endpoint and server</p><div id="5fc0"><pre>app.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/public'</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { res.<span class="hljs-title function_">status</span>(<span class="hljs-number">200</span>).<span class="hljs-title function_">send</span>({ <span class="hljs-string

Options

">'message'</span>: <span class="hljs-string">"This is a public enpoint which can be accessed by anonymous users"</span>, }); })

app.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/secured'</span>, [keycloak.<span class="hljs-title function_">protect</span>(isAuthenticated)], <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { res.<span class="hljs-title function_">status</span>(<span class="hljs-number">200</span>).<span class="hljs-title function_">send</span>({ <span class="hljs-string">'message'</span>: <span class="hljs-string">"This is a secured enpoint which can be accessed by any authenticated user"</span>, }); })

app.<span class="hljs-title function_">get</span>(<span class="hljs-string">'/secured-admin'</span>, [keycloak.<span class="hljs-title function_">protect</span>(adminOnly)], <span class="hljs-function">(<span class="hljs-params">req, res</span>) =></span> { res.<span class="hljs-title function_">status</span>(<span class="hljs-number">200</span>).<span class="hljs-title function_">send</span>({ <span class="hljs-string">'message'</span>: <span class="hljs-string">"This is a secured enpoint which can be accessed only by any authenticated user with role admin"</span>, }); })

app.<span class="hljs-title function_">listen</span>(<span class="hljs-variable constant_">PORT</span>, <span class="hljs-function">(<span class="hljs-params">error</span>) =></span> { <span class="hljs-keyword">if</span> (!error) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"Server is Successfully Running, and App is listening on port "</span> + <span class="hljs-variable constant_">PORT</span>) } <span class="hljs-keyword">else</span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"Error occurred, server can't start"</span>, error); } } );</pre></div><h2 id="a530">Update package.json scripts to run server</h2><ul><li>Update the content in the <b>scripts </b>section of <b>package.json</b> as below. It will help you run your express server in both development and production mode</li></ul><div id="6d0e"><pre> <span class="hljs-attr">"scripts"</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span> <span class="hljs-attr">"start"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"node app.js"</span><span class="hljs-punctuation">,</span> <span class="hljs-attr">"dev"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"nodemon app.js"</span> <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span></pre></div><ul><li>Start your express server in development mode(auto-reload on file change) using the below command</li></ul><div id="464c"><pre>npm run dev</pre></div><h1 id="1699">Testing your REST APIs</h1><h2 id="f8a6">Public Endpoint— http://localhost:3000/public</h2><ul><li>Even without an authentication token, this endpoint will fetch a response.</li></ul><figure id="74df"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xjvkOCdQJ5VIolwBtu-TWg.png"><figcaption></figcaption></figure><h2 id="f796">Secured Endpoint — http://localhost:3000/secured</h2><ul><li>Without a token, you should get an authentication error</li></ul><figure id="ad69"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*rSANl9_J1BRma6p1UjgLPg.png"><figcaption></figcaption></figure><ul><li>Let’s set up the <b>Authorization</b> tab to generate a token using the <b>Configure New Token </b>section. You can use the values below.</li></ul><div id="3d60"><pre>Token <span class="hljs-type">Name</span> - secure-express-service <span class="hljs-keyword">Grant</span> <span class="hljs-keyword">type</span> - <span class="hljs-keyword">Password</span> Credentials <span class="hljs-keyword">Access</span> Token URL - http://localhost:<span class="hljs-number">8080</span>/realms/master/protocol/openid-<span class="hljs-keyword">connect</span>/token Client ID - secure-express-service Username - <span class="hljs-keyword">user</span>@nerdcore.com <span class="hljs-keyword">Password</span> - Client Authentication - Send <span class="hljs-keyword">as</span> basic auth <span class="hljs-keyword">header</span></pre></div><figure id="5d0e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*AEhl6BHxVPT5J6X6wKnxGg.png"><figcaption></figcaption></figure><ul><li>Click on <b>Get New Access Token</b>, then P<b>roceed</b>, then <b>Use Token</b></li></ul><figure id="ea60"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*t7pdQRYhqsnhK_NDBSD19g.png"><figcaption></figcaption></figure><figure id="55e0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*MwMrf1npagIRpGmXEfOWcw.png"><figcaption></figcaption></figure><ul><li>Now fire the <b>/secured</b> rest endpoint again, and you will get a successful response.</li></ul><figure id="e106"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xsq6E7TWbMeTHXzNEQMD1Q.png"><figcaption></figcaption></figure><h2 id="97a4">Secured Admin Endpoint — http://localhost:3000/secured-admin</h2><ul><li>If you try to access the endpoint without any token — Auth error</li><li>If you try to access the endpoint with the token from [email protected] — Auth error</li><li>If you try to access the endpoint with the token from [email protected] — Success</li></ul><p id="db11">You can find the GitHub repository <a href="https://github.com/saurav-samantray/secure-express-service">here</a> for reference.</p><p id="eda2">Happy learning and happy coding!</p></article></body>

Your Creativity Will Improve When You Choose to Persevere

Adversity is necessary so that growth can reveal itself.

Photo by Jakob Owens on Unsplash

Last week was one of the longest weeks of my life. I’m not sure if it was the amount of work or the anxiety of getting it all done that drained the life out of me most, but I knew a break was needed.

Part of it was my fault, though.

Scratch that: all of it was my fault!

I was the one brilliant enough to register for a full-time summer semester instead of taking some time off like a normal human being.

I knew the work was going to be accelerated. Paper after paper turned in one after the other, sometimes even on the same day.

None of that was lost to my pre-semester examination process. But what I failed to realize was just how much it would pull from me.

There were times when I was about to throw in the towel, drop the classes, and continue on with my life. Everyone else is enjoying their summer, why shouldn’t I?

But something inside of me wouldn’t allow that kind of decision to be made. I had to finish what I started, even though it tends to be a little inconvenient to my conscience at times.

The truth is, I realized I could still enjoy my summer. In order to do that, though, I had to get serious about how I used my time and attention in the midst of chaos.

Giving up just because things get difficult forms bad habits. To persevere, on the other hand, takes a different sort of mindset.

Adjusting Your Focal Point

As a photographer, I’ve become acclimated to the use of aperture: the hole on a camera’s lens that determines how much light is allowed to enter to camera’s body.

The lower the aperture, the more light passes through the lens. The higher the aperture, the less light passes through. I didn’t understand this at first. And to be honest, I thought it sounded a little contradictory.

It wasn’t until I actually started taking photos that it all made sense to me.

Then something else became clearer. Aperture also affects the focal point, otherwise known in the photography world as depth of field.

What this does is makes an object or person stand out from the background, in turn making the background more blurry and out of focus. (It looks amazing if I should say so myself!)

In essence, you’re able to determine how much you choose to pay attention to by setting the aperture of your lens. Life is no different.

We’re often focused on the wrong things because we’ve gotten distracted by other unimportant things. And in the process, time is wasted.

You have to be aiming for something, and it should definitely be specific. Chances are, if your goals aren’t clear and your eyes aren’t fixed on the process itself, you’ll be more prone to quit than any other time.

It’s so easy to do that once you’ve lost sight of what’s pushing you forward. The purpose behind why you move is unclear because too many things stand as your focal point.

Photo by Jacob Sapp on Unsplash

Feet That Run Aimlessly

Many start out with no clear objectives. Instead, they start simply because someone else did, mimicking without analyzing their purpose for taking that first step.

That’s not to say you should have it all figured out before beginning your journey into a creative wonderland.

After all, there has to some level of curiosity that says, “I’m not sure where this will lead, but I’m gonna do it anyway.”

To be honest, a lot of people fail to move because they are unsure of where it could lead. The thought is pretty scary if we were all honest. Negative results loom in no time and fill our heads with all kinds of reason to stay still.

It may fall through. It could ruin our lives and all the passion we believe is there.

But those are all possibilities just as much as success stands as a possibility.

You could also do well. Over time, you could develop a unique approach to the most common things, like photography, writing, music, drawing, etc. Yet, you’ll never know unless you begin.

That was my mindset when I started writing in November of last year. I was a speck in a very large ocean. But I had one thing that nudged me to start typing curiosity.

I’m still a speck. But at least now there’s a better understanding of how this speck contributes to the roaring waves of our modern society.

Final Words

No matter how rough things get, and no matter how hard it seems, don’t give up just because the going gets tough.

Take control of your focal point in life.

Cut out what pulls you away from what matters.

Then recenter and clarify your objectives for starting your journey.

You don’t possess creativity just for the sake of having it. You should be using it to add something of value to the world. It all starts with taking your time and attention seriously.

Kevin Horton is a photographer, student, modest book-worm, and wanna-be web developer with a new-found love for writing. He writes helpful words about creativity, productivity, and the enjoyably simple life.

’Til next time. Thanks for reading!

Related Stories

Creativity
Productivity
Self Improvement
Self
Personal Development
Recommended from ReadMedium