3

我正在使用 sqlx 在 actix-web 2.0.0 中实现身份验证提取器来访问数据库。我有这个代码:

use actix_web::{dev, web, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorUnauthorized;
use futures::future::{ok, err, Ready};
use sqlx::PgPool;
use serde_derive::Deserialize;

use crate::model::User;

#[derive(Debug, Deserialize)]
pub struct Auth {
    user_id: u32,
}

impl FromRequest for Auth {
    type Error = Error;
    type Future = Ready<Result<Self, Self::Error>>;
    type Config = ();

    fn from_request(req: &HttpRequest, _: &mut dev::Payload) -> Self::Future {
        use actix_web::HttpMessage;

        let db_pool = req.app_data::<web::Data<PgPool>>().unwrap();
        let error = ErrorUnauthorized("{\"details\": \"Please log in\"}");

        if let Some(session_id) = req.cookie("sessionid") {
            log::info!("Session id {}", session_id);
            // let result = User::find_by_session(db_pool.get_ref(), session_id).await;
            ok(Auth { user_id: 0 })
        } else {
            err(error)
        }

    }
}

当然,我不能await在那里使用。我看到了一个使用type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>和返回的例子,Box::pin(async move { ... })但我无法让它工作(有生命周期的问题req)。

4

1 回答 1

8

我设法做到了。我之前提取了cookie,async move所以没有问题req

use std::pin::Pin;
use futures::Future;
use actix_web::{dev, web, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorUnauthorized;
use sqlx::PgPool;
use serde_derive::Deserialize;

use crate::model::User;

#[derive(Debug, Deserialize)]
pub struct Auth {
    user_id: u32,
}

impl FromRequest for Auth {
    type Error = Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self, Self::Error>>>>;
    type Config = ();

    fn from_request(req: &HttpRequest, _: &mut dev::Payload) -> Self::Future {
        use actix_web::HttpMessage;

        let db_pool = req.app_data::<web::Data<PgPool>>().unwrap().clone();
        let cookie = req.cookie("sessionid");

        Box::pin(async move {
            let error = Err(ErrorUnauthorized("{\"details\": \"Please log in\"}"));

            if let Some(session_id) = cookie {
                let result = User::find_by_session(db_pool.get_ref(), session_id).await;
                // auth code
                Ok(Auth { user_id: 0 })
            } else {
                error
            }
        })
    }
}
于 2020-08-10T15:16:30.980 回答