LCOV - code coverage report
Current view: top level - Servidor - Tokenizer.cpp (source / functions) Hit Total Coverage
Test: coverage_merged.info Lines: 58 59 98.3 %
Date: 2016-07-11 17:43:33 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Tokenizer.cpp
       3             :  *
       4             :  *  Created on: Jun 3, 2016
       5             :  *      Author: tobias
       6             :  */
       7             : 
       8             : #include "Tokenizer.h"
       9             : 
      10             : using namespace std;
      11             : 
      12         194 : Tokenizer::Tokenizer(DatabaseManager *database) {
      13         194 :         this->database = database;
      14             : 
      15         194 : }
      16             : 
      17             : //format year,month,day,hours,min,sec
      18          35 : bool Tokenizer::timeStampExpired(std::string timeStamp){
      19          35 :         vector<string> times;
      20          35 :         time_t now = time(NULL);
      21             : 
      22          70 :         stringstream ss(timeStamp);
      23          70 :         string cell;
      24         280 :         while (ss.good()) {
      25         210 :                 getline(ss, cell, ',');
      26         210 :                 times.push_back(cell);
      27             :         }
      28          35 :         if (times.size() != TS_COUNT){
      29           0 :                 LOGG(WARNING) << "Extra data in timeStamp of token";
      30             :         }
      31          35 :         struct tm _then = {0};
      32          35 :         _then.tm_hour = stoi(times[TS_HOUR]) ;
      33          35 :         _then.tm_min = stoi(times[TS_MIN]);
      34          35 :         _then.tm_sec = stoi(times[TS_SEC]);
      35          35 :         _then.tm_year = stoi(times[TS_YEAR]);
      36          35 :         _then.tm_mon = stoi(times[TS_MON]);
      37          35 :         _then.tm_mday = stoi(times[TS_DAY]);
      38             : 
      39          35 :         double elapsedTime = difftime(now, mktime(&_then));
      40             : 
      41          70 :         return elapsedTime > (double)EXPIRATION_TIME;
      42             : 
      43             : }
      44             : 
      45          34 : std::string Tokenizer::getNowTimeStamp(){
      46          34 :         time_t now = time(NULL);
      47          34 :         struct tm * gmt = localtime(&now);
      48          68 :         std::string year, month, day, hours, min, sec;
      49          34 :         year = to_string(gmt->tm_year);
      50          34 :         month = to_string(gmt->tm_mon);
      51          34 :         day = to_string(gmt->tm_mday);
      52          34 :         hours = to_string(gmt->tm_hour);
      53          34 :         min = to_string(gmt->tm_min);
      54          34 :         sec = to_string(gmt->tm_sec);
      55             : 
      56          34 :         std::string timeStamp = year + "," + month + ",";
      57          34 :         timeStamp += day + "," + hours + "," + min + ",";
      58          34 :         timeStamp += sec;
      59          68 :         return timeStamp;
      60             : }
      61             : 
      62          34 : std::string Tokenizer::newToken(std::string mail, std::string pass){
      63             :         //hash
      64             :     unsigned char digest[16];
      65          34 :     string timeStamp = getNowTimeStamp();
      66             : 
      67          68 :         std::string toHash = mail + pass + timeStamp;
      68          34 :     const char* string = toHash.c_str();
      69          34 :     const unsigned char* unsignedString = (unsigned char*) string;
      70             :     //cout << "string length: " << strlen(string) << endl;
      71             : 
      72             :         MD5_CTX ctx;
      73          34 :         MD5_Init(&ctx);
      74          34 :         MD5_Update(&ctx, unsignedString, strlen(string));
      75          34 :         MD5_Final(digest, &ctx);
      76             : 
      77             :         char mdString[33];
      78         578 :         for (int i = 0; i < 16; i++)
      79         544 :         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);
      80             : 
      81             :         //cout << "md5 digest: " << mdString << endl;
      82          34 :         std::string hashed(mdString);
      83             : 
      84          34 :         database->addEntry(TOKENIZER_PREFIX + hashed, timeStamp);
      85          68 :         return hashed;
      86             : 
      87             : }
      88             : 
      89          40 : bool Tokenizer::hasExpired(std::string token){
      90          40 :         std::string timeStamp;
      91          40 :         if (database->getEntry(TOKENIZER_PREFIX + token,timeStamp)){
      92          35 :                 return this->timeStampExpired(timeStamp);
      93             :         }
      94           5 :         return true;
      95             : 
      96             : }
      97             : 
      98          31 : void Tokenizer::remove(std::string token){
      99          31 :         this->database->deleteEntry(TOKENIZER_PREFIX + token);
     100          31 : }
     101             : 
     102         374 : Tokenizer::~Tokenizer() {
     103             : 
     104         440 : }
     105             : 

Generated by: LCOV version 1.12-4-g04a3c0e