Giter Site home page Giter Site logo

oatpp / example-jwt Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 7.0 38 KB

A complete example of a CRUD service with API secured with JSON Web Token (JWT)

Home Page: https://oatpp.io/

License: Apache License 2.0

CMake 5.60% Dockerfile 1.09% C++ 90.95% Shell 2.36%
oatpp jwt authorization crud swagger-ui

example-jwt's Introduction

Example-JWT Build Status

A complete example of a "CRUD" API secured with JSON Web Token (JWT) built with Oat++ and jwt-cpp.

In this example:

  • How to create CRUD endpoints.
  • How to secure endpoints with JWT.
  • How to use Oat++ ORM - PostgreSQL example.
  • How to document API with Swagger-UI and OpenApi 3.0.0.

More about Oat++:

Overview

This project is using the following oatpp modules:

3rd party dependencies

Project layout

|- CMakeLists.txt                        // projects CMakeLists.txt
|- sql/                                  // SQL migration scripts
|- src/
|   |
|   |- controller/                       // Folder containing REST Controllers (AuthController, StoryController)
|   |- db/                               // Folder containing database clients
|   |- dto/                              // DTOs are declared here
|   |- service/                          // Service business logic classes (AuthService, StoryService)
|   |- AppComponent.hpp                  // Service config
|   |- DatabaseComponent.hpp             // Database config
|   |- SwaggerComponent.hpp              // Swagger-UI config
|   |- App.cpp                           // main() is here
|
|- test/                                 // test folder
|- utility/install-oatpp-modules.sh      // utility script to install required oatpp-modules.

example-jwt's People

Contributors

jerrysb03 avatar lganzzzo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

example-jwt's Issues

Got some troubles compiling this example with conan

Hello,

I'm trying to tweak this example in order to use conan. So far, I have the following conanfile.txt file:

[requires]
oatpp/1.3.0
oatpp-swagger/1.3.0
oatpp-postgresql/1.3.0
oatpp-openssl/1.3.0
jwt-cpp/0.6.0
openssl/3.0.1
picojson/cci.20210117

[generators]
cmake

I have tweaked the CMakeLists.txt file:

## include directories

target_include_directories(example-jwt-lib PUBLIC src)

##find_package(OpenSSL 1.1 REQUIRED)

## link libs

##find_package(oatpp              1.3.0 REQUIRED)
##find_package(oatpp-swagger      1.3.0 REQUIRED)
##find_package(oatpp-postgresql   1.3.0 REQUIRED)
##find_package(oatpp-openssl      1.3.0 REQUIRED)

##find_package(jwt-cpp REQUIRED PATHS /usr/local/cmake)

target_link_libraries(example-jwt-lib
        # oatpp
        PUBLIC CONAN_PKG::oatpp
        PUBLIC CONAN_PKG::oatpp-swagger
        PUBLIC CONAN_PKG::oatpp-postgresql
        PUBLIC CONAN_PKG::oatpp-openssl

        # third-party
        PUBLIC CONAN_PKG::picojson
        PUBLIC CONAN_PKG::jwt-cpp
        PUBLIC CONAN_PKG::openssl
        #PUBLIC CONAN_PKG::Crypto

)

But I have the following issue:

Scanning dependencies of target example-jwt-lib
[  8%] Building CXX object CMakeFiles/example-jwt-lib.dir/src/auth/AuthHandler.cpp.o
In file included from /home/jean-pierre/acp_dev/CppRestAPI/oatpp/example-jwt/src/auth/AuthHandler.hpp:5,
                 from /home/jean-pierre/acp_dev/CppRestAPI/oatpp/example-jwt/src/auth/AuthHandler.cpp:2:
/home/jean-pierre/acp_dev/CppRestAPI/oatpp/example-jwt/src/auth/JWT.hpp:22:42: error: ‘picojson_traits’ is not a member of ‘jwt’
   22 |   jwt::verifier<jwt::default_clock, jwt::picojson_traits> m_verifier;
      |                                          ^~~~~~~~~~~~~~~
/home/jean-pierre/acp_dev/CppRestAPI/oatpp/example-jwt/src/auth/JWT.hpp:22:42: error: ‘picojson_traits’ is not a member of ‘jwt’
/home/jean-pierre/acp_dev/CppRestAPI/oatpp/example-jwt/src/auth/JWT.hpp:22:57: error: template argument 2 is invalid
   22 |   jwt::verifier<jwt::default_clock, jwt::picojson_traits> m_verifier;
      |                                                         ^
make[2]: *** [CMakeFiles/example-jwt-lib.dir/build.make:63 : CMakeFiles/example-jwt-lib.dir/src/auth/AuthHandler.cpp.o] Erreur 1
make[1]: *** [CMakeFiles/Makefile2:134 : CMakeFiles/example-jwt-lib.dir/all] Erreur 2
make: *** [Makefile:95 : all] Erreur 2

I'll continue to investigate, but If you have any clue in the meantime, I'll be gratefull.

Best regards,

Jean-Pierre

Change in some functions from JWT

Change in some JWT functions

In the auth/JWT.cpp file, in the line:

payload->userId = decoded.get_payload_claims().at("userId").as_string()

It looks like it should now be:

payload->userId = decoded.get_payload_claim("userId").as_string()

In the file auth/JWT.hpp, in the line:

jwt::verifier<jwt::default_clock, jwt::picojson_traits> m_verifier;

It looks like it should now be something like:

jwt::verifier<jwt::default_clock, jwt::traits::kazuho_picojson> m_verifier;

What is the purpose of this repo?

Looks like there was a nice idea to create JWT example 7 months ago but it simply did not happen?
I would expect that example repos are created when they have a purpose, this way they are useless and just add to a noise.
This is a plain copy of crud example that has not been worked on...
If there are no plans to update this example it is better to remove it...

[Question] Is it possible to add CORS configuration for authorized failed response?

Hello,

I'm trying to implement the authorization logic in my project,
I face a CORS problem when I get the Unauthorized (401) response from the oatpp server

image

I know there is a macro ADD_CORS that can handle the CORS problem at each END POINT
but Is it possible to add CORS configuration in my authorize callback function?

class BearerAuthorizationObject : public oatpp::web::server::handler::AuthorizationObject {
 public:
  // oatpp::String user;
  // oatpp::String password;
  oatpp::String token;
};

class MyBearerAuthorizationHandler : public oatpp::web::server::handler::BearerAuthorizationHandler {
 public:
  MyBearerAuthorizationHandler() : oatpp::web::server::handler::BearerAuthorizationHandler("my-realm") {}

  std::shared_ptr<AuthorizationObject> authorize(const oatpp::String& token) override {

    if (token == "4e99e8c12de7e01535248d2bac85e732") {
      auto obj = std::make_shared<BearerAuthorizationObject>();
      obj->user = "foo";
      obj->password = "bar";
      obj->token = token;
      return obj;
    }

    return nullptr;
  }
};

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.