<?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 Wole Ikotun on Medium]]></title>
        <description><![CDATA[Stories by Wole Ikotun on Medium]]></description>
        <link>https://medium.com/@wolkott?source=rss-15939be6fd8e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*o1jwU_sYzZkkbM-Q_LNhyg@2x.jpeg</url>
            <title>Stories by Wole Ikotun on Medium</title>
            <link>https://medium.com/@wolkott?source=rss-15939be6fd8e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 04 Nov 2026 22:50:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wolkott/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[Securing Your Python Flask API with JSON Web Tokens (JWT)]]></title>
            <link>https://medium.com/@wolkott/securing-your-python-flask-api-with-json-web-tokens-jwt-a30855a404e?source=rss-15939be6fd8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a30855a404e</guid>
            <dc:creator><![CDATA[Wole Ikotun]]></dc:creator>
            <pubDate>Fri, 20 Jan 2023 00:17:09 GMT</pubDate>
            <atom:updated>2023-01-20T00:22:37.641Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*2oZ1eLy6IoB38EB2PUf65w.jpeg" /></figure><p>Building a robust and secure API is crucial for any application that handles sensitive information or requires user authentication. One way to ensure the security of your API is by implementing JSON Web Tokens (JWT) for authentication and authorization. In this article, we will discuss how to secure a Python Flask API using JWT.</p><p><strong>What is JSON Web Token (JWT)?</strong></p><p>JWT is a JSON-based open standard (RFC 7519) that is used to securely transmit information between parties. JWTs consist of three parts: a header, a payload, and a signature.</p><p>The header contains information about the type of token and the signature algorithm used to generate it. The payload contains the claims, which are statements about an entity (typically, the user) and the actions that can be performed on the API. The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message hasn’t been tampered with.</p><p><strong>Using JWT for Authentication in Flask</strong></p><p>To use JWT for authentication in a Flask API, we can use a library like PyJWT to handle the encoding and decoding of the tokens. We can also use a library like Flask-JWT-Extended to handle the creation and validation of the tokens.</p><p>The flow of using JWT for authentication in Flask would be as follows:</p><ol><li>The client sends a login request with their credentials to the server.</li><li>If the credentials are valid, the server generates a JWT and sends it back to the client.</li><li>The client stores the JWT and includes it in the header of all subsequent requests to the server.</li><li>The server verifies the JWT on each request to ensure that the client is authenticated and authorized to access the requested resources.</li></ol><p>It’s important to note that JWT is not a replacement for a database and it’s not meant to store the user data. Instead, it’s used to identify the user and their roles and permissions.</p><p><strong>Using JWT for Authorization in Flask</strong></p><p>JWT can also be used for authorization in a Flask API. The payload of the JWT can contain information about the user’s roles and permissions, which the server can use to determine what actions the user is allowed to perform.</p><p>For example, the payload of the JWT could contain the following information:</p><pre>{<br>  &quot;sub&quot;: &quot;1234567890&quot;,<br>  &quot;name&quot;: &quot;John Doe&quot;,<br>  &quot;iat&quot;: 1516239022,<br>  &quot;exp&quot;: 1516239022,<br>  &quot;roles&quot;: [&quot;admin&quot;, &quot;user&quot;]<br>}</pre><p>The server can then use the “roles” field to determine what actions the user is allowed to perform. For example, if the user has the “admin” role, they would be allowed to perform administrative tasks such as creating new users or deleting existing ones.</p><p><strong>Securing the JWT</strong></p><p>It’s important to note that JWT should be treated as an opaque string and should not be decoded by the client. This is because the JWT contains sensitive information that should only be accessible to the server. The JWT should also be properly signed using a secure algorithm such as HS256, HS384, or HS512, to prevent tampering of the JWT.</p><p>Another important security measure to take is to ensure that the JWT is transmitted over a secure connection (HTTPS) and to use a unique and long secret key for signing the JWT.</p><p><strong>Conclusion</strong></p><p>JSON Web Tokens (JWT) provide a secure and efficient way for handling authentication and authorization in web based applications / rest apis, and we discussed how it can be applied to a python flask web application.</p><p>Rolling your own authentication mechanism might not be efficient or good enough, in which case it might be better to use existing authentication methods available such as the social logins as provided by Google, Facebook, Twitter, etc.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a30855a404e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>