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>
af3a4cd4
 #include <iostream>
 #include <functional>
1c7f8bf0
 
bef74c28
 #include "token.h"
 #include "user.h"
215ea751
 #include "sys_fstream.h"
bef74c28
 
 namespace
 {
e064ffa9
 class tokens_impl : public tokens_ifc
0e218820
 {
 private:
215ea751
     fstreams fstreams_;
0e218820
 public:
6603197e
     tokens_impl (const fstreams &fstreams) :
bc6c3d35
         fstreams_ (fstreams) {}
af3a4cd4
     std::string token (const user &user) const override
0e218820
     {
4bf199d2
         const std::string file_path (user.home_directory() + "/.dual_control");
bc6c3d35
         std::vector<char> line (7);
4bf199d2
 
bc6c3d35
         fstreams::pstream stream (fstreams_.open_fstream (file_path));
4bf199d2
 
215ea751
         if (!stream->good()) {
4bf199d2
             return "";
         }
 
bc6c3d35
         stream->getline (line.data(), line.size());
         return std::string (line.data());
0e218820
     }
decf9568
     void save (const user &user, const std::string &token) const override
d10906ee
     {
         std::string file_path (user.home_directory() + "/.dual_control");
         fstreams::postream stream (fstreams_.open_ofstream (file_path,
                                    std::ios_base::trunc));
53f9fba3
         *stream << token << std::endl;
af3a4cd4
     }
0e218820
 };
bef74c28
 }
6603197e
 tokens tokens::create (const fstreams &fstreams)
bef74c28
 {
e064ffa9
     return tokens (tokens::delegate
6603197e
                    (new tokens_impl (fstreams)));
bef74c28
 }