Member-only story

REST APIs & Custom Authentication in RUST— Writing Extractors in Axum

Shanmukh Sista
6 min readOct 6, 2022
Writing Custom Middleware in Rust : The right way

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:

  1. Parse Headers every time and extract come information.
  2. Throw errors in case the information is missing.
  3. 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.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

No responses yet

Write a response