Home > How to Use Starlette-Context to Pass Authorization Tokens between FastAPI APIs?
30 May 2023
Lire cet article en Français

How to Use Starlette-Context to Pass Authorization Tokens between FastAPI APIs?

How to Use Starlette-Context to Pass Authorization Tokens between FastAPI APIs?

This post will explore using starlette-context with FastAPI to pass authorization tokens between your various microservices.

Authentication and authorization are crucial components of any IT system because they ensure data security and that users can only access resources for which they have the necessary permissions. To guarantee application end-to-end data security in a microservices architecture, exchanging authorization tokens across services is common practice.

FastAPI is a popular Python framework for creating web apps. It has many features that make development easier, such as input validation and automatic documentation generation. However, sharing data between different components of an application, such as user settings, query contexts, and especially authentication tokens, can be challenging.

The good news is that starlette-context can help with this. Starlette-context is a Python library that provides context for web apps built with the Starlette framework, including FastAPI. It lets you store information and share it between different components of an application, like middleware, paths, and asynchronous tasks.

This allows data to be shared in your application as context rather than explicitly passing it as parameters.

For the rest of the post, we’ll use the example of the ID token (bearer) that Azure Easy Auth returns in response to a request to the /.auth/me front end. This is needed to make requests to a microservice, which can then make further authenticated requests to other microservices using the same end-to-end token.

Azure Easy Auth is a Microsoft service that helps cloud applications handle authentication and authorization. It can authenticate users from their existing accounts with services like Microsoft, Google, Facebook, etc. Follow the steps in our previous blog post to add Azure Easy Auth to your application.


How to Set Up Starlette-Context?


First, we need to use pip to install the starlette-context library:

`pip install starlette-context


FastAPI lets us use dependencies to inject values, such as context data, into path operations. We can do this by using the `Depends` function and a generator that binds the context data for the duration of the request cycle.

Here’s an example of defining a path that uses a shared context to make an authenticated call to another microservice:


This code uses `my_context_dependency` to retrieve the `Authorization` header and store it in the request context. This context can then be used to include the authentication token in the headers of the aiohttp request sent to another microservice. If this microservice is a FastAPI (or Starlette) API and also needs to make authenticated calls to other microservices, simply repeat the process at the lower level until the desired granularity is reached.


You can access the context by importing `context` from the library:

`from starlette_context import context`.


We can then access the context’s components as we would data in a standard dictionary.

Here is an example: `context[“authorization”]`


Starlette-Context: Key Takeaways


We have seen how to use starlette-context with FastAPI to communicate authentication information across various microservices. You can customize the code snippet to fit your needs and microservices architecture.

Want to find out more? Have a specific question? Leave a comment and ask away!

Offres d'emploi consultant Cloud Paris Lyon Nantes Cellenza

This posts should interest you
Leave a Reply

Receive the best of Cloud, DevOps and IT news.
Receive the best of Cloud, DevOps and IT news.