git.fiddlerwoaroof.com
token.cc
7684972a
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
bef74c28
  * terms of the  GNU General Public License version 2  or
7684972a
  * later.
  *
  * You should have received a copy of the license with this
  * file. If not, you will find a copy in the "LICENSE" file
  * at https://github.com/cjdev/dual-control.
  */
 
55259c47
 #include <string>
 #include <vector>
 #include <memory>
4bf199d2
 #include <fstream>
 #include <sstream>
 #include <iostream>
1c7f8bf0
 
bef74c28
 #include "token.h"
 #include "user.h"
 
 namespace
 {
34b9d4f7
 class user_token_supplier_impl : public user_token_supplier_ifc
0e218820
 {
 private:
     file_reader file_reader_;
 public:
34b9d4f7
     user_token_supplier_impl (file_reader &file_reader) : file_reader_
         (file_reader) {}
0e218820
     std::string token (user &user)
     {
4bf199d2
         const std::string file_path (user.home_directory() + "/.dual_control");
         std::ifstream token_file;
         std::string fetched_token;
 
bd15e7c1
         bool token_file_opened = file_reader_.open (token_file, file_path);
4bf199d2
 
         if (!token_file_opened) {
             return "";
         }
 
34b9d4f7
         return file_reader_.getline (token_file, fetched_token);
0e218820
     }
 };
 
 class file_reader_impl : public file_reader_ifc
 {
 public:
bd15e7c1
     bool open (std::ifstream &token_file, const std::string &file_path)
     {
         token_file.open (file_path);
4bf199d2
         return token_file.good();
     }
bd15e7c1
     std::string getline (std::ifstream &token_file, std::string &fetched_token)
     {
         std::getline (token_file, fetched_token);
4bf199d2
         return fetched_token;
0e218820
     }
 };
bef74c28
 }
 
 file_reader file_reader::create ()
 {
34b9d4f7
     return file_reader (file_reader::delegate (new
0e218820
                         file_reader_impl ));
bef74c28
 }
 
 user_token_supplier user_token_supplier::create (file_reader &file_reader)
 {
34b9d4f7
     return user_token_supplier (user_token_supplier::delegate
                                 (new user_token_supplier_impl (file_reader)));
bef74c28
 }