示例#1
0
 /**
  * Pretty much a helper function to set up the request
  *
  * @param \OAuth\Consumer $consumer
  * @param \OAuth\Token $token
  * @param string $httpMethod
  * @param string $httpUrl
  * @param array $parameters OPTIONAL
  * @return \OAuth\Request
  */
 public static function fromConsumerAndToken($consumer, $token, $httpMethod, $httpUrl, $parameters = null)
 {
     $parameters = $parameters ? $parameters : array();
     $defaults = array("oauth_version" => Request::$version, "oauth_nonce" => Request::generateNonce(), "oauth_timestamp" => Request::generateTimestamp(), "oauth_consumer_key" => $consumer->getKey());
     if ($token) {
         $defaults['oauth_token'] = $token->getKey();
     }
     $parameters = array_merge($defaults, $parameters);
     return new Request($httpMethod, $httpUrl, $parameters);
 }
 public function testRefreshTokenNoSubScope()
 {
     $h = new HttpRequest("https://auth.example.org/token", "POST");
     $h->setBasicAuthUser("testcodeclient");
     $h->setBasicAuthPass("abcdef");
     $h->setPostParameters(array("refresh_token" => "r3fr3sh", "scope" => "we want no sub scope", "grant_type" => "refresh_token"));
     $t = new Token($this->_config, NULL);
     $response = $t->handleRequest($h);
     $this->assertEquals(200, $response->getStatusCode());
     $this->assertRegexp('|^{"access_token":"[a-zA-Z0-9]+","expires_in":5,"scope":"read write foo","token_type":"bearer"}$|', $response->getContent());
 }
 */
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";
use RestService\Http\HttpResponse;
use RestService\Utils\Config;
use RestService\Http\IncomingHttpRequest;
use RestService\Http\HttpRequest;
use OAuth\Token;
use RestService\Utils\Logger;
use RestService\Utils\Json;
$logger = NULL;
$request = NULL;
$response = NULL;
try {
    $config = new Config(dirname(__DIR__) . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "oauth.ini");
    $logger = new Logger($config->getSectionValue('Log', 'logLevel'), $config->getValue('serviceName'), $config->getSectionValue('Log', 'logFile'), $config->getSectionValue('Log', 'logMail', FALSE));
    $t = new Token($config, $logger);
    $request = HttpRequest::fromIncomingHttpRequest(new IncomingHttpRequest());
    $response = $t->handleRequest($request);
} catch (Exception $e) {
    $response = new HttpResponse(500, "application/json");
    $response->setContent(Json::enc(array("error" => $e->getMessage())));
    if (NULL !== $logger) {
        $logger->logFatal($e->getMessage() . PHP_EOL . $request . PHP_EOL . $response);
    }
}
if (NULL !== $logger) {
    $logger->logDebug($request);
}
if (NULL !== $logger) {
    $logger->logDebug($response);
}