consumer = new Zeed_OAuth_Consumer("key", "secret"); $this->requestToken = new Zeed_OAuth_Token("requestkey", "requestsecret"); $this->accessToken = new Zeed_OAuth_Token("accesskey", "accesssecret"); $this->nonce = "nonce"; } /** * 获取应用信息及密钥 * * @param string $consumer_key * @return OAuthConsumer */ public function lookupConsumer($consumer_key) { $app = Com_Model_App::instance()->getAppByApikey($consumer_key); if ($app) return new Zeed_OAuth_Consumer($app['apikey'],$app['apisecret']); return NULL; } /** * * @param OAuthConsumer $consumer * @param string $token_type * @param string $token * @return Zeed_OAuth_Token|NULL */ public function lookupToken($consumer, $token_type, $token_key) { if ($token_type == 'request') { $tokenInDb = Com_Model_Token::instance()->getTokenByRequestToken($token_key); if ($tokenInDb && $tokenInDb['apikey'] == $consumer->key) { return new Zeed_OAuth_Token($tokenInDb['request_token'],$tokenInDb['request_secret']); } } else if ($token_type == 'access') { $tokenInDb = Com_Model_Token::instance()->getTokenByAccessToken($token_key); if ($tokenInDb && $tokenInDb['apikey'] == $consumer->key && $tokenInDb['status'] == self::TOKEN_STATUS_GRANTED) { return new Zeed_OAuth_Token($tokenInDb['access_token'],$tokenInDb['access_secret']); } else { throw new Zeed_OAuth_Exception("User Denied Access"); } } return NULL; } public function lookupNonce($consumer, $token, $nonce, $timestamp) { if ($consumer->key == $this->consumer->key && (($token && $token->key == $this->request_token->key) || ($token && $token->key == $this->access_token->key)) && $nonce == $this->nonce) { return $this->nonce; } return NULL; } /** * 产生新的Request Token * @param OAuthConsumer $consumer */ public function newRequestToken($consumer, $callback_url = NULL) { $app = Com_Model_App::instance()->getAppByApikey($consumer->key); if ($app) { $token = $this->getRandomString(); $secret = $this->getRandomString(); $accessToken = $this->getRandomString(); $accessSecret = $this->getRandomString(); $tokenVerifier = $this->getRandomString(); $tokenSet = array('apikey'=>$consumer->key,'request_token'=>$token,'request_secret'=>$secret,'callback_url'=>$callback_url, 'access_token'=>$accessToken,'access_secret'=>$accessSecret,'verifier'=>$tokenVerifier); Com_Model_Token::instance()->addToken($tokenSet); return new Zeed_OAuth_Token($token, $secret); } return NULL; } /** * 产生新的ACCESS TOKNE * @param Zeed_OAuth_Token $request_token * @param OAuthConsumer $consumer * @param string $verifier */ public function newAccessToken($request_token, $consumer, $verifier = null) { $tokenInDb = Com_Model_Token::instance()->getTokenByRequestToken($request_token->key); if ($tokenInDb && $tokenInDb['apikey'] == $consumer->key && $tokenInDb['status'] == self::TOKEN_STATUS_GRANTED && $tokenInDb['verifier'] == $verifier) { return new Zeed_OAuth_Token($tokenInDb['access_token'],$tokenInDb['access_secret']); } else if ($tokenInDb['verifier'] != $verifier) { throw new Zeed_OAuth_Exception("Invalid Verifier"); } else if ($tokenInDb['status'] != self::TOKEN_STATUS_GRANTED) { throw new Zeed_OAuth_Exception("User Denied Access"); } return NULL; } /** * 生成随机不重复字串 * * @return string */ public static function getRandomString() { $computer = $_SERVER["SERVER_NAME"].'/'.$_SERVER["SERVER_ADDR"]; $long = (rand(0,1)?'-':'').rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999); $microtime = microtime(true); return rand(0,1) ? 'm-'.sha1($computer.$long.$microtime) : 's-'.md5($computer.$long.$microtime); } } // End ^ Native EOL ^ encoding