Skip to content

Porting from FastApi (Python) to Poem Rust - Feedback required #424

Answered by sunli829
chikko80 asked this question in Q&A
Discussion options

You must be logged in to vote

This is the first way, a custom Extractor is used to extract the username from Bearer.

fn parse_user_from_token(token: &str) -> Option<String> {
    todo!()
}

#[poem::async_trait]
impl<'a> poem::FromRequest<'a> for User {
    async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self> {
        use poem::web::headers::authorization::Bearer;
        use poem::web::headers::Authorization;

        let username = req
            .headers()
            .typed_get::<Authorization<Bearer>>()
            .and_then(|bearer| parse_user_from_token(bearer.0.token()))
            .ok_or_else(|| Error::from_status(StatusCode::UNAUTHORIZED))?;

        Ok(User { name: username })
    

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
2 replies
@chikko80
Comment options

@NexRX
Comment options

Answer selected by chikko80
Comment options

You must be logged in to vote
4 replies
@NexRX
Comment options

@chikko80
Comment options

@sunli829
Comment options

@NexRX
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants