Skip to content
Snippets Groups Projects
Verified Commit 4c45f307 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: authentication

parent 1cfb409a
No related branches found
No related tags found
No related merge requests found
Pipeline #9476 failed
......@@ -20,14 +20,15 @@
# add all header files to this list
set(libcaosdb_INCL
${CMAKE_CURRENT_BINARY_DIR}/constants.h
${CMAKE_CURRENT_SOURCE_DIR}/connection.h
${CMAKE_CURRENT_SOURCE_DIR}/authentication.h
${CMAKE_CURRENT_SOURCE_DIR}/utils.h
${CMAKE_CURRENT_BINARY_DIR}/caosdb/constants.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/utils.h
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/exceptions.h
)
# pass variable to parent scope
set(libcaosdb_INCL ${libcaosdb_INCL} PARENT_SCOPE)
# initialize constants
configure_file(constants.h.in constants.h)
configure_file(caosdb/constants.h.in caosdb/constants.h)
......@@ -19,8 +19,8 @@
*
*/
#ifndef AUTHENTICATION_H
#define AUTHENTICATION_H
#ifndef CAOSDB_AUTHENTICATION_H
#define CAOSDB_AUTHENTICATION_H
/**
* @file authentication.h
* @author Timm Fitschen
......@@ -32,7 +32,7 @@
#include <memory>
#include <grpc/grpc.h>
#include <grpcpp/security/credentials.h>
#include "utils.h"
#include "caosdb/utils.h"
namespace caosdb {
namespace authentication {
......
......@@ -19,8 +19,8 @@
*
*/
#ifndef CONNECTION_H
#define CONNECTION_H
#ifndef CAOSDB_CONNECTION_H
#define CAOSDB_CONNECTION_H
/**
* @file connection.h
* @author Timm Fitschen
......@@ -29,7 +29,7 @@
*/
#include <string>
#include <iosfwd>
#include "authentication.h"
#include "caosdb/authentication.h"
#include <memory>
#include <grpc/grpc.h>
#include <grpcpp/channel.h>
......@@ -48,7 +48,7 @@ class VersionInfo;
} // namespace info
} // namespace caosdb
namespace caosdb {
namespace caosdb::connection {
using caosdb::authentication::Authenticator;
using caosdb::info::v1alpha1::GeneralInfoService;
using caosdb::info::v1alpha1::VersionInfo;
......@@ -142,5 +142,5 @@ public:
-> GeneralInfoService::Stub &;
[[nodiscard]] auto getVersionInfo() const -> VersionInfo;
};
} // namespace caosdb
} // namespace caosdb::connection
#endif
......@@ -20,8 +20,8 @@
*
*/
#ifndef CAOSDB_CONFIG_H
#define CAOSDB_CONFIG_H
#ifndef CAOSDB_CONSTANTS_H
#define CAOSDB_CONSTANTS_H
namespace caosdb {
// clang-format off
constexpr int LIBCAOSDB_VERSION_MAJOR = @libcaosdb_VERSION_MAJOR@;
......
/*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef CAOSDB_EXCEPTIONS_H
#define CAOSDB_EXCEPTIONS_H
#include <stdexcept>
#include <string>
namespace caosdb::exceptions {
using std::runtime_error;
/**
* @brief Exception for authentication errors.
*/
class AuthenticationError : public runtime_error {
public:
explicit AuthenticationError(const std::string &what_arg)
: runtime_error(what_arg) {}
};
class ConnectionError : public runtime_error {
public:
explicit ConnectionError(const std::string &what_arg)
: runtime_error(what_arg) {}
};
} // namespace caosdb::exceptions
#endif
......@@ -19,8 +19,8 @@
*
*/
#ifndef UTILS_H
#define UTILS_H
#ifndef CAOSDB_UTILS_H
#define CAOSDB_UTILS_H
#include <string_view>
#include <fstream>
#include <string>
......
......@@ -21,9 +21,8 @@
# add all source files to this list
set(libcaosdb_SRC
src/utils.cpp
src/authentication.cpp
src/connection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.cpp
)
# pass variable to parent scope
......
......@@ -20,7 +20,7 @@
*/
#include <string>
#include <memory>
#include "authentication.h"
#include "caosdb/authentication.h"
#include <grpcpp/impl/codegen/status.h>
#include <grpcpp/impl/codegen/string_ref.h>
#include <map>
......
......@@ -20,9 +20,9 @@
*
*/
#include "connection.h"
#include "utils.h"
#include "authentication.h"
#include "caosdb/connection.h"
#include "caosdb/utils.h"
#include "caosdb/authentication.h"
#include <grpcpp/create_channel.h>
#include <grpcpp/impl/codegen/client_context.h>
#include <grpcpp/impl/codegen/status.h>
......@@ -32,9 +32,12 @@
#include <string>
#include "caosdb/info/v1alpha1/main.grpc.pb.h"
#include "caosdb/info/v1alpha1/main.pb.h"
#include "caosdb/exceptions.h"
namespace caosdb {
namespace caosdb::connection {
using caosdb::authentication::Authenticator;
using caosdb::exceptions::AuthenticationError;
using caosdb::exceptions::ConnectionError;
using caosdb::info::v1alpha1::GeneralInfoService;
using caosdb::info::v1alpha1::GetVersionInfoRequest;
using caosdb::info::v1alpha1::GetVersionInfoResponse;
......@@ -154,10 +157,18 @@ auto CaosDBConnection::getGeneralInfoService() const
stub_->GetVersionInfo(&context, request, &response);
if (!status.ok()) {
throw std::runtime_error(status.error_message());
switch (status.error_code()) {
case grpc::StatusCode::UNAUTHENTICATED:
throw AuthenticationError(status.error_message());
case grpc::StatusCode::UNAVAILABLE:
throw ConnectionError(status.error_message());
default:
std::cout << status.error_code() << "\n";
throw std::runtime_error(status.error_message());
}
}
VersionInfo result(response.version_info());
return result;
}
} // namespace caosdb
} // namespace caosdb::connection
......@@ -25,10 +25,10 @@
#include <memory>
#include <string>
#include "caosdb/info/v1alpha1/main.pb.h"
#include "constants.h"
#include "connection.h"
#include "authentication.h"
#include "utils.h"
#include "caosdb/constants.h"
#include "caosdb/connection.h"
#include "caosdb/authentication.h"
#include "caosdb/utils.h"
auto main() -> int {
......@@ -44,14 +44,17 @@ auto main() -> int {
const auto port_str =
caosdb::utils::get_env_var("CAOSDB_SERVER_PORT", "8000");
const auto port = std::stoi(port_str);
const auto user = caosdb::utils::get_env_var("CAOSDB_USER", "admin");
const auto password = caosdb::utils::get_env_var("CAOSDB_PASSWORD", "caosdb");
auto auth =
std::make_shared<caosdb::authentication::PlainPasswordAuthenticator>(
"tf", "vuY36moa");
auto cacert = std::make_shared<caosdb::PemFileCACertProvider>(pem_file);
auto config = std::make_shared<caosdb::SslCaosDBConnectionConfig>(
user, password);
auto cacert =
std::make_shared<caosdb::connection::PemFileCACertProvider>(pem_file);
auto config = std::make_shared<caosdb::connection::SslCaosDBConnectionConfig>(
host, port, cacert, auth);
caosdb::CaosDBConnection connection(config);
caosdb::connection::CaosDBConnection connection(config);
std::cout << std::endl << connection << std::endl;
const auto &v_info = connection.getVersionInfo();
// const auto &build = v_info.build();
......
#include "utils.h"
namespace caosdb::utils {} // namespace caosdb::utils
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment