Skip to content

Question: Accessing State in OpenApiFromRequest Similar to FromRequest #151

@NewtTheWolf

Description

@NewtTheWolf

Hello rocket_okapi team,

I'm currently working on the zitadel-rust project and generating OpenAPI documentation using the OpenApiFromRequest trait. You can find the relevant PR here: smartive/zitadel-rust#559.

In this project, I need to configure the SecurityScheme for OpenID Connect, and the authority URL for this configuration is stored in Rocket's State.

In a typical request guard implementation using FromRequest, I can access the request object and retrieve the configuration from State like this:

#[async_trait]
impl<'request> FromRequest<'request> for &'request IntrospectedUser {
    type Error = &'request IntrospectionGuardError;

    async fn from_request(request: &'request Request<'_>) -> Outcome<Self, Self::Error> {
        // Accessing the authorization header
        let auth: Vec<_> = request.headers().get("authorization").collect();
        if auth.len() > 1 {
            return Outcome::Error((Status::BadRequest, &IntrospectionGuardError::InvalidHeader));
        } else if auth is_empty() {
            return Outcome::Error((Status::Unauthorized, &IntrospectionGuardError::Unauthorized));
        }

        let token = auth[0];
        if !token.starts_with("Bearer ") {
            return Outcome::Error((Status::Unauthorized, &IntrospectionGuardError::WrongScheme));
        }

        // Accessing the configuration from State
        let config = request.rocket().state::<IntrospectionConfig>();
        if config is_none() {
            return Outcome::Error((Status::InternalServerError, &IntrospectionGuardError::MissingConfig));
        }

        // Further processing...
    }
}

However, when working within the OpenApiFromRequest trait, I haven't found a way to similarly access the request object or State to retrieve the configuration data. Currently, I'm using figment to extract the configuration, like this:

let config: IntrospectionRocketConfig = figment
    .extract()
    .expect("authority must be set in Rocket.toml");

But I would prefer to directly access the State via the request, similar to how it's done in FromRequest, to keep consistency and avoid duplicating configuration extraction logic.

My Question:

Is it possible to access the request object within OpenApiFromRequest, or is there another recommended way to retrieve the State while generating OpenAPI documentation with rocket_okapi?

Thank you for your assistance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions