Derive Macro apistos::ApiSecurity

source ·
#[derive(ApiSecurity)]
{
    // Attributes available to this derive:
    #[openapi_security]
}
Expand description

Generates a reusable OpenAPI security scheme.

This #[derive] macro should be used in combination with api_operation. The macro requires one and only one openapi_security.

use apistos::ApiSecurity;

#[derive(ApiSecurity)]
#[openapi_security(scheme(security_type(api_key(name = "api_key", api_key_in = "header"))))]
pub struct ApiKey;

§#[openapi_security(...)] options:

  • name = "..." an optional name for your security definition. If not provided, the struct ident will be used.
  • scheme(...) a required parameter with:
    • description = "..." an optional description
    • security_type(...) a required parameter with one of
      • oauth2(flows(...)) with
        • implicit(...) with authorization_url = "..." a required parameter, refresh_url = "..." an optional parameter and scopes(scope = "...", description = "...") a list of scopes
        • password(...) with token_url = "..." a required parameter, refresh_url = "..." an optional parameter and scopes(scope = "...", description = "...") a list of scopes
        • client_credentials(...) with token_url = "..." a required parameter, refresh_url = "..." an optional parameter and scopes(scope = "...", description = "...") a list of scopes
        • authorization_code(...) with token_url = "..." a required parameter, refresh_url = "..." an optional parameter and scopes(scope = "...", description = "...") a list of scopes
      • api_key(...) with
        • name = "..." a required parameter
        • api_key_in = "..." a required parameter being one of query, header or cookie
      • http(...) with
        • scheme = "..." a required parameter
        • bearer_format = "..." a required parameter
      • open_id_connect(open_id_connect_url = "...")

§Examples:

§oauth2

use apistos::ApiSecurity;

#[derive(ApiSecurity)]
#[openapi_security(scheme(security_type(oauth2(flows(implicit(
  authorization_url = "https://authorize.com",
  refresh_url = "https://refresh.com",
  scopes(scope = "all:read", description = "Read all the things"),
  scopes(scope = "all:write", description = "Write all the things")
))))))]
pub struct ApiKey;

§api_key

use apistos::ApiSecurity;

#[derive(ApiSecurity)]
#[openapi_security(scheme(security_type(api_key(name = "api_key", api_key_in = "header"))))]
pub struct ApiKey;

§http

use apistos::ApiSecurity;

#[derive(ApiSecurity)]
#[openapi_security(scheme(security_type(http(scheme = "bearer", bearer_format = "JWT"))))]
pub struct ApiKey;

§open_id_connect

use apistos::ApiSecurity;

#[derive(ApiSecurity)]
#[openapi_security(scheme(security_type(open_id_connect(open_id_connect_url = "https://connect.com"))))]
pub struct ApiKey;