Skip to content

Latest commit

 

History

History
105 lines (75 loc) · 6.96 KB

securing-flask-application-using-kratos-and-keto.mdx

File metadata and controls

105 lines (75 loc) · 6.96 KB
templateKey path author title teaser subtitle overline publishedAt published description tags
blog
/securing-flask-application-using-kratos-and-keto/
gen1us2k
Securing Your Flask Application Using Kratos and Keto
Securing your flask application with open source solutions
Let's build a flask application using Ory Kratos and Ory Keto
Flask authentication guide
2022-02-01
true

import CodeFromRemote from '../../components/freestanding/utils/codefromremote'

Nowadays the engineering community has many products for authentication in their frameworks. Lots of them have built-in features for authentication and a lot of libraries available for social sign-in. We have the Django framework, Flask, and python-social-auth to build almost everything we need to authenticate users in the pythonic world.

In this article, I'll show you an example of how to add everything we need for the user's authentication without writing lots of lines of code. The code used in this blog post is available on GitHub. We'll use Flask, flask cookie-cutter, docker, docker-compose, Postgres, Ory Kratos and Ory Keto.

Let's take a look at the login flow of our application using Ory Kratos and Ory Keto Diagram of using Ory Kratos and Keto to secure Flask application

What we will use in our project

  • Flask cookiecutter is a great tool to bootstrap our project structure. It's always a great idea to have ready-to-use linters, Dockerfile, and package management tools out of the box.
  • Postgres as an RDBMS. We will have two Postgres services running in two containers in this example. I think that it's a great idea to keep it simple without using custom scripts to have multiple databases available in a single docker-compose service.
  • Ory Kratos with UI to authenticate users.
  • Ory Keto as an access control service.

Setting up Ory Kratos

Ory Kratos will be responsible for storing identity data such as email/login and password. Using the quickstart guide we need to copy the contents of contrib/quickstart/kratos/email-password to the root of your project and then add the following content to the docker-compose:

Setting up Ory Keto

You can get familiar with the concepts of Ory Keto reading the quickstart guide. These articles can give you a brief introduction to it. Since we need to manage access to the home page, we need to create a folder keto at the root of our project and have a keto/keto.yml file with the following content:

We need the following containers:

  • postgresd-auth is the database for Ory Keto.
  • keto-migrate that takes care of database migrations.
  • keto-perms is a wrapper to work with permissions using a command-line interface.
  • keto runs the server.

Working with policies

Keto has configured namespace app to use in Flask application. Following the guide Check whether a User has Access to Something I decided to implement simple permission policy for the demo project:

  • Use keto-cli managing permissions.
  • Use email for subjects without @ symbol.

Pros

  • Easy to use and maintain.
  • Can easily be automated using CI/CD pipelines.

Cons

  • Lack of UI can be dealbreaker for non-engineering staff
  • This permission policy can violate GDPR, HIPAA or any other compliances due to personal data usage.

Flask part

Nota bene

  • Consider having authorization and authentication packages that use Kratos SDK and Keto SDK. Instead of just calling some magic endpoints, your code will be more readable with SDKs.
  • Please pay attention to configure login session and cookies.
  • It's better to use Ory Cloud instead of having Ory Kratos managed by your team just because Ory manages it, and you don't need to enable observability/logging/metrics for your service.

Next steps

  1. Add Two Factor Authentication (2FA) to your App
  2. Add social signin features
  3. Configure more secure password policies
  4. Implement email and phone verification and account Activation