<?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: Diego Dias</title>
    <description>The latest articles on Forem by Diego Dias (@ddiasfront).</description>
    <link>https://forem.com/ddiasfront</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%2F1485997%2F194a6fde-71fe-41bf-b967-06db02a7d5c4.jpeg</url>
      <title>Forem: Diego Dias</title>
      <link>https://forem.com/ddiasfront</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ddiasfront"/>
    <language>en</language>
    <item>
      <title>OAuth &amp; OpenID - Tokens basics</title>
      <dc:creator>Diego Dias</dc:creator>
      <pubDate>Wed, 28 Aug 2024 10:07:47 +0000</pubDate>
      <link>https://forem.com/ddiasfront/oauth-openid-tokens-basics-1e27</link>
      <guid>https://forem.com/ddiasfront/oauth-openid-tokens-basics-1e27</guid>
      <description>&lt;p&gt;As promised, I'll cover tokens a bit more in-depth particularly on this post, as I try to keep them as more as dry/simple as possible to make sure they're effective, so you're kind eating power food for each one of them :D&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick recap ( some words that summarise previous content ).
&lt;/h3&gt;

&lt;p&gt;1 - Authorization&lt;br&gt;
2 - Authentication&lt;br&gt;
3 - Client Registering&lt;br&gt;
4 - Redirecting&lt;/p&gt;

&lt;p&gt;Previously we started introducing something around tokens, we saw that tokens can carry or not user information.&lt;/p&gt;

&lt;p&gt;That's why we have denominated two types for them:&lt;br&gt;
1 - Opaque&lt;br&gt;
2 - Structured&lt;/p&gt;

&lt;p&gt;The opaque token doesn't carry any user information or role access info, on the other hand, the structured token is usually forged using a secret key and provided back by the Authorization server, therefore this token is usually also a string, but a string in which can only be parsed by those who have the secret key to retrieve user information and access roles or anything else you'd like to put on the token. &lt;/p&gt;

&lt;p&gt;PS: It is not recommended to put more info than necessary as the token travels a lot, so it is not recommended bit-wise.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;p&gt;1 - Only who have the key can parse the token&lt;br&gt;
2 - Resource servers should have the key in their secrets to parse the token.&lt;/p&gt;

&lt;p&gt;With that being said the access token is now obtained, now there's another dev decision to make: "Do we need get &lt;strong&gt;MORE&lt;/strong&gt; user info or not"?&lt;/p&gt;

&lt;p&gt;Although a structured token can carry user data, it is recommended to keep it as clean as possible, a common pattern of JWT token is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "iss": "https://auth.example.com",
  "sub": "1234567890",
  "aud": "https://api.example.com",
  "iat": 1693257600,
  "exp": 1693261200,
  "scope": "read write",
  "email": "user@example.com",
  "roles": ["admin", "user"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;User ID (sub): A unique identifier for the user.&lt;/li&gt;
&lt;li&gt;Issuer (iss): Identifies the issuing authority of the token.&lt;/li&gt;
&lt;li&gt;Expiration (exp): The token's expiration time.&lt;/li&gt;
&lt;li&gt;Issued at (iat): The timestamp of when the token was issued.&lt;/li&gt;
&lt;li&gt;Audience (aud): The intended recipient(s) of the token (e.g., your application).&lt;/li&gt;
&lt;li&gt;Scopes/Permissions: Define what the user is allowed to do.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that we have the user ID (sub), we could call the auth server again to get the user information by calling Open ID Connect which would provide more user info to our application IF WE DECIDE so, it is not mandatory, but...&lt;/p&gt;

&lt;p&gt;Now we are getting to an end, there's 2 important things I want to cover here: Refresh token and Token revocation:&lt;/p&gt;

&lt;p&gt;1 - Refresh token&lt;br&gt;
Whenever we receive an access token, there's an option to also receive a refresh token alongside with it, that means now you have 2 tokens, the only difference is that the refresh token doesn't give you access and a longer or no expiration time.&lt;/p&gt;

&lt;p&gt;So whenever your access token expires, to avoid that annoying login screen again we just send it alongside with your refresh token to the auth server, so we can retrieve access again without extra user effort.&lt;/p&gt;

&lt;p&gt;2 - Token revocation&lt;br&gt;
This is an endpoint on the authorisation server that works for expiring your tokens immediately, so whenever calling this endpoint, your access token expires at the authorisation server, meaning further checks on it will obtain a negative scenario. &lt;br&gt;
PS: Usually the refresh token expires when this is called.&lt;/p&gt;

&lt;p&gt;Okay, I guess that would do for now, here is what we covered so far:&lt;/p&gt;

&lt;p&gt;1 - Token types&lt;br&gt;
2 - Token structure&lt;br&gt;
3 - Roles&lt;br&gt;
4 - Open Id is a decision&lt;br&gt;
5 - Token refreshment&lt;br&gt;
6 - Token revokation&lt;/p&gt;

&lt;p&gt;That is a lot of crucial steps on understanding authentication/auth, I hope that helps anyone on understanding, please leave comments and suggestions on how to improve !&lt;/p&gt;

</description>
    </item>
    <item>
      <title>OAuth andOpenID - Introduction</title>
      <dc:creator>Diego Dias</dc:creator>
      <pubDate>Thu, 22 Aug 2024 08:59:50 +0000</pubDate>
      <link>https://forem.com/ddiasfront/oauth-andopenid-introduction-402m</link>
      <guid>https://forem.com/ddiasfront/oauth-andopenid-introduction-402m</guid>
      <description>&lt;h1&gt;
  
  
  Hello, welcome to my second post at Dev.to! :D
&lt;/h1&gt;

&lt;p&gt;We'll cover the basics of OAuth and OpenId and try to make the concept simple enough to memoize for interviews, let's go!&lt;/p&gt;

&lt;h2&gt;
  
  
  First thing, OpenID and OAuth are different things.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Authorization
&lt;/h3&gt;

&lt;p&gt;OAuth is responsible for issuing a "token" after you provide your credentials to the OAuth server, which answers back with the &lt;strong&gt;access token&lt;/strong&gt; that gives you access to APIs but doesn't carry any user data. That's what they call &lt;strong&gt;authorization&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;p&gt;OpenId implements the concept of user identity on top of the OAuth token mechanism, the difference is that you also receive an &lt;strong&gt;ID Token&lt;/strong&gt; alongside the access token. That's what they call &lt;strong&gt;authentication&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const OAuth = 'Authorization';
const OpenID = 'Authentication';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple, isn't it? Well, that's what they say. Look at the diagram below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fam416w2kaxwww1u21wap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fam416w2kaxwww1u21wap.png" alt="OAuth and OpenID Diagram" width="800" height="910"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Link to the full diagram:&lt;br&gt;
&lt;a href="https://infosec.mozilla.org/guidelines/assets/images/OIDC_sequence_diagram.png" rel="noopener noreferrer"&gt;https://infosec.mozilla.org/guidelines/assets/images/OIDC_sequence_diagram.png&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's basically the flow for an OpenID authentication.&lt;/p&gt;

&lt;p&gt;The main difference between this type of authentication and the standard cookie model is that it has its own authentication server and this server has full access agency, meaning controlling access by registering applications upfront and generating ClientID's and ClientSecret's for each registered client on this server.&lt;/p&gt;

&lt;p&gt;These Secret's and ID's are now used on the clients to perform requests to the authentication server, which provides the token.&lt;/p&gt;

&lt;p&gt;I think that's a good introduction to OAuth and OpenID.&lt;/p&gt;

&lt;p&gt;What've learned so far:&lt;/p&gt;

&lt;p&gt;1 - Authentication and Authorization&lt;br&gt;
2 - Authorization Server&lt;br&gt;
3 - ClientID and ClientSecret&lt;/p&gt;

&lt;p&gt;In the next articles, I'll cover more about the token and its different strands.&lt;/p&gt;

</description>
      <category>oauth</category>
      <category>openid</category>
      <category>authentication</category>
    </item>
    <item>
      <title>JWT - Concept for interviews</title>
      <dc:creator>Diego Dias</dc:creator>
      <pubDate>Wed, 14 Aug 2024 21:49:01 +0000</pubDate>
      <link>https://forem.com/ddiasfront/jwt-concept-for-interviews-27cn</link>
      <guid>https://forem.com/ddiasfront/jwt-concept-for-interviews-27cn</guid>
      <description>&lt;p&gt;I've been working with JWT in mostly all of the projects for the past 5 years, I myself had set up an authentication endpoint using JWT for a personal project from scratch and yet, sometimes I fail to answer how JWT works in interviews. &lt;/p&gt;

&lt;p&gt;The objective of this post is basically to create a simple strategy to fix this concept in our heads so when we get to an interview and they ask around that, we know how to counter-punch.&lt;/p&gt;

&lt;p&gt;It should be simple, so let's stop with the useless chatty chat and go to what really matters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;THE JWT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&lt;/p&gt;

&lt;p&gt;That's how a JWT token would look like, each dot represents a divisor in the structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ptwdr2u66u9q0xvugsf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3ptwdr2u66u9q0xvugsf.png" alt="Image description" width="800" height="564"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Structure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;header.payload.signature&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Header&lt;/strong&gt;&lt;br&gt;
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.&lt;/p&gt;

&lt;p&gt;The header usually stores the algorithm and the token type.&lt;/p&gt;

&lt;p&gt;The algorithm stands for the type of algorithm that will be used to encode the token, usually we use HS256.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Payload&lt;/strong&gt;&lt;br&gt;
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.&lt;/p&gt;

&lt;p&gt;The payload usually contains something like:&lt;/p&gt;

&lt;p&gt;{"iat": timestamp, "iss": string, "exp": timestamp, "userId": string, "userRole": boolean}&lt;/p&gt;

&lt;p&gt;User identification (e.g.: userId, userRole)&lt;br&gt;
Expiry&lt;br&gt;
IssuedAt (iat): Represents the token creation date.&lt;br&gt;
Issuer: (iss): Servername whom issued the token.&lt;br&gt;
Expiry: (exp): Token expiration time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Signature&lt;/strong&gt;&lt;br&gt;
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c&lt;/p&gt;

&lt;p&gt;The signature is compound by creating a hash using HMACSHA256 algorithm with the given input:&lt;/p&gt;

&lt;p&gt;HMACSHA256(&lt;br&gt;
base64(header), &lt;br&gt;
base64(payload), &lt;br&gt;
base64(key)&lt;br&gt;
) = Signature.&lt;/p&gt;

&lt;p&gt;Simple as that, we have an output which is the Encoded JWT token, it now can only be validated by servers who have the key and where the key is matchable with the token.&lt;/p&gt;

</description>
      <category>jwt</category>
      <category>interview</category>
      <category>authentication</category>
    </item>
  </channel>
</rss>
