From 4c45f307209db75038349968204873e66d7073b5 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Tue, 29 Jun 2021 21:58:25 +0200
Subject: [PATCH] WIP: authentication

---
 include/CMakeLists.txt                | 11 ++++---
 include/{ => caosdb}/authentication.h |  6 ++--
 include/{ => caosdb}/connection.h     | 10 +++---
 include/{ => caosdb}/constants.h.in   |  4 +--
 include/caosdb/exceptions.h           | 46 +++++++++++++++++++++++++++
 include/{ => caosdb}/utils.h          |  4 +--
 src/CMakeLists.txt                    |  5 ++-
 src/{ => caosdb}/authentication.cpp   |  2 +-
 src/{ => caosdb}/connection.cpp       | 23 ++++++++++----
 src/caosdbcli.cpp                     | 19 ++++++-----
 src/utils.cpp                         |  3 --
 11 files changed, 95 insertions(+), 38 deletions(-)
 rename include/{ => caosdb}/authentication.h (96%)
 rename include/{ => caosdb}/connection.h (96%)
 rename include/{ => caosdb}/constants.h.in (95%)
 create mode 100644 include/caosdb/exceptions.h
 rename include/{ => caosdb}/utils.h (97%)
 rename src/{ => caosdb}/authentication.cpp (98%)
 rename src/{ => caosdb}/connection.cpp (89%)
 delete mode 100644 src/utils.cpp

diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 773ab81..b60f5df 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -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)
diff --git a/include/authentication.h b/include/caosdb/authentication.h
similarity index 96%
rename from include/authentication.h
rename to include/caosdb/authentication.h
index cf8a7bf..a1fdca8 100644
--- a/include/authentication.h
+++ b/include/caosdb/authentication.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 {
diff --git a/include/connection.h b/include/caosdb/connection.h
similarity index 96%
rename from include/connection.h
rename to include/caosdb/connection.h
index 0b535fa..978fdcc 100644
--- a/include/connection.h
+++ b/include/caosdb/connection.h
@@ -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
diff --git a/include/constants.h.in b/include/caosdb/constants.h.in
similarity index 95%
rename from include/constants.h.in
rename to include/caosdb/constants.h.in
index 3d436f9..02c0341 100644
--- a/include/constants.h.in
+++ b/include/caosdb/constants.h.in
@@ -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@;
diff --git a/include/caosdb/exceptions.h b/include/caosdb/exceptions.h
new file mode 100644
index 0000000..8e70fec
--- /dev/null
+++ b/include/caosdb/exceptions.h
@@ -0,0 +1,46 @@
+/*
+ * 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
diff --git a/include/utils.h b/include/caosdb/utils.h
similarity index 97%
rename from include/utils.h
rename to include/caosdb/utils.h
index 75e04be..79393b1 100644
--- a/include/utils.h
+++ b/include/caosdb/utils.h
@@ -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>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 9b793ce..cb2e20a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -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
diff --git a/src/authentication.cpp b/src/caosdb/authentication.cpp
similarity index 98%
rename from src/authentication.cpp
rename to src/caosdb/authentication.cpp
index 9286a85..5e6a89a 100644
--- a/src/authentication.cpp
+++ b/src/caosdb/authentication.cpp
@@ -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>
diff --git a/src/connection.cpp b/src/caosdb/connection.cpp
similarity index 89%
rename from src/connection.cpp
rename to src/caosdb/connection.cpp
index 5f0d798..071714b 100644
--- a/src/connection.cpp
+++ b/src/caosdb/connection.cpp
@@ -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
diff --git a/src/caosdbcli.cpp b/src/caosdbcli.cpp
index 1d18c07..7b83487 100644
--- a/src/caosdbcli.cpp
+++ b/src/caosdbcli.cpp
@@ -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();
diff --git a/src/utils.cpp b/src/utils.cpp
deleted file mode 100644
index e1afe42..0000000
--- a/src/utils.cpp
+++ /dev/null
@@ -1,3 +0,0 @@
-#include "utils.h"
-
-namespace caosdb::utils {} // namespace caosdb::utils
-- 
GitLab