Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2.  
  3. $auth = new Authenticator();
  4. $user = $auth->process();
  5. if ($user->username == "animuson") $final = "animuson@boogiewoogie";
  6. else $final = "{$user->username}@tronner";
  7. $auth->conclude("PASSWORD_OK {$final}\n", 200);
  8.  
  9. class Authenticator {
  10.     private $authority = "tronner.com"; // The authority domain that we should be using
  11.     private $statusOk = true; // Should it always force HTTP status code 200 when sending the message?
  12.     private $username; // The username being checked
  13.     private $prefix = "arma"; // The prefix that is attached to the front of passwords
  14.     private $suffix = "md5"; // The suffix that is attached to the end of passwords
  15.     private $methods = array("md5" => array("prefix" => "arma", "suffix" => "md5")); // The encryption methods you support
  16.    
  17.     function __construct() {
  18.         global $ani;
  19.         if ($ani->i->server['http_host'] != $this->authority) $this->conclude("WRONG_HOST", 404);
  20.     }
  21.    
  22.     public function process($query = NULL) {
  23.         global $ani;
  24.         if (is_null($query)) $query = strtolower($ani->i->get['query']);
  25.         switch ($query):
  26.             case "methods":
  27.                 $this->conclude("METHODS {$this->getMethods()}");
  28.                 break;
  29.             case "params":
  30.                 $this->conclude($this->getParameters(strtolower($ani->i->get['method'])));
  31.                 break;
  32.             case "check":
  33.                 return $this->checkPassword();
  34.                 break;
  35.             default:
  36.                 $this->conclude("UNKNOWN_QUERY", 404);
  37.         endswitch;
  38.     }
  39.    
  40.     private function conclude($message, $status = 200) {
  41.         if ($this->statusOk === true) $status = 200;
  42.         header("Status: {$status}", true, $status);
  43.         header("Content-Type: text/plain");
  44.         die("{$message}\n");
  45.     }
  46.    
  47.     private function substituteUser($string) { return str_replace("%u", $this->username, $string); }
  48.    
  49.     private function getMethods() { return implode(",", array_keys($this->methods)); }
  50.    
  51.     private function getParameters($method) {
  52.         if (!array_key_exists($method, $this->methods)) $this->conclude("UNKNOWN_METHOD", 404);
  53.         $params = array();
  54.         if ($this->methods[$method]['prefix'] != "") $params[] = "PREFIX {$this->methods[$method]['prefix']}";
  55.         if ($this->methods[$method]['suffix'] != "") $params[] = "SUFFIX {$this->methods[$method]['suffix']}";
  56.         return implode("\n", $params);
  57.     }
  58.    
  59.     private function getPassword($username, $method) {
  60.         global $ani;
  61.         $this->username = $username;
  62.         // Let's not bother if it's an invalid username...
  63.         if (preg_match("/[^a-zA-Z0-9_]/", $username) || strlen($username) < 4 || strlen($username) > 30) $this->conclude("UNKNOWN_USER", 404);
  64.         $user = $ani->e->db->fetch($ani->e->db->query("SELECT * FROM `global_users` WHERE `username` = '{$username}'"));
  65.         if ($user['uid'] == "") $this->conclude("UNKNOWN_USER", 404);
  66.         $cache = new Cache("users", array($user['uid'], $username));
  67.         $user = $cache->decode();
  68.         switch ($method):
  69.             case "bmd5":
  70.                 return array($user, $user->password);
  71.                 break;
  72.             case "md5":
  73.                 return array($user, $user->tronner->password);
  74.                 break;
  75.             default:
  76.                 $this->conclude("METHOD_NOT_IMPLEMNTED", 501);
  77.         endswitch;
  78.     }
  79.    
  80.     private function checkPassword() {
  81.         global $ani;
  82.         $info = $this->getPassword(strtolower($ani->i->get['user']), strtolower($ani->i->get['method']));
  83.         $salt = $ani->i->get['salt'];
  84.         $hash = $ani->i->get['hash'];
  85.         $packedSalt = pack("H*", $salt);
  86.         $correctPassword = pack("H*", $info[1]);
  87.         $correctHash = md5("{$packedSalt}{$correctPassword}");
  88.         if (strcasecmp($hash, $correctHash) === 0) return $info[0];
  89.         else $this->conclude("PASSWORD_FAIL {$hash} / {$correctHash}", 401);
  90.     }
  91. }
  92.  
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement