<?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: Paul John</title>
    <description>The latest articles on DEV Community by Paul John (@namestarlit).</description>
    <link>https://dev.to/namestarlit</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%2F1002953%2Ff223965c-87c0-4278-b85a-30dee6a22191.png</url>
      <title>DEV Community: Paul John</title>
      <link>https://dev.to/namestarlit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/namestarlit"/>
    <language>en</language>
    <item>
      <title>Multiple OAuth2 Schemes FastAPI</title>
      <dc:creator>Paul John</dc:creator>
      <pubDate>Mon, 01 Jul 2024 14:16:40 +0000</pubDate>
      <link>https://dev.to/namestarlit/multiple-oauth2-schemes-fastapi-452j</link>
      <guid>https://dev.to/namestarlit/multiple-oauth2-schemes-fastapi-452j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;How do you create multiple OAuth2 token schemes in FastAPI and make sure they are usable in the SwaggerUI auto documentation?&lt;/p&gt;

&lt;p&gt;I am building a REST API for a SaaS Loan Management System (LMS) to be used internally by company personnel. I have two kinds of users: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;System users&lt;/strong&gt; - LMS software administrators.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Company users&lt;/strong&gt; - personnel using the software. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;System users can create companies and the first company users. Assign admin role, to allow them to register other company users linked to the same company. &lt;/p&gt;

&lt;p&gt;I created two OAuth2 token schemes; &lt;code&gt;admin_oauth2&lt;/code&gt; for system users and &lt;code&gt;user_oauth2&lt;/code&gt; for company users. Separating the authentication models it made it easy to manage. I can create tokens for admins with a simple payload of expiration date and admin ID. And add extra data in the company users' token like the company ID and the user roles for easy role-based access control and authorization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Multiple OAuth2 Schemes in FastAPI
&lt;/h2&gt;

&lt;p&gt;To create multiple OAuth2 schemes, create two token dependencies; one for the admins, and another for users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;user_oauth2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OAuth2PasswordBearer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;tokenUrl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/v1/login/access-token&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;scheme_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user_oauth2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;UserTokenDep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_oauth2&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="n"&gt;admin_oauth2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OAuth2PasswordBearer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;tokenUrl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/v1/admin/login/access-token&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;scheme_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;admin_oauth2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;AdminTokenDep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin_oauth2&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That will allow an API to have two OAuth2 authentications depending on the endpoint accessed. Notice the &lt;code&gt;scheme_name&lt;/code&gt; argument in the scheme definition. That's an important detail when you have multiple OAuth2 schemes in the same API - that is, if you don't define the scheme name, the authentication token depends on the last endpoint you defined regardless of whether the operation depends on that token or the other token. I spent hours trying to figure out that simple detail. That's all there is to it, the rest are skill issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read Me
&lt;/h2&gt;

&lt;p&gt;Hi there! I am Paul John, a recent software engineering graduate. I recently joined a backend, DevOps and Product Testing internship at &lt;a href="https://hng.tech/internship"&gt;HNG Internship&lt;/a&gt; to put my learned skills to work and broaden my horizons in Software engineering. My goal is to be able to create a software product from scratch to the maintenance stage (the whole SDLC).&lt;/p&gt;

&lt;p&gt;If anything, check out &lt;a href="https://hng.tech/premium"&gt;HNG Premium&lt;/a&gt;, it delivers added value to the Internship.&lt;/p&gt;

&lt;p&gt;Adios!&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>backend</category>
      <category>restapi</category>
      <category>oauth</category>
    </item>
    <item>
      <title>Multiple OAuth2 Schemes FastAPI</title>
      <dc:creator>Paul John</dc:creator>
      <pubDate>Mon, 01 Jul 2024 14:16:39 +0000</pubDate>
      <link>https://dev.to/namestarlit/multiple-oauth2-schemes-fastapi-5f9</link>
      <guid>https://dev.to/namestarlit/multiple-oauth2-schemes-fastapi-5f9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;How do you create multiple OAuth2 token schemes in FastAPI and make sure they are usable in the SwaggerUI auto documentation?&lt;/p&gt;

&lt;p&gt;I am building a REST API for a SaaS Loan Management System (LMS) to be used internally by company personnel. I have two kinds of users: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;System users&lt;/strong&gt; - LMS software administrators.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Company users&lt;/strong&gt; - personnel using the software. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;System users can create companies and the first company users. Assign admin role, to allow them to register other company users linked to the same company. &lt;/p&gt;

&lt;p&gt;I created two OAuth2 token schemes; &lt;code&gt;admin_oauth2&lt;/code&gt; for system users and &lt;code&gt;user_oauth2&lt;/code&gt; for company users. Separating the authentication models it made it easy to manage. I can create tokens for admins with a simple payload of expiration date and admin ID. And add extra data in the company users' token like the company ID and the user roles for easy role-based access control and authorization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Multiple OAuth2 Schemes in FastAPI
&lt;/h2&gt;

&lt;p&gt;To create multiple OAuth2 schemes, create two token dependencies; one for the admins, and another for users.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;user_oauth2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OAuth2PasswordBearer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;tokenUrl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/v1/login/access-token&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;scheme_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user_oauth2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;UserTokenDep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_oauth2&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;

&lt;span class="n"&gt;admin_oauth2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OAuth2PasswordBearer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;tokenUrl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/api/v1/admin/login/access-token&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;scheme_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;admin_oauth2&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;AdminTokenDep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Annotated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Depends&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin_oauth2&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That will allow an API to have two OAuth2 authentications depending on the endpoint accessed. Notice the &lt;code&gt;scheme_name&lt;/code&gt; argument in the scheme definition. That's an important detail when you have multiple OAuth2 schemes in the same API - that is, if you don't define the scheme name, the authentication token depends on the last endpoint you defined regardless of whether the operation depends on that token or the other token. I spent hours trying to figure out that simple detail. That's all there is to it, the rest are skill issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Read Me
&lt;/h2&gt;

&lt;p&gt;Hi there! I am Paul John, a recent software engineering graduate. I recently joined a backend, DevOps and Product Testing internship at &lt;a href="https://hng.tech/internship"&gt;HNG Internship&lt;/a&gt; to put my learned skills to work and broaden my horizons in Software engineering. My goal is to be able to create a software product from scratch to the maintenance stage (the whole SDLC).&lt;/p&gt;

&lt;p&gt;If anything, check out &lt;a href="https://hng.tech/premium"&gt;HNG Premium&lt;/a&gt;, it delivers added value to the Internship.&lt;/p&gt;

&lt;p&gt;Adios!&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>backend</category>
      <category>restapi</category>
      <category>oauth</category>
    </item>
  </channel>
</rss>
