poem_openapi

Derive Macro SecurityScheme

Source
#[derive(SecurityScheme)]
{
    // Attributes available to this derive:
    #[oai]
}
Expand description

Define an OpenAPI Security Scheme.

§Macro parameters

AttributeDescriptionTypeOptional
renameRename the security scheme.stringY
tyThe type of the security scheme. (api_key, basic, bearer, oauth2, openid_connect)stringN
key_inapi_key The location of the API key. Valid values are “query”, “header” or “cookie”. (query, header, cookie)stringY
key_nameapi_key The name of the header, query or cookie parameter to be used..stringY
bearer_formatbearer A hint to the client to identify how the bearer token is formatted. Bearer tokens are usually generated by an authorization server, so this information is primarily for documentation purposes.stringY
flowsoauth2 An object containing configuration information for the flow types supported.OAuthFlowsY
openid_connect_urlOpenId Connect URL to discover OAuth2 configuration values.stringY
checkerSpecify a function to check the original authentication information and convert it to the return type of this function. This function must return Option<T> or poem::Result<T>, with None meaning a General Authorization error and an Err reflecting the error supplied.stringY

§OAuthFlows

AttributedescriptionTypeOptional
implicitConfiguration for the OAuth Implicit flowOAuthFlowY
passwordConfiguration for the OAuth Resource Owner Password flowOAuthFlowY
client_credentialsConfiguration for the OAuth Client Credentials flowOAuthFlowY
authorization_codeConfiguration for the OAuth Authorization Code flowOAuthFlowY

§OAuthFlow

AttributedescriptionTypeOptional
authorization_urlimplicit authorization_code The authorization URL to be used for this flow.stringY
token_urlpassword client_credentials authorization_code The token URL to be used for this flow.stringY
refresh_urlThe URL to be used for obtaining refresh tokens.stringY
scopesThe available scopes for the OAuth2 security scheme.OAuthScopesY

§Multiple Authentication Methods

When SecurityScheme macro is used with an enumerated type, it is used to define multiple authentication methods.

use poem_openapi::{OpenApi, SecurityScheme};
use poem_openapi::payload::PlainText;
use poem_openapi::auth::{ApiKey, Basic};

#[derive(SecurityScheme)]
#[oai(ty = "basic")]
struct MySecurityScheme1(Basic);

#[derive(SecurityScheme)]
#[oai(ty = "api_key", key_name = "X-API-Key", key_in = "header")]
struct MySecurityScheme2(ApiKey);

#[derive(SecurityScheme)]
enum MySecurityScheme {
    MySecurityScheme1(MySecurityScheme1),
    MySecurityScheme2(MySecurityScheme2),
}

struct MyApi;

#[OpenApi]
impl MyApi {
    #[oai(path = "/test", method = "get")]
    async fn test(&self, auth: MySecurityScheme) -> PlainText<String> {
        match auth {
            MySecurityScheme::MySecurityScheme1(auth) => {
                PlainText(format!("basic: {}", auth.0.username))
            }
            MySecurityScheme::MySecurityScheme2(auth) => {
                PlainText(format!("api-key: {}", auth.0.key))
            }
        }
    }
}