Skip to content

Commit

Permalink
Merge pull request #81 from mlibrary/enable-ssl-api
Browse files Browse the repository at this point in the history
Ensure that SSL support is enabled in HttpClient
  • Loading branch information
botimer authored Jul 12, 2024
2 parents 2734efe + ce9b2ee commit c303b63
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
38 changes: 27 additions & 11 deletions apache/client/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ lauth_integration_tests = files([
'test/lauth/http_client_test.cpp',
])

os = host_machine.system()
if os == 'darwin'
httplib_links = []
else
httplib_links = ['-lssl', '-lcrypto']
endif

liblauth = shared_library(
'lauth',
lauth_sources,
Expand All @@ -39,9 +46,12 @@ liblauth = shared_library(
cpp_httplib,
json,
],
link_args: httplib_links,
install: true,
)

liblauth_dep = declare_dependency(include_directories: lauth_includes, link_with: liblauth)

install_headers(
'include/lauth/api_client.hpp',
'include/lauth/authorization_result.hpp',
Expand All @@ -64,8 +74,9 @@ if get_option('tests')
cpp_httplib,
json,
gtest,
gmock
]
gmock,
],
link_args: httplib_links,
)
test('lauth-test', tests)
endif
Expand All @@ -80,18 +91,12 @@ if get_option('integration-tests')
json,
gtest,
gmock
]
)
],
link_args: httplib_links,
)
test('lauth-integration-test', integration_tests)
endif

os = host_machine.system()
if os == 'darwin'
httplib_links = []
else
httplib_links = ['-lssl', '-lcrypto']
endif

executable(
'http-service',
files(['test/mock_service.cpp']),
Expand All @@ -110,3 +115,14 @@ executable(
],
link_args: httplib_links
)

executable(
'api-check',
files(['src/api_check.cpp']),
include_directories: lauth_includes,
dependencies: [
cpp_httplib,
liblauth_dep,
],
link_args: httplib_links,
)
35 changes: 35 additions & 0 deletions apache/client/src/api_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <exception>
#include <iostream>
#include <map>
#include <memory>

#include <lauth/authorizer.hpp>
#include <lauth/logging.hpp>

using namespace mlibrary::lauth;

int main(int argc, char **argv) {
if (argc != 6) {
std::cout << "Usage: api-check <host> <token> <uri> <ip> <username>" << std::endl;
return -1;
}

auto host = std::string(argv[1]);
auto token = std::string(argv[2]);
auto uri = std::string(argv[3]);
auto ip = std::string(argv[4]);
auto user = std::string(argv[5]);
std::cout << "Authorizing (via " << host << ") -- uri: " << uri << ", ip: " << ip << ", user: " << user << std::endl;

Logger::set(std::make_shared<Logger>(std::make_unique<StdOut>()));
auto auth = Authorizer(host, token);
Request req = Request {
.ip = ip,
.uri = uri,
.user = user
};

auto result = auth.authorize(req);
std::cout << "Authorizer worked... determination: " << result["determination"] << std::endl;
return 0;
}
4 changes: 4 additions & 0 deletions apache/client/src/lauth/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <optional>

#ifndef CPPHTTPLIB_OPENSSL_SUPPORT
#define CPPHTTPLIB_OPENSSL_SUPPORT 1
#endif

#include <httplib.h>

#include "lauth/http_params.hpp"
Expand Down
9 changes: 9 additions & 0 deletions apache/client/test/lauth/http_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using testing::_;
using testing::Eq;
using testing::HasSubstr;
using testing::IsTrue;

using namespace mlibrary::lauth;
Expand Down Expand Up @@ -85,3 +86,11 @@ TEST(HttpClient, GetRequestWithAuthorizationHeaderEncodesIt) {

EXPECT_THAT(*response, Eq(R"({"Bearer":"dGVzdA=="})"));
}

TEST(HttpClient, HttpsSchemeIsSupported) {
HttpClient client("https://example.com");

auto response = client.get("/");

EXPECT_THAT(*response, HasSubstr("Example Domain"));
}

0 comments on commit c303b63

Please sign in to comment.