Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specifying multiple security requirement keys #813

Merged
merged 7 commits into from
Dec 14, 2023

Conversation

abs0luty
Copy link
Contributor

@abs0luty abs0luty commented Dec 2, 2023

Hi! The first contribution to this repo, so I may have missed some things.

I use utoipa in my project and I faced a problem of not being able to use multiple security requirement keys.

As described in Swagger docs: Some REST APIs support several authentication types. The security section lets you combine the security requirements using logical OR and AND to achieve the desired result.

I made an issue. Then, I resolved it myself. Now you can use multiple security requirements separated by a comma in #[utoipa::path]. Example:

#[utoipa::path(
      delete,
      path = "/todo/{id}",
      responses(
          (status = 200, description = "Todo marked done successfully"),
          (status = 401, description = "Unauthorized to delete Todo", body = TodoError, example = json!(TodoError::Unauthorized(String::from("missing api key")))),
          (status = 404, description = "Todo not found", body = TodoError, example = json!(TodoError::NotFound(String::from("id = 1"))))
      ),
      params(
          ("id" = i32, Path, description = "Todo database id")
      ),
      security(
          // literally means (api_key and api_key2) or api_key3
          ("api_key" = [], "api_key2" = []),
          ("api_key3" = []),
      )
  )]

Also, now you can construct those with add() method:

SecurityRequirement::default()
  .add("api_key", ["test"])
  .add("api_key2", ["test"])

I didn't change the names of already existing methods and types. Additionally, as you can see new security(...) syntax is compatible with the old one. So it isn't really a breaking change.

Resolves #803. Resolves #717

@abs0luty abs0luty changed the title Add multiple security requirements Add support for specifying multiple security requirements Dec 3, 2023
Copy link
Owner

@juhaku juhaku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice 👍 Just some minor things to check out and it is done.

utoipa-gen/src/security_requirement.rs Outdated Show resolved Hide resolved
utoipa-gen/tests/openapi_derive_test.rs Show resolved Hide resolved
utoipa/src/openapi/security.rs Show resolved Hide resolved
@abs0luty abs0luty requested a review from juhaku December 4, 2023 14:33
@abs0luty abs0luty changed the title Add support for specifying multiple security requirements Add support for specifying multiple security requirement keys Dec 4, 2023
@abs0luty
Copy link
Contributor Author

abs0luty commented Dec 5, 2023

@juhaku fixed it.

Copy link
Owner

@juhaku juhaku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👍

@juhaku juhaku merged commit 7493c33 into juhaku:master Dec 14, 2023
10 checks passed
@juhaku
Copy link
Owner

juhaku commented Aug 16, 2024

Future support for duplicate name keys in security: attribute of the OpenApi #992

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants