本文整理汇总了PHP中OAuthServer类的典型用法代码示例。如果您正苦于以下问题:PHP OAuthServer类的具体用法?PHP OAuthServer怎么用?PHP OAuthServer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OAuthServer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handle
/**
* Handle a request for temporary OAuth credentials
*
* Make sure the request is kosher, then emit a set of temporary
* credentials -- AKA an unauthorized request token.
*
* @param array $args array of arguments
*
* @return void
*/
function handle($args)
{
parent::handle($args);
$datastore = new ApiStatusNetOAuthDataStore();
$server = new OAuthServer($datastore);
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($hmac_method);
try {
$req = OAuthRequest::from_request();
// verify callback
if (!$this->verifyCallback($req->get_parameter('oauth_callback'))) {
throw new OAuthException("You must provide a valid URL or 'oob' in oauth_callback.", 400);
}
// check signature and issue a new request token
$token = $server->fetch_request_token($req);
common_log(LOG_INFO, sprintf("API OAuth - Issued request token %s for consumer %s with oauth_callback %s", $token->key, $req->get_parameter('oauth_consumer_key'), "'" . $req->get_parameter('oauth_callback') . "'"));
// return token to the client
$this->showRequestToken($token);
} catch (OAuthException $e) {
common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
// Return 401 for for bad credentials or signature problems,
// and 400 for missing or unsupported parameters
$code = $e->getCode();
$this->clientError($e->getMessage(), empty($code) ? 401 : $code, 'text');
}
}
示例2: handle
/**
* Class handler.
*
* @param array $args array of arguments
*
* @return void
*/
function handle($args)
{
parent::handle($args);
$datastore = new ApiStatusNetOAuthDataStore();
$server = new OAuthServer($datastore);
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($hmac_method);
$atok = $app = null;
// XXX: Insist that oauth_token and oauth_verifier be populated?
// Spec doesn't say they MUST be.
try {
$req = OAuthRequest::from_request();
$this->reqToken = $req->get_parameter('oauth_token');
$this->verifier = $req->get_parameter('oauth_verifier');
$app = $datastore->getAppByRequestToken($this->reqToken);
$atok = $server->fetch_access_token($req);
} catch (Exception $e) {
common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
common_debug(var_export($req, true));
$code = $e->getCode();
$this->clientError($e->getMessage(), empty($code) ? 401 : $code, 'text');
return;
}
if (empty($atok)) {
// Token exchange failed -- log it
$msg = sprintf('API OAuth - Failure exchanging OAuth request token for access token, ' . 'request token = %s, verifier = %s', $this->reqToken, $this->verifier);
common_log(LOG_WARNING, $msg);
// TRANS: Client error given from the OAuth API when the request token or verifier is invalid.
$this->clientError(_('Invalid request token or verifier.'), 400, 'text');
} else {
common_log(LOG_INFO, sprintf("Issued access token '%s' for application %d (%s).", $atok->key, $app->id, $app->name));
$this->showAccessToken($atok);
}
}
示例3: omb_oauth_server
function omb_oauth_server()
{
static $server = null;
if (is_null($server)) {
$server = new OAuthServer(omb_oauth_datastore());
$server->add_signature_method(omb_hmac_sha1());
}
return $server;
}
示例4: handleOAuthBodyPOST
function handleOAuthBodyPOST($oauth_consumer_key, $oauth_consumer_secret)
{
$request_headers = OAuthUtil::get_headers();
// print_r($request_headers);
// Must reject application/x-www-form-urlencoded
if ($request_headers['Content-type'] == 'application/x-www-form-urlencoded' ) {
throw new Exception("OAuth request body signing must not use application/x-www-form-urlencoded");
}
if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
$header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
// echo("HEADER PARMS=\n");
// print_r($header_parameters);
$oauth_body_hash = $header_parameters['oauth_body_hash'];
// echo("OBH=".$oauth_body_hash."\n");
}
if ( ! isset($oauth_body_hash) ) {
throw new Exception("OAuth request body signing requires oauth_body_hash body");
}
// Verify the message signature
$store = new TrivialOAuthDataStore();
$store->add_consumer($oauth_consumer_key, $oauth_consumer_secret);
$server = new OAuthServer($store);
$method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($method);
$request = OAuthRequest::from_request();
global $LastOAuthBodyBaseString;
$LastOAuthBodyBaseString = $request->get_signature_base_string();
// echo($LastOAuthBodyBaseString."\n");
try {
$server->verify_request($request);
} catch (Exception $e) {
$message = $e->getMessage();
throw new Exception("OAuth signature failed: " . $message);
}
$postdata = file_get_contents('php://input');
// echo($postdata);
$hash = base64_encode(sha1($postdata, TRUE));
if ( $hash != $oauth_body_hash ) {
throw new Exception("OAuth oauth_body_hash mismatch");
}
return $postdata;
}
示例5: genSign
public function genSign($key, $secret, $token, $tokenSecret, $httpMethod, $endpoint)
{
$authServer = new OAuthServer(new MockOAuthDataStore());
$hmac_method = new OAuthSignatureMethodHmacSha1();
$authServer->add_signature_method($hmac_method);
$sig_method = $hmac_method;
$authConsumer = new OAuthConsumer($key, $secret, NULL);
$authToken = NULL;
$authToken = new OAuthToken($token, $tokenSecret);
//$params is the query param array which is required only in the httpMethod is "GET"
$params = array();
//TODO: set the Query parameters to $params if httpMethod is "GET"
$acc_req = OAuthRequest::from_consumer_and_token($authConsumer, $authToken, $httpMethod, $endpoint, $params);
$acc_req->sign_request($sig_method, $authConsumer, $authToken);
return OAuthutil::parseQueryString($acc_req);
}
示例6: access_token
public function access_token($params)
{
try {
$server = new OAuthServer($this->oauthDataStore);
$server->add_signature_method(new OAuthSignatureMethod_HMAC_SHA1());
$server->add_signature_method(new OAuthSignatureMethod_PLAINTEXT());
$request = OAuthRequest::from_request();
$token = $server->fetch_access_token($request);
if ($token) {
echo $token->to_string();
}
} catch (OAuthException $e) {
$this->sendServerError(401, $e->getMessage());
} catch (Exception $e) {
$this->sendServerError(400, $e->getMessage());
}
}
示例7: handle
/**
* Class handler.
*
* @param array $args array of arguments
*
* @return void
*/
function handle($args)
{
parent::handle($args);
$datastore = new ApiStatusNetOAuthDataStore();
$server = new OAuthServer($datastore);
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($hmac_method);
try {
$req = OAuthRequest::from_request();
$token = $server->fetch_request_token($req);
print $token;
} catch (OAuthException $e) {
common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
header('HTTP/1.1 401 Unauthorized');
header('Content-Type: text/html; charset=utf-8');
print $e->getMessage() . "\n";
}
}
示例8: __construct
/**
* Create new Basic LTI access object
*
* @param string $key
* @param string $secret
*
* @throws \Exception
*/
public function __construct($key, $secret)
{
$request = \OAuthRequest::from_request();
$oauth_consumer_key = $request->get_parameter("oauth_consumer_key");
// ensure the key in the request matches the locally supplied one
if ($oauth_consumer_key == null) {
throw new \Exception("Missing oauth_consumer_key in request");
}
if ($oauth_consumer_key != $key) {
throw new \Exception("oauth_consumer_key doesn't match supplied key");
}
// verify the message signature
$store = new TrivialOAuthDataStore($oauth_consumer_key, $secret);
$server = new \OAuthServer($store);
$method = new \OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($method);
$server->verify_request($request);
$this->request = $request;
}
示例9: handle_oauth_body_post
function handle_oauth_body_post($oauthconsumerkey, $oauthconsumersecret, $body, $requestheaders = null)
{
if ($requestheaders == null) {
$requestheaders = OAuthUtil::get_headers();
}
// Must reject application/x-www-form-urlencoded.
if (isset($requestheaders['Content-type'])) {
if ($requestheaders['Content-type'] == 'application/x-www-form-urlencoded') {
throw new OAuthException("OAuth request body signing must not use application/x-www-form-urlencoded");
}
}
if (@substr($requestheaders['Authorization'], 0, 6) == "OAuth ") {
$headerparameters = OAuthUtil::split_header($requestheaders['Authorization']);
$oauthbodyhash = $headerparameters['oauth_body_hash'];
}
if (!isset($oauthbodyhash)) {
throw new OAuthException("OAuth request body signing requires oauth_body_hash body");
}
// Verify the message signature.
$store = new TrivialOAuthDataStore();
$store->add_consumer($oauthconsumerkey, $oauthconsumersecret);
$server = new OAuthServer($store);
$method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($method);
$request = OAuthRequest::from_request();
try {
$server->verify_request($request);
} catch (\Exception $e) {
$message = $e->getMessage();
throw new OAuthException("OAuth signature failed: " . $message);
}
$postdata = $body;
$hash = base64_encode(sha1($postdata, true));
if ($hash != $oauthbodyhash) {
throw new OAuthException("OAuth oauth_body_hash mismatch");
}
return $postdata;
}
示例10: authorizeAction
public function authorizeAction()
{
$auth = Zend_Auth::getInstance();
$store = OAuthStore::instance();
$registry = Zend_Registry::getInstance();
$router = Zend_Controller_Front::getInstance()->getRouter();
$request = $this->getRequest();
if (!$auth->hasIdentity()) {
Zend_Controller_Front::getInstance()->registerPlugin(new Ml_Plugins_LoginRedirect());
}
$this->_helper->loadOauthstore->preloadServer();
$server = new OAuthServer();
$form = Ml_Model_Api::authorizeForm();
// Check if there is a valid request token in the current request
// Returns an array with the
//consumer key, consumer secret, token, token secret and token type.
$rs = $server->authorizeVerify();
$consumer = $store->getConsumer($rs['consumer_key'], $auth->getIdentity());
$this->view->consumerInfo = $consumer;
if ($request->isPost() && $form->isValid($request->getPost())) {
$values = $form->getValues();
if (isset($values['allow'])) {
$authorized = true;
} else {
if (isset($values['deny'])) {
$authorized = false;
}
}
if (isset($authorized)) {
$server->authorizeFinish($authorized, $auth->getIdentity());
//If no oauth_callback, the user is redirected to
$this->_redirect($router->assemble(array(), "accountapps") . "?new_addition", array("exit"));
}
}
$this->view->authorizeForm = $form;
}
示例11: handle
/**
* Class handler.
*
* @param array $args array of arguments
*
* @return void
*/
function handle($args)
{
parent::handle($args);
$datastore = new ApiStatusNetOAuthDataStore();
$server = new OAuthServer($datastore);
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($hmac_method);
$atok = null;
try {
$req = OAuthRequest::from_request();
$atok = $server->fetch_access_token($req);
} catch (OAuthException $e) {
common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
common_debug(var_export($req, true));
$this->outputError($e->getMessage());
return;
}
if (empty($atok)) {
common_debug('couldn\'t get access token.');
print "Token exchange failed. Has the request token been authorized?\n";
} else {
print $atok;
}
}
示例12: actionAuthorize
public function actionAuthorize()
{
//登陆用户
$user_id = Yii::app()->user->id;
$model = new LoginForm();
$errmsg = '';
// 取得 oauth store 和 oauth server 对象
$server = new OAuthServer();
try {
// 检查当前请求中是否包含一个合法的请求token
// 返回一个数组, 包含consumer key, consumer secret, token, token secret 和 token type.
$rs = $server->authorizeVerify($user_id);
// 没有登录时不允许跳转
if (!empty($user_id)) {
//当application_type 为 system 时,可以不须经过用户授权
if ($rs['application_type'] == 'system') {
$authorized = True;
$server->authorizeFinish($authorized, $user_id);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// 判断用户是否点击了 "allow" 按钮(或者你可以自定义为其他标识)
$authorized = True;
// 设置token的认证状态(已经被认证或者尚未认证)
// 如果存在 oauth_callback 参数, 重定向到客户(消费方)地址
$verifier = $server->authorizeFinish($authorized, $user_id);
// 如果没有 oauth_callback 参数, 显示认证结果
// ** 你的代码 **
echo $verifier;
die;
} else {
#echo 'Error';
}
} else {
// if it is ajax validation request
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
echo EActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if (isset($_POST['LoginForm'])) {
$model->attributes = $_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if ($model->validate() && $model->login()) {
$this->refresh();
}
}
}
} catch (OAuthException $e) {
$errmsg = $e->getMessage();
throw new CHttpException(401, $errmsg);
// 请求中没有包含token, 显示一个使用户可以输入token以进行验证的页面
// ** 你的代码 **
} catch (OAuthException2 $e) {
$errmsg = $e->getMessage();
// 请求了一个错误的token
// ** 你的代码 **
throw new CHttpException(401, $errmsg);
}
$data = array('rs' => $rs, 'model' => $model, 'errmsg' => $errmsg);
$this->render('Authorize', $data);
}
示例13: checkOAuthRequest
/**
* Verifies the OAuth request signature, sets the auth user
* and access type (read-only or read-write)
*
* @param OAuthRequest $request the OAuth Request
*
* @return nothing
*/
function checkOAuthRequest($request)
{
$datastore = new ApiGNUsocialOAuthDataStore();
$server = new OAuthServer($datastore);
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($hmac_method);
try {
$server->verify_request($request);
$consumer = $request->get_parameter('oauth_consumer_key');
$access_token = $request->get_parameter('oauth_token');
$app = Oauth_application::getByConsumerKey($consumer);
if (empty($app)) {
common_log(LOG_WARNING, 'API OAuth - Couldn\'t find the OAuth app for consumer key: ' . $consumer);
// TRANS: OAuth exception thrown when no application is found for a given consumer key.
throw new OAuthException(_('No application for that consumer key.'));
}
// set the source attr
if ($app->name != 'anonymous') {
$this->source = $app->name;
}
$appUser = Oauth_application_user::getKV('token', $access_token);
if (!empty($appUser)) {
// If access_type == 0 we have either a request token
// or a bad / revoked access token
if ($appUser->access_type != 0) {
// Set the access level for the api call
$this->access = $appUser->access_type & Oauth_application::$writeAccess ? self::READ_WRITE : self::READ_ONLY;
// Set the auth user
if (Event::handle('StartSetApiUser', array(&$user))) {
$user = User::getKV('id', $appUser->profile_id);
if (!empty($user)) {
if (!$user->hasRight(Right::API)) {
// TRANS: Authorization exception thrown when a user without API access tries to access the API.
throw new AuthorizationException(_('Not allowed to use API.'));
}
}
$this->auth_user = $user;
// FIXME: setting the value returned by common_current_user()
// There should probably be a better method for this. common_set_user()
// does lots of session stuff.
global $_cur;
$_cur = $this->auth_user;
Event::handle('EndSetApiUser', array($user));
}
$msg = "API OAuth authentication for user '%s' (id: %d) on behalf of " . "application '%s' (id: %d) with %s access.";
common_log(LOG_INFO, sprintf($msg, $this->auth_user->nickname, $this->auth_user->id, $app->name, $app->id, ($this->access = self::READ_WRITE) ? 'read-write' : 'read-only'));
} else {
// TRANS: OAuth exception given when an incorrect access token was given for a user.
throw new OAuthException(_('Bad access token.'));
}
} else {
// Also should not happen.
// TRANS: OAuth exception given when no user was found for a given token (no token was found).
throw new OAuthException(_('No user for that token.'));
}
} catch (OAuthException $e) {
$this->logAuthFailure($e->getMessage());
common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
$this->clientError($e->getMessage(), 401);
}
}
示例14: checkOAuthRequest
/**
* Verifies the OAuth request signature, sets the auth user
* and access type (read-only or read-write)
*
* @param OAuthRequest $request the OAuth Request
*
* @return nothing
*/
function checkOAuthRequest($request)
{
$datastore = new ApiStatusNetOAuthDataStore();
$server = new OAuthServer($datastore);
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$server->add_signature_method($hmac_method);
try {
$server->verify_request($request);
$consumer = $request->get_parameter('oauth_consumer_key');
$access_token = $request->get_parameter('oauth_token');
$app = Oauth_application::getByConsumerKey($consumer);
if (empty($app)) {
common_log(LOG_WARNING, 'Couldn\'t find the OAuth app for consumer key: ' . $consumer);
throw new OAuthException('No application for that consumer key.');
}
// set the source attr
$this->source = $app->name;
$appUser = Oauth_application_user::staticGet('token', $access_token);
if (!empty($appUser)) {
// If access_type == 0 we have either a request token
// or a bad / revoked access token
if ($appUser->access_type != 0) {
// Set the access level for the api call
$this->access = $appUser->access_type & Oauth_application::$writeAccess ? self::READ_WRITE : self::READ_ONLY;
// Set the auth user
if (Event::handle('StartSetApiUser', array(&$user))) {
$this->auth_user = User::staticGet('id', $appUser->profile_id);
Event::handle('EndSetApiUser', array($user));
}
$msg = "API OAuth authentication for user '%s' (id: %d) on behalf of " . "application '%s' (id: %d) with %s access.";
common_log(LOG_INFO, sprintf($msg, $this->auth_user->nickname, $this->auth_user->id, $app->name, $app->id, ($this->access = self::READ_WRITE) ? 'read-write' : 'read-only'));
} else {
throw new OAuthException('Bad access token.');
}
} else {
// Also should not happen
throw new OAuthException('No user for that token.');
}
} catch (OAuthException $e) {
common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
$this->clientError($e->getMessage(), 401, $this->format);
exit;
}
}
示例15: OAuthServer
<?php
$server = new OAuthServer(new DataApi_OAuthDataStore());
$server->add_signature_method(new OAuthSignatureMethod_HMAC_SHA1());
$request = OAuthRequest::from_request();
try {
if ($server->verify_request($request)) {
echo json_encode(true);
}
} catch (Exception $e) {
echo json_encode("Exception: " . $e->getMessage());
}
class DataApi_OAuthDataStore extends OAuthDataStore
{
function lookup_consumer($consumer_key)
{
$consumer_secrets = array('thisisakey' => 'thisisasecret', 'anotherkey' => 'f3ac5b093f3eab260520d8e3049561e6');
if (isset($consumer_secrets[$consumer_key])) {
return new OAuthConsumer($consumer_key, $consumer_secrets[$consumer_key], NULL);
} else {
return false;
}
}
function lookup_token($consumer, $token_type, $token)
{
// we are not using tokens, so return empty token
return new OAuthToken("", "");
}
function lookup_nonce($consumer, $token, $nonce, $timestamp)
{
// @todo lookup nonce and make sure it hasn't been used before (perhaps in combination with timestamp?)