git.fiddlerwoaroof.com
validator.cc
7684972a
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
  * terms of the  GNU General Public License  version 2  or
  * 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.
  */
 
f757e35b
 #include <string>
28855478
 #include <vector>
f757e35b
 
 #include "validator.h"
21e4960f
 #include "become.h"
 #include "sys_unistd.h"
f757e35b
 
ee12703f
 namespace
 {
28855478
 class impl : public validator_ifc
 {
 private:
     directory directory_;
e064ffa9
     tokens tokens_;
21e4960f
     unistd unistd_;
28855478
 public:
     impl (const directory &directory,
21e4960f
           const tokens tokens,
           const unistd unistd) :
28855478
         directory_ (directory),
21e4960f
         tokens_ (tokens),
         unistd_ (unistd)
     {}
6ff7d0a7
     bool validate (const std::string &requester_user_name,
                    const std::string &authorizer_user_name,
21e4960f
                    const std::string &token,
                    const std::string &reason) override
28855478
     {
6ff7d0a7
         std::vector<user> found_user = directory_.find_user (authorizer_user_name);
 
044b2625
         if (reason.empty()) {
             return false;
         }
 
39b6d4a1
         if (requester_user_name.empty()) {
             return false;
         }
 
6ff7d0a7
         if (requester_user_name == authorizer_user_name) {
             return false;
         }
28855478
 
         if (found_user.empty()) {
             return false;
ee12703f
         }
 
21e4960f
         auto user_ = found_user[0];
0d8b9a17
         become become_ (user_, unistd_);
21e4960f
 
         std::string user_token = tokens_.token (user_);
28855478
         return user_token == token;
     }
 };
ee12703f
 }
 
da1b9b25
 validator validator::create (const directory &directory,
21e4960f
                              const tokens &tokens,
                              const unistd &unistd)
cdf7fd74
 {
0d8b9a17
     std::shared_ptr<validator_ifc> delegate (new impl (directory, tokens,
             unistd));
28855478
     return validator (delegate);
f757e35b
 }
0d8b9a17