Member-only story
REST APIs & Custom Authentication in RUST— Writing Extractors in Axum

In this short post, we’ll look at writing some custom logic as a middleware for REST APIs using Rust And Axum Framework. We’ll start by creating a new Rust binary project and add dependencies for Axum Framework. The actual problem statement is given below:
Problem Statement
A common requirement while writing REST Apis is that we may need to extract User information from the Request Context. In a majority of the use cases, it can as simple as extracting the User Authorization Header. It may be a JWT , or it may be a simple user object that is serialized — ( if you already have an auth proxy setup. ).
In either case, we want the following functionality within our APIs:
- Parse Headers every time and extract come information.
- Throw errors in case the information is missing.
- Provide a User Object to the API handlers, so that they can use it.
We’ll look at these use cases in Rust and Axum framework, and write a very simple application that enables us to do everything in a nice, concise way.
Project Setup
Let’s start by setting up a new Rust Project. We’ll create a new Binary.