use salt store in posbookie
authorOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 03:55:25 +0000 (22:55 -0500)
committerOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 03:55:25 +0000 (22:55 -0500)
database/gen-cpp/PosBookie_server.skeleton.cpp

index 2c75465..79bdc71 100644 (file)
@@ -11,6 +11,7 @@
 #include <thrift/transport/TSocket.h>
 #include <thrift/transport/TTransportUtils.h>
 
+#include "salt_store.h"
 #include "sha1.h"
 #include <stdio.h>
 
@@ -30,6 +31,8 @@ class PosBookieHandler : virtual public PosBookieIf {
   shared_ptr<TProtocol> protocol;
   PosClient client;
 
+  SaltStore * saltStore;
+
   std::string getAuthString() {
     std::string s;
     this->client.getSalt(s);
@@ -49,32 +52,38 @@ class PosBookieHandler : virtual public PosBookieIf {
       printf("failed to contact server");
       exit(1);
     }
+
+    saltStore = new SaltStore(120, 256, "SEKRET!");
   }
 
   void ping() { }
 
   void getSalt(std::string& _return) {
-    this->client.getSalt(_return);
+    _return = this->saltStore->getSalt();
   }
 
   E_PURCHASE_STATUS::type purchaseItems(const std::string& auth, const std::string& dataToHash, const std::vector<int64_t> & upcs) {
-    try {
-      int32_t total_price = 0;
-      std::vector<int64_t>::iterator i;
-      for(i = upcs.begin(); i != upcs.end(); i++) {
-        total_price += this->getUPCPrice(*i);
-      }
-
-      int32_t accountBalance = this->client.getHashAccountBalance(dataToHash);
-      if(accountBalance < total_price) {
-        this->client.performTransactionOnHash(this->getAuthString(), dataToHash, total_price * -1);
-        return E_PURCHASE_STATUS::type::EPS_SUCCESS;
+    if(saltStore->consumeKey(auth)) {  
+      try {
+        int32_t total_price = 0;
+        std::vector<int64_t>::iterator i;
+        for(i = upcs.begin(); i != upcs.end(); i++) {
+          total_price += this->getUPCPrice(*i);
+        }
+
+        int32_t accountBalance = this->client.getHashAccountBalance(dataToHash);
+        if(accountBalance < total_price) {
+          this->client.performTransactionOnHash(this->getAuthString(), dataToHash, total_price * -1);
+          return E_PURCHASE_STATUS::type::EPS_SUCCESS;
+        }
+
+        return E_PURCHASE_STATUS::type::EPS_NOMONEY;
+      } catch(...) {
+        return E_PURCHASE_STATUS::type::EPS_FAIL;
       }
-
-      return E_PURCHASE_STATUS::type::EPS_NOMONEY;
-    } catch(...) {
-      return E_PURCHASE_STATUS::type::EPS_FAIL;
     }
+
+    return E_PURCHASE_STATUS::type::EPS_FAIL;
   }
 
   int32_t getUPCPrice(const int64_t upc) {