Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if ($this->__pmg_request->o == 'createconsumer') {
- $provider = new Oauth_Provider();
- Oauth_Provider::createConsumer('abc','http://www.abc.loc');
- }
- else if ($this->__pmg_request->o == 'requesttoken') {
- $provider = new Oauth_Provider($this->__pmg_request->d->oauth_consumer_key);
- $provider->setRequestTokenQuery();
- $provider->checkRequest();
- $provider->generateRequestToken();
- }
- ?>
- <?php
- class Oauth_Provider {
- private $oauth = null;
- private $consumer = null;
- private $oauth_error = null;
- private $user = null;
- private $consumer_key = null;
- private $consumer_secret = null;
- private $authentification_url = "http://www.abc.loc/signin";
- public function __construct($consumer_key=null, $consumer_secret=null)
- {
- $this->oauth = new OAuthProvider();
- $this->oauth->consumerHandler(array($this,'checkConsumer'));
- $this->oauth->timestampNonceHandler(array($this,'checkNonce'));
- $this->oauth->tokenHandler(array($this,'checkToken'));
- }
- public static function createConsumer($app_name,$callback) {
- $fp = fopen('/dev/urandom','rb');
- $entropy = fread($fp, 32);
- fclose($fp);
- $entropy .= uniqid(mt_rand(), true);
- $hash = sha1($entropy);
- return Oauth_Consumer::create(substr($hash,0,30), substr($hash,30,10),$app_name,$callback);
- }
- public function checkConsumer($provider){
- $return = OAUTH_CONSUMER_KEY_UNKNOWN;
- $consumer = Oauth_Consumer::findByKey($provider->consumer_key);
- if(is_object($consumer)){
- if(!$consumer->getActive()){
- $return = OAUTH_CONSUMER_KEY_REFUSED;
- } else {
- $this->consumer = $consumer;
- $provider->consumer_secret = $this->consumer->getSecret();
- $return = OAUTH_OK;
- }
- }
- return $return;
- }
- public function checkNonce($provider){
- if($this->oauth->timestamp < time() - 5*60){
- return OAUTH_BAD_TIMESTAMP;
- } elseif($this->consumer->hasNonce($provider->nonce,$this->oauth->timestamp)) {
- return OAUTH_BAD_NONCE;
- } else {
- $this->consumer->addNonce($this->oauth->nonce,$this->oauth->timestamp);
- return OAUTH_OK;
- }
- }
- public function checkToken($provider){
- $token = Oauth_Token::findByToken($provider->token);
- if(is_null($token)){
- return OAUTH_TOKEN_REJECTED;
- } elseif($token->getType() == 1 && $token->getVerifier() != $provider->verifier){
- return OAUTH_VERIFIER_INVALID;
- } else {
- if($token->getType() == 2){
- $this->user = $token->getUser();
- }
- $provider->token_secret = $token->getSecret();
- return OAUTH_OK;
- }
- }
- public function setRequestTokenQuery(){
- $this->oauth->isRequestTokenEndpoint(true);
- //$this->oauth->addRequiredParameter("oauth_callback");
- }
- public function checkRequest(){
- try{
- $this->oauth->checkOAuthRequest();
- } catch(OAuthException $E){
- echo OAuthProvider::reportProblem($E);
- $this->oauth_error = true;
- }
- }
- public function generateRequestToken(){
- if($this->oauth_error){
- return false;
- }
- $token = sha1(OAuthProvider::generateToken(20,true));
- $token_secret = sha1(OAuthProvider::generateToken(20,true));
- $callback = 'http://www.abc.loc';
- //Token::createRequestToken($this->consumer, $token, $token_secret, $callback);
- return "authentification_url=".$this->authentification_url."&oauth_token=".$token."&oauth_token_secret=".$token_secret."&oauth_callback_confirmed=true";
- }
- }
- ?>
- <?php
- class Oauth_Consumer {
- private $id = null;
- private $key = null;
- private $secret = null;
- private $appname = null;
- private $callbackUrl = null;
- private $active = true;
- public function __construct($id = 0)
- {
- if($id != 0){
- $this->id = $id;
- $this->load();
- }
- }
- public function getAppname()
- {
- return $this->appname;
- }
- public function getCallbackUrl()
- {
- return $this->callbackUrl;
- }
- public function setAppname($appname)
- {
- $this->appname = $appname;
- }
- public function setCallbackUrl($callbackUrl)
- {
- $this->callbackUrl = $callbackUrl;
- }
- public function getId()
- {
- return $this->id;
- }
- public function getKey()
- {
- return $this->key;
- }
- public function getSecret()
- {
- return $this->secret;
- }
- public function getActive()
- {
- return $this->active;
- }
- public function setId($id)
- {
- $this->id = $id;
- }
- public function setKey($key)
- {
- $this->key = $key;
- }
- public function setSecret($secret)
- {
- $this->secret = $secret;
- }
- public function setActive($active)
- {
- $this->active = $active;
- }
- public static function findByKey($key)
- {
- //"select id from consumers where consumer_key = @key"
- $consumer = null;
- $consumer = new Oauth_Consumer(1/*$row->id*/);
- return $consumer;
- }
- private function load()
- {
- //SELECT consumer_key,consumer_secret,app_name,callback_url FROM consumers WHERE id = $this->id
- $this->key = $row['consumer_key'];
- $this->secret = $row['consumer_secret'];
- $this->appname = $row['app_name'];
- $this->callbackUrl = $row['callback_url'];
- }
- public static function create($key,$secret,$appname,$callback){
- //INSERT INTO consumers (consumer_ke}y,consumer_secret,app_name,callback_url,active) VALUES ($key,$secret,$appname,$callback,1)
- $consumer = new Oauth_Consumer(/*mysql_insert_id()*/'1');
- return $consumer;
- }
- public function hasNonce($nonce,$timestamp)
- {
- //select count(*) as count from consumer_nonce where timestamp = $timestamp and nonce = $nonce and consumer_id = $this->id
- if($row['count'] > 0){
- return true;
- } else {
- return false;
- }
- }
- public function addNonce($nonce,$timestamp)
- {
- //insert into sb_consumer_nonce (consumer_id,timestamp,nonce) values ($id,$time,$nonce)
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement