changeset 24:166664f0f860

base64
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 28 Aug 2012 10:42:43 +0200
parents 62bd05fe4b7c
children dd1489098265
files CMakeLists.txt Core/HttpServer/MongooseServer.cpp Core/HttpServer/MongooseServer.h Core/Toolbox.cpp Core/Toolbox.h Resources/Configuration.json Resources/base64/base64.cpp Resources/base64/base64.h UnitTests/main.cpp
diffstat 9 files changed, 193 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Tue Aug 28 10:18:34 2012 +0200
+++ b/CMakeLists.txt	Tue Aug 28 10:42:43 2012 +0200
@@ -26,6 +26,7 @@
 
 SET(THIRD_PARTY_SOURCES
   ${CMAKE_SOURCE_DIR}/Resources/md5/md5.c
+  ${CMAKE_SOURCE_DIR}/Resources/base64/base64.cpp
   )
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/BoostConfiguration.cmake)
 include(${CMAKE_SOURCE_DIR}/Resources/CMake/DcmtkConfiguration.cmake)
--- a/Core/HttpServer/MongooseServer.cpp	Tue Aug 28 10:18:34 2012 +0200
+++ b/Core/HttpServer/MongooseServer.cpp	Tue Aug 28 10:42:43 2012 +0200
@@ -608,7 +608,9 @@
                                     const char* password)
   {
     Stop();
-    registeredUsers_[username] = password;
+
+    std::string tag = std::string(username) + ":" + std::string(password);
+    registeredUsers_.insert(Toolbox::EncodeBase64(tag));
   }
 
   void MongooseServer::SetSslEnabled(bool enabled)
--- a/Core/HttpServer/MongooseServer.h	Tue Aug 28 10:18:34 2012 +0200
+++ b/Core/HttpServer/MongooseServer.h	Tue Aug 28 10:42:43 2012 +0200
@@ -41,7 +41,7 @@
     typedef std::list<HttpHandler*> Handlers;
     Handlers handlers_;
 
-    typedef std::map<std::string, std::string> RegisteredUsers;
+    typedef std::set<std::string> RegisteredUsers;
     RegisteredUsers registeredUsers_;
 
     bool ssl_;
--- a/Core/Toolbox.cpp	Tue Aug 28 10:18:34 2012 +0200
+++ b/Core/Toolbox.cpp	Tue Aug 28 10:42:43 2012 +0200
@@ -37,7 +37,7 @@
 #endif
 
 #include "../Resources/md5/md5.h"
-
+#include "../Resources/base64/base64.h"
 
 namespace Palantir
 {
@@ -339,4 +339,11 @@
       result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16);
     }
   }
+
+
+  std::string Toolbox::EncodeBase64(const std::string& data)
+  {
+    return base64_encode(data.c_str());
+  }
+
 }
--- a/Core/Toolbox.h	Tue Aug 28 10:18:34 2012 +0200
+++ b/Core/Toolbox.h	Tue Aug 28 10:42:43 2012 +0200
@@ -60,5 +60,7 @@
 
     void ComputeMD5(std::string& result,
                     const std::string& data);
+
+    std::string EncodeBase64(const std::string& data);
   }
 }
--- a/Resources/Configuration.json	Tue Aug 28 10:18:34 2012 +0200
+++ b/Resources/Configuration.json	Tue Aug 28 10:42:43 2012 +0200
@@ -1,20 +1,37 @@
 {
+    /**
+     * General configuration of Palantir
+     **/
+
     // Path to the directory that holds the database
     "StorageDirectory" : "PalantirStorage",
 
+
+
+    /**
+     * Configuration of the HTTP server
+     **/
+
     // HTTP port for the REST services and for the GUI
     "HttpPort" : 8000,
 
+
+
+    /**
+     * Configuration of the DICOM server
+     **/
+
     // The DICOM Application Entity Title
     "DicomAet" : "ANY-SCP",
 
     // The DICOM port
     "DicomPort" : 4242,
 
-    // The list of the DICOM modalities.
-    "DicomModalities" : {
-        // "sample" : [ "SAMPLESCP", "192.168.100.42", 104 ]
-    },
+
+
+    /**
+     * Security-related options
+     **/
 
     // Whether or not SSL is enabled
     "SslEnabled" : false,
@@ -22,6 +39,26 @@
     // Path to the SSL certificate
     "SslCertificate" : "certificate.pem",
 
+    // Whether or not the password protection is enabled
+    "AuthenticationEnabled" : true,
+
+    // The list of the registered users. Because Palantir uses HTTP
+    // Basic Authentication, the passwords are stored as plain text.
+    "RegisteredUsers" : {
+        "alice" : "alicePassword"
+    },
+
+
+    /**
+     * Network topology
+     **/
+
+    // The list of the known DICOM modalities
+    "DicomModalities" : {
+        // "sample" : [ "SAMPLESCP", "192.168.100.42", 104 ]
+    },
+
+    // The list of the known Palantir peers (currently unused)
     "PalantirPeers" : {
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/base64/base64.cpp	Tue Aug 28 10:42:43 2012 +0200
@@ -0,0 +1,126 @@
+/* 
+   base64.cpp and base64.h
+
+   Copyright (C) 2004-2008 René Nyffenegger
+
+   This source code is provided 'as-is', without any express or implied
+   warranty. In no event will the author be held liable for any damages
+   arising from the use of this software.
+
+   Permission is granted to anyone to use this software for any purpose,
+   including commercial applications, and to alter it and redistribute it
+   freely, subject to the following restrictions:
+
+   1. The origin of this source code must not be misrepresented; you must not
+      claim that you wrote the original source code. If you use this source code
+      in a product, an acknowledgment in the product documentation would be
+      appreciated but is not required.
+
+   2. Altered source versions must be plainly marked as such, and must not be
+      misrepresented as being the original source code.
+
+   3. This notice may not be removed or altered from any source distribution.
+
+   René Nyffenegger rene.nyffenegger@adp-gmbh.ch
+
+*/
+
+#include "base64.h"
+#include <string.h>
+
+static const std::string base64_chars = 
+             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+             "abcdefghijklmnopqrstuvwxyz"
+             "0123456789+/";
+
+
+static inline bool is_base64(unsigned char c) {
+  return (isalnum(c) || (c == '+') || (c == '/'));
+}
+
+std::string base64_encode(const char* stringToEncode) {
+  unsigned char const* bytes_to_encode = reinterpret_cast<unsigned char const*>(stringToEncode);
+  unsigned int in_len = strlen(stringToEncode);
+  
+  std::string ret;
+  int i = 0;
+  int j = 0;
+  unsigned char char_array_3[3];
+  unsigned char char_array_4[4];
+
+  while (in_len--) {
+    char_array_3[i++] = *(bytes_to_encode++);
+    if (i == 3) {
+      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
+      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
+      char_array_4[3] = char_array_3[2] & 0x3f;
+
+      for(i = 0; (i <4) ; i++)
+        ret += base64_chars[char_array_4[i]];
+      i = 0;
+    }
+  }
+
+  if (i)
+  {
+    for(j = i; j < 3; j++)
+      char_array_3[j] = '\0';
+
+    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
+    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
+    char_array_4[3] = char_array_3[2] & 0x3f;
+
+    for (j = 0; (j < i + 1); j++)
+      ret += base64_chars[char_array_4[j]];
+
+    while((i++ < 3))
+      ret += '=';
+
+  }
+
+  return ret;
+
+}
+
+std::string base64_decode(const std::string& encoded_string) {
+  int in_len = encoded_string.size();
+  int i = 0;
+  int j = 0;
+  int in_ = 0;
+  unsigned char char_array_4[4], char_array_3[3];
+  std::string ret;
+
+  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
+    char_array_4[i++] = encoded_string[in_]; in_++;
+    if (i ==4) {
+      for (i = 0; i <4; i++)
+        char_array_4[i] = base64_chars.find(char_array_4[i]);
+
+      char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
+      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
+      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
+
+      for (i = 0; (i < 3); i++)
+        ret += char_array_3[i];
+      i = 0;
+    }
+  }
+
+  if (i) {
+    for (j = i; j <4; j++)
+      char_array_4[j] = 0;
+
+    for (j = 0; j <4; j++)
+      char_array_4[j] = base64_chars.find(char_array_4[j]);
+
+    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
+    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
+    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
+
+    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
+  }
+
+  return ret;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Resources/base64/base64.h	Tue Aug 28 10:42:43 2012 +0200
@@ -0,0 +1,4 @@
+#include <string>
+
+std::string base64_encode(const char* stringToEncode);
+std::string base64_decode(const std::string& s);
--- a/UnitTests/main.cpp	Tue Aug 28 10:18:34 2012 +0200
+++ b/UnitTests/main.cpp	Tue Aug 28 10:42:43 2012 +0200
@@ -266,6 +266,13 @@
   ASSERT_EQ("d41d8cd98f00b204e9800998ecf8427e", s);
 }
 
+TEST(Toolbox, Base64)
+{
+  ASSERT_EQ("", Toolbox::EncodeBase64(""));
+  ASSERT_EQ("YQ==", Toolbox::EncodeBase64("a"));
+  ASSERT_EQ("SGVsbG8gd29ybGQ=", Toolbox::EncodeBase64("Hello world"));
+}
+
 
 int main(int argc, char **argv)
 {