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>
1c7f8bf0
 
bef74c28
 #include "token.h"
 #include "user.h"
215ea751
 #include "sys_fstream.h"
bef74c28
 
 namespace
 {
34b9d4f7
 class user_token_supplier_impl : public user_token_supplier_ifc
0e218820
 {
 private:
215ea751
     fstreams fstreams_;
0e218820
 public:
215ea751
     user_token_supplier_impl (fstreams &fstreams) :
         fstreams_(fstreams) {}
0e218820
     std::string token (user &user)
     {
4bf199d2
         const std::string file_path (user.home_directory() + "/.dual_control");
215ea751
         std::vector<char> line(7);
4bf199d2
 
215ea751
         fstreams::pstream stream(fstreams_.open_fstream(file_path));
4bf199d2
 
215ea751
         if (!stream->good()) {
4bf199d2
             return "";
         }
 
215ea751
         stream->getline(line.data(), line.size());
         return std::string(line.data());
0e218820
     }
 };
bef74c28
 }
215ea751
 user_token_supplier user_token_supplier::create (fstreams &fstreams)
bef74c28
 {
34b9d4f7
     return user_token_supplier (user_token_supplier::delegate
215ea751
                                 (new user_token_supplier_impl (fstreams)));
bef74c28
 }