Coder Social home page Coder Social logo

Basic Authentication about oatpp HOT 2 CLOSED

oatpp avatar oatpp commented on May 10, 2024
Basic Authentication

from oatpp.

Comments (2)

eduardo-tadeu avatar eduardo-tadeu commented on May 10, 2024 1

thank you very much!

from oatpp.

lganzzzo avatar lganzzzo commented on May 10, 2024

Hello @eduardo-tadeu ,

Thanks for the question!

The simplest example:

class MyController : public oatpp::web::server::api::ApiController {
public:

  MyController(const std::shared_ptr<ObjectMapper>& objectMapper)
    : oatpp::web::server::api::ApiController(objectMapper)
  {
    /*set default auth handler for controller*/
    setDefaultAuthorizationHandler(std::make_shared<oatpp::web::server::handler::BasicAuthorizationHandler>("default-test-realm"));
  }

public:

  static std::shared_ptr<DefaultBasicAuthorizationController> createShared(
    const std::shared_ptr<ObjectMapper>& objectMapper = OATPP_GET_COMPONENT(std::shared_ptr<ObjectMapper>)
  ) {
    return std::make_shared<MyController>(objectMapper);
  }

#include OATPP_CODEGEN_BEGIN(ApiController)

  ENDPOINT("GET", "default-basic-authorization", basicAuthorization,
           AUTHORIZATION(std::shared_ptr<oatpp::web::server::handler::DefaultBasicAuthorizationObject>, authObject)) {

    if(authObject->userId == "admin" && authObject->password == "password") {
      return createResponse(Status::CODE_200, "Hello");
    } else {
      return createResponse(Status::CODE_401, "Unauthorized");
    }

  }

#include OATPP_CODEGEN_END(ApiController)

};

This is a simplest example using default basic auth handler with default auth object.

You may also find more examples in the oatpp tests:
https://github.com/oatpp/oatpp/blob/master/test/oatpp/web/app/BasicAuthorizationController.hpp

Please let me know if you have more questions.

Best Regards,
Leonid

from oatpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.