<?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>Forem: Gilbert Hawkins Winja</title>
    <description>The latest articles on Forem by Gilbert Hawkins Winja (@hawkinswinja).</description>
    <link>https://forem.com/hawkinswinja</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%2F1198314%2Fc5c8489d-aa17-4a8b-b61a-3a475791f293.jpg</url>
      <title>Forem: Gilbert Hawkins Winja</title>
      <link>https://forem.com/hawkinswinja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hawkinswinja"/>
    <language>en</language>
    <item>
      <title>API Authentication with Open ID Connect</title>
      <dc:creator>Gilbert Hawkins Winja</dc:creator>
      <pubDate>Wed, 09 Oct 2024 21:39:25 +0000</pubDate>
      <link>https://forem.com/hawkinswinja/api-authentication-with-open-id-connect-32hc</link>
      <guid>https://forem.com/hawkinswinja/api-authentication-with-open-id-connect-32hc</guid>
      <description>&lt;p&gt;I recently was working on a take home assessment for a junior role in API development, which required to implement Authentication using OpenID Connect (OIDC). There are lots of tutorials that try to explain  authentication protocols and how they differentiate from each other. Well, this is not one of those, as Nate Barbettini did a good job already.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/996OiexHze0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In this article, I share how to implement a simple OIDC authentication in Django using mozilla-django-oidc. mozilla-django-oidc &lt;a href="https://mozilla-django-oidc.readthedocs.io/en/stable/index.html" rel="noopener noreferrer"&gt;docs&lt;/a&gt; is really good and easy to follow for a simple setup. Before we continue you'll need to set up your identity provider (IdP). An IdP is where users authenticate from to access your application such as &lt;a href="https://console.cloud.google.com/apis/credentials" rel="noopener noreferrer"&gt;Google&lt;/a&gt;. Download the metadata.json file which will contain the required values for our settings file that we will transfer to the .env file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Project Setup (Make sure to have virtual environment activated).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; pip &lt;span class="nb"&gt;install &lt;/span&gt;Django, mozilla-django-oidc
 django-admin startproject oidc &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open the settings file and add the following
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Add 'mozilla_django_oidc' to INSTALLED_APPS&lt;/span&gt;
INSTALLED_APPS &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    &lt;span class="s1"&gt;'django.contrib.auth'&lt;/span&gt;,
    &lt;span class="s1"&gt;'mozilla_django_oidc'&lt;/span&gt;,  &lt;span class="c"&gt;# Load after auth&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="c"&gt;# Add 'mozilla_django_oidc' authentication backend&lt;/span&gt;
AUTHENTICATION_BACKENDS &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="s1"&gt;'mozilla_django_oidc.auth.OIDCAuthenticationBackend'&lt;/span&gt;,
    &lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
OIDC_RP_IDP_SIGN_KEY &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"RS256"&lt;/span&gt;
OIDC_OP_JWKS_ENDPOINT &lt;span class="o"&gt;=&lt;/span&gt; os.environ[&lt;span class="s1"&gt;'OIDC_OP_JWKS_ENDPOINT'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
OIDC_RP_CLIENT_ID &lt;span class="o"&gt;=&lt;/span&gt; os.environ[&lt;span class="s1"&gt;'OIDC_RP_CLIENT_ID'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
OIDC_RP_CLIENT_SECRET &lt;span class="o"&gt;=&lt;/span&gt; os.environ[&lt;span class="s1"&gt;'OIDC_RP_CLIENT_SECRET'&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
OIDC_OP_AUTHORIZATION_ENDPOINT &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;URL of the OIDC OP authorization endpoint&amp;gt;"&lt;/span&gt;
OIDC_OP_TOKEN_ENDPOINT &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;URL of the OIDC OP token endpoint&amp;gt;"&lt;/span&gt;
OIDC_OP_USER_ENDPOINT &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;URL of the OIDC OP userinfo endpoint&amp;gt;"&lt;/span&gt;
LOGIN_REDIRECT_URL &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;URL path to redirect to after login&amp;gt;"&lt;/span&gt;
LOGOUT_REDIRECT_URL &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;URL path to redirect to after logout&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If you read the mozilla-django-oidc docs (#RTFM), you probably saw the callback url path: &lt;code&gt;/oidc/callback&lt;/code&gt;. Add these to your IdP settings for now, but can later update the same for production environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update the urls.py file to include the oidc urls from mozilla. This is necessary for the callback, logout, and authentication paths.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# File urls.py &lt;/span&gt;
from django.urls import path

urlpatterns &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
    &lt;span class="c"&gt;# ...&lt;/span&gt;
    path&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'oidc/'&lt;/span&gt;, include&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'mozilla_django_oidc.urls'&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;,
    &lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;With these simple setup, your project is ready with OIDC. Start the server and navigate to the &lt;code&gt;localhost:8000/oidc/authenticate/&lt;/code&gt;
This will redirect you to a Google login screen (check the url, you notice its no longer localhost). Successful login will redirect back to localhost. mozilla-django-oidc will create a new User object using a hash of your email as the username. This behavior can be configured as explained in its docs.&lt;/li&gt;
&lt;li&gt;To logout, visit the endpoint &lt;code&gt;oidc/logout/&lt;/code&gt; which will terminate the user session.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;mozilla-django-oidc makes it easy to implement and understand how openID works in a simple way. To understand how to implement this for a REST API and a React Frontend, and how the whole flow works, try and clone the &lt;a href="https://github.com/hawkinswinja/mini-commerce.git" rel="noopener noreferrer"&gt;minicommerce project&lt;/a&gt; and run the project.&lt;/p&gt;

&lt;p&gt;Hope this helps to start out using openID&lt;/p&gt;

</description>
      <category>api</category>
      <category>django</category>
    </item>
    <item>
      <title>You need to understand networking as a SWE!</title>
      <dc:creator>Gilbert Hawkins Winja</dc:creator>
      <pubDate>Tue, 31 Oct 2023 20:28:26 +0000</pubDate>
      <link>https://forem.com/hawkinswinja/you-need-to-understand-networking-as-a-swe-5afa</link>
      <guid>https://forem.com/hawkinswinja/you-need-to-understand-networking-as-a-swe-5afa</guid>
      <description>&lt;p&gt;Networking is among the key beginner fundamentals necessary if considering a breakthrough in any software engineering related role. From the various protocols to understanding how the internet works, you cannot ignore Networking. The internet is the biggest network ever, and since most software communicate or interact with their users through the internet, it is essential to have core understanding of core networking concepts.&lt;br&gt;
The tech world and roles are so versatile and with new technology such as cloud cropping up, they are set to increase. Most of these roles require core understanding of networking concepts which define how communication happens across the internet. Below I share some key concepts of networking you need to understand critically well:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Protocols
&lt;/h3&gt;

&lt;p&gt;A network is only but an interconnection of computers that can communicate to each other, but it does not define how this happens. Protocols are the set of rules that govern communication over a network. A lot of protocols exist for different purposes. Some of the common ones include TCP, UDP, IP, FTP, and HTTP among many others.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. OSI (Open Systems Interconnection) model
&lt;/h3&gt;

&lt;p&gt;It is impossible to discuss networking without mentioning the OSI model. The OSI is a conceptual framework that standardizes telecommunication into seven distinct layers with different functionality. Understanding what happens at each layer is critical for designing robust systems whether hardware or software.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. IP addressing And Domain Name Server (DNS)
&lt;/h3&gt;

&lt;p&gt;IP address is the unique identifier that makes networking possible. Every device connected to a network is identified using its IP address or a domain name. Domain names are also unique and are resolved using a DNS. A famous interview question is to describe what happens when you enter a URL in a browser. It is a great high-level demonstration of how the internet works using IPs and domain names.&lt;/p&gt;

&lt;p&gt;Networking is very broad with so much to learn including the various devices, routing, and network managements. With the advent of cloud technology and container orchestration, understanding core network fundamentals is critical in resolving a lot of issues. As a beginner software engineer looking to break into tech, irrespective of the role, whether cloud architect, DevOps, or Machine Learning, mastering networking is a necessity.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>devops</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
