Advertisement
cgchamila

Untitled

Oct 3rd, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.74 KB | None | 0 0
  1. <?php
  2. if ($this->__pmg_request->o == 'createconsumer') {
  3.             $provider = new Oauth_Provider();
  4.             Oauth_Provider::createConsumer('abc','http://www.abc.loc');
  5.         }
  6.         else if ($this->__pmg_request->o == 'requesttoken') {
  7.             $provider = new Oauth_Provider($this->__pmg_request->d->oauth_consumer_key);
  8.             $provider->setRequestTokenQuery();
  9.             $provider->checkRequest();
  10.             $provider->generateRequestToken();
  11.         }
  12. ?>
  13.  
  14. <?php
  15. class Oauth_Provider {
  16.     private $oauth = null;
  17.     private $consumer = null;
  18.     private $oauth_error = null;
  19.     private $user = null;
  20.     private $consumer_key = null;
  21.     private $consumer_secret = null;
  22.     private $authentification_url = "http://www.abc.loc/signin";
  23.    
  24.     public function __construct($consumer_key=null, $consumer_secret=null)
  25.     {
  26.         $this->oauth = new OAuthProvider();
  27.         $this->oauth->consumerHandler(array($this,'checkConsumer'));
  28.         $this->oauth->timestampNonceHandler(array($this,'checkNonce'));
  29.         $this->oauth->tokenHandler(array($this,'checkToken'));
  30.     }
  31.  
  32.     public static function createConsumer($app_name,$callback) {
  33.         $fp = fopen('/dev/urandom','rb');
  34.         $entropy = fread($fp, 32);
  35.         fclose($fp);
  36.         $entropy .= uniqid(mt_rand(), true);
  37.         $hash = sha1($entropy);
  38.         return Oauth_Consumer::create(substr($hash,0,30), substr($hash,30,10),$app_name,$callback);
  39.     }
  40.    
  41.     public function checkConsumer($provider){
  42.         $return = OAUTH_CONSUMER_KEY_UNKNOWN;
  43.        
  44.         $consumer = Oauth_Consumer::findByKey($provider->consumer_key);
  45.        
  46.         if(is_object($consumer)){
  47.             if(!$consumer->getActive()){
  48.                 $return = OAUTH_CONSUMER_KEY_REFUSED;
  49.             } else {
  50.                 $this->consumer = $consumer;
  51.                 $provider->consumer_secret = $this->consumer->getSecret();
  52.                 $return = OAUTH_OK;
  53.             }
  54.         }
  55.         return $return;
  56.     }
  57.    
  58.     public function checkNonce($provider){
  59.         if($this->oauth->timestamp < time() - 5*60){
  60.             return OAUTH_BAD_TIMESTAMP;
  61.         } elseif($this->consumer->hasNonce($provider->nonce,$this->oauth->timestamp)) {
  62.             return OAUTH_BAD_NONCE;
  63.         } else {
  64.             $this->consumer->addNonce($this->oauth->nonce,$this->oauth->timestamp);
  65.             return OAUTH_OK;
  66.         }
  67.     }
  68.    
  69.     public function checkToken($provider){
  70.         $token = Oauth_Token::findByToken($provider->token);
  71.        
  72.         if(is_null($token)){
  73.             return OAUTH_TOKEN_REJECTED;
  74.         } elseif($token->getType() == 1 && $token->getVerifier() != $provider->verifier){
  75.             return OAUTH_VERIFIER_INVALID;
  76.         } else {
  77.             if($token->getType() == 2){
  78.                 $this->user = $token->getUser();
  79.             }
  80.             $provider->token_secret = $token->getSecret();
  81.             return OAUTH_OK;
  82.         }
  83.     }
  84.    
  85.     public function setRequestTokenQuery(){
  86.         $this->oauth->isRequestTokenEndpoint(true);
  87.         //$this->oauth->addRequiredParameter("oauth_callback");
  88.     }
  89.    
  90.     public function checkRequest(){
  91.         try{
  92.             $this->oauth->checkOAuthRequest();
  93.         } catch(OAuthException $E){
  94.             echo OAuthProvider::reportProblem($E);
  95.             $this->oauth_error = true;
  96.         }
  97.     }
  98.    
  99.     public function generateRequestToken(){
  100.        
  101.         if($this->oauth_error){
  102.             return false;
  103.         }
  104.        
  105.         $token = sha1(OAuthProvider::generateToken(20,true));
  106.         $token_secret = sha1(OAuthProvider::generateToken(20,true));
  107.        
  108.         $callback = 'http://www.abc.loc';
  109.        
  110.         //Token::createRequestToken($this->consumer, $token, $token_secret, $callback);
  111.         return "authentification_url=".$this->authentification_url."&oauth_token=".$token."&oauth_token_secret=".$token_secret."&oauth_callback_confirmed=true";
  112.     }
  113. }
  114. ?>
  115.  
  116.  
  117. <?php
  118. class Oauth_Consumer {
  119.     private $id = null;
  120.     private $key = null;
  121.     private $secret = null;
  122.     private $appname = null;
  123.     private $callbackUrl = null;
  124.     private $active = true;
  125.    
  126.     public function __construct($id = 0)
  127.     {
  128.         if($id != 0){
  129.             $this->id = $id;
  130.             $this->load();
  131.         }
  132.     }
  133.    
  134.     public function getAppname()
  135.     {
  136.         return $this->appname;
  137.     }
  138.  
  139.     public function getCallbackUrl()
  140.     {
  141.         return $this->callbackUrl;
  142.     }
  143.  
  144.     public function setAppname($appname)
  145.     {
  146.         $this->appname = $appname;
  147.     }
  148.  
  149.     public function setCallbackUrl($callbackUrl)
  150.     {
  151.         $this->callbackUrl = $callbackUrl;
  152.     }
  153.    
  154.     public function getId()
  155.     {
  156.         return $this->id;
  157.     }
  158.  
  159.     public function getKey()
  160.     {
  161.         return $this->key;
  162.     }
  163.  
  164.     public function getSecret()
  165.     {
  166.         return $this->secret;
  167.     }
  168.  
  169.     public function getActive()
  170.     {
  171.         return $this->active;
  172.     }
  173.  
  174.     public function setId($id)
  175.     {
  176.         $this->id = $id;
  177.     }
  178.  
  179.     public function setKey($key)
  180.     {
  181.         $this->key = $key;
  182.     }
  183.  
  184.     public function setSecret($secret)
  185.     {
  186.         $this->secret = $secret;
  187.     }
  188.  
  189.     public function setActive($active)
  190.     {
  191.         $this->active = $active;
  192.     }
  193.    
  194.     public static function findByKey($key)
  195.     {
  196.         //"select id from consumers where consumer_key = @key"
  197.         $consumer = null;
  198.        
  199.         $consumer = new Oauth_Consumer(1/*$row->id*/);
  200.         return $consumer;
  201.     }
  202.    
  203.     private function load()
  204.     {
  205.     //SELECT consumer_key,consumer_secret,app_name,callback_url FROM consumers WHERE id = $this->id
  206.        
  207.        
  208.        
  209.             $this->key = $row['consumer_key'];
  210.             $this->secret = $row['consumer_secret'];
  211.             $this->appname = $row['app_name'];
  212.             $this->callbackUrl = $row['callback_url'];  
  213.        
  214.     }
  215.    
  216.     public static function create($key,$secret,$appname,$callback){
  217. //INSERT INTO consumers (consumer_ke}y,consumer_secret,app_name,callback_url,active) VALUES ($key,$secret,$appname,$callback,1)
  218.        
  219.         $consumer = new Oauth_Consumer(/*mysql_insert_id()*/'1');
  220.         return $consumer;
  221.     }
  222.    
  223.     public function hasNonce($nonce,$timestamp)
  224.     {
  225. //select count(*) as count from consumer_nonce where timestamp = $timestamp and nonce = $nonce and consumer_id = $this->id
  226.        
  227.        
  228.             if($row['count'] > 0){
  229.                 return true;
  230.             } else {
  231.                 return false;
  232.             }  
  233.        
  234.     }
  235.    
  236.     public function addNonce($nonce,$timestamp)
  237.     {
  238.     //insert into sb_consumer_nonce (consumer_id,timestamp,nonce) values ($id,$time,$nonce)
  239.        
  240.     }
  241. }
  242. ?>
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement