oatpp / oatpp-swagger

OpenApi 3.0.0 docs + Swagger UI for oatpp services
https://oatpp.io/
Apache License 2.0
94 stars 53 forks source link

Add authorization on the swagger controller #82

Open MarkosKon opened 1 year ago

MarkosKon commented 1 year ago

I want to protect with HTTP Basic authentication the swagger URLs themselves. I tried the following:

oatpp::base::Environment::init();
AppComponent components;
OATPP_COMPONENT(const std::shared_ptr<oatpp::web::server::HttpRouter>, router);
auto misc_controller = router->addController(std::make_shared<MiscController>());

oatpp::web::server::api::Endpoints docEndpoints{};
docEndpoints.append(misc_controller->getEndpoints());
auto swagger_controller = oatpp::swagger::Controller::createShared(docEndpoints);
swagger_controller->setDefaultAuthorizationHandler(
          std::make_shared<MyBasicAuthorizationHandler>());
router->addController(swagger_controller);

I can probably implement this with a request interceptor, but it's not ideal because I have to manually keep track of the swagger paths.

Is there a way to do this by using my custom basic authorization handler?