public function setUp()
 {
     $this->storage = new PdoStorage(new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']));
     $this->storage->initDatabase();
     $this->storage->addClient(new ClientData(array('id' => 'token_client', 'name' => 'Token Client', 'allowed_scope' => 'read', 'redirect_uri' => 'https://example.org/callback.html', 'type' => 'token')));
     $this->storage->addClient(new ClientData(array('id' => 'code_client', 'name' => 'Code Client', 'secret' => 'foobar', 'allowed_scope' => 'read', 'redirect_uri' => 'https://example.org/callback', 'type' => 'code')));
     $ioStub = $this->getMockBuilder('fkooman\\OAuth\\Server\\IO')->getMock();
     $ioStub->method('getRandomHex')->will($this->onConsecutiveCalls('11111111'));
     $ioStub->method('getTime')->willReturn(1111111111);
     $compatStorage =& $this->storage;
     $basicAuthenticationPlugin = new BasicAuthentication(function ($userId) use($compatStorage) {
         return 'admin' === $userId ? password_hash('adm1n', PASSWORD_DEFAULT, array('cost' => 4)) : false;
     }, array('realm' => 'OAuth Server Authentication'));
     $pluginRegistry = new PluginRegistry();
     $pluginRegistry->registerDefaultPlugin($basicAuthenticationPlugin);
     $pluginRegistry->registerDefaultPlugin(new ReferrerCheckPlugin());
     $this->service = new AuthorizeService($this->storage, $ioStub, 5, false);
     $this->service->setPluginRegistry($pluginRegistry);
 }
예제 #2
0
 public function testOptionalPluginEnabledReturnObject()
 {
     $stub = $this->getMockBuilder('fkooman\\Rest\\ServicePluginInterface')->setMockClassName('Stub')->getMock();
     $stub->expects($this->any())->method('execute')->will($this->returnValue((object) array('foo' => 'bar')));
     $srv = array('SERVER_NAME' => 'www.example.org', 'SERVER_PORT' => 80, 'QUERY_STRING' => '', 'REQUEST_URI' => '/', 'SCRIPT_NAME' => '/index.php', 'REQUEST_METHOD' => 'GET');
     $request = new Request($srv);
     $route = new Route(array('GET'), '/', function () {
     }, array('Stub' => array('enabled' => true)));
     $p = new PluginRegistry();
     $p->registerOptionalPlugin($stub);
     $response = $p->run($request, $route);
     $this->assertObjectHasAttribute('foo', $response['stdClass']);
 }
 public function setUp()
 {
     $this->storage = new PdoStorage(new PDO($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD']));
     $this->storage->initDatabase();
     $this->storage->addClient(new ClientData(array('id' => 'code_client', 'name' => 'Code Client', 'secret' => 'abcdef', 'allowed_scope' => 'read write foo bar foobar', 'redirect_uri' => 'https://example.org/callback', 'type' => 'code')));
     $this->storage->addClient(new ClientData(array('id' => 'token_client', 'name' => 'Token Client', 'secret' => 'whynot', 'allowed_scope' => 'foo', 'redirect_uri' => 'https://example.org/callback.html', 'type' => 'token')));
     $ioStub = $this->getMockBuilder('fkooman\\OAuth\\Server\\IO')->getMock();
     $ioStub->method('getRandomHex')->will($this->onConsecutiveCalls('11111111'));
     $ioStub->method('getTime')->willReturn(1111111111);
     $this->storage->addApproval('code_client', 'admin', 'read write foo', 'r3fr3sh');
     $this->storage->storeAuthorizationCode('4uth0r1z4t10n', 'admin', 1111111222, 'code_client', null, 'read');
     $this->storage->storeAuthorizationCode('3xp1r3d4uth0r1z4t10n', 'admin', 1111110000, 'code_client', null, 'read');
     $this->storage->storeAuthorizationCode('authorizeRequestWithRedirectUri', 'admin', 1111111222, 'code_client', 'http://localhost/php-oauth/unit/test.html', 'read');
     $compatStorage =& $this->storage;
     $basicAuthenticationPlugin = new BasicAuthentication(function ($userId) use($compatStorage) {
         $clientData = $compatStorage->getClient($userId);
         return false !== $clientData ? password_hash($clientData->getSecret(), PASSWORD_DEFAULT, array('cost' => 4)) : false;
     }, array('realm' => 'OAuth Server'));
     $this->service = new TokenService($this->storage, $ioStub, 5);
     $pluginRegistry = new PluginRegistry();
     $pluginRegistry->registerDefaultPlugin($basicAuthenticationPlugin);
     $this->service->setPluginRegistry($pluginRegistry);
 }
예제 #4
0
 private function executeCallback(Request $request, Route $route, array $availableRouteCallbackParameters)
 {
     if (null !== $this->pluginRegistry) {
         $pluginResponse = $this->pluginRegistry->run($request, $route);
         if ($pluginResponse instanceof Response) {
             // received Response from plugin, return this immediately
             return $pluginResponse;
         }
         $availableRouteCallbackParameters = array_merge($availableRouteCallbackParameters, $pluginResponse);
     }
     $availableRouteCallbackParameters[get_class($request)] = $request;
     $response = $route->executeCallback($availableRouteCallbackParameters);
     if (!$response instanceof Response) {
         // if the response is a string, we assume it needs to be sent back
         // to the client as text/html
         if (!is_string($response)) {
             throw new RuntimeException('callback return value must be Response object or string');
         }
         $htmlResponse = new Response();
         $htmlResponse->setBody($response);
         return $htmlResponse;
     }
     return $response;
 }
예제 #5
0
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
require_once dirname(__DIR__) . '/vendor/autoload.php';
use fkooman\Ini\IniReader;
use fkooman\OAuth\Server\ApprovalsService;
use fkooman\OAuth\Server\Authenticator;
use fkooman\Rest\PluginRegistry;
use fkooman\OAuth\Server\PdoStorage;
use fkooman\Rest\Plugin\ReferrerCheck\ReferrerCheckPlugin;
use fkooman\Rest\ExceptionHandler;
ExceptionHandler::register();
$iniReader = IniReader::fromFile(dirname(__DIR__) . '/config/oauth.ini');
$db = new PDO($iniReader->v('PdoStorage', 'dsn'), $iniReader->v('PdoStorage', 'username', false), $iniReader->v('PdoStorage', 'password', false));
$auth = new Authenticator($iniReader);
$authenticationPlugin = $auth->getAuthenticationPlugin();
$service = new ApprovalsService(new PdoStorage($db));
$pluginRegistry = new PluginRegistry();
$pluginRegistry->registerDefaultPlugin(new ReferrerCheckPlugin());
$pluginRegistry->registerDefaultPlugin($authenticationPlugin);
$service->setPluginRegistry($pluginRegistry);
$service->run()->send();
예제 #6
0
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
require_once dirname(__DIR__) . '/vendor/autoload.php';
use fkooman\Ini\IniReader;
use fkooman\OAuth\Server\PdoStorage;
use fkooman\OAuth\Server\TokenService;
use fkooman\Rest\Plugin\Basic\BasicAuthentication;
use fkooman\Rest\PluginRegistry;
use fkooman\Rest\ExceptionHandler;
ExceptionHandler::register();
$iniReader = IniReader::fromFile(dirname(__DIR__) . '/config/oauth.ini');
$db = new PDO($iniReader->v('PdoStorage', 'dsn'), $iniReader->v('PdoStorage', 'username', false), $iniReader->v('PdoStorage', 'password', false));
$pdoStorage = new PdoStorage($db);
$basicAuthenticationPlugin = new BasicAuthentication(function ($userId) use($pdoStorage) {
    $clientData = $pdoStorage->getClient($userId);
    return false !== $clientData ? password_hash($clientData->getSecret(), PASSWORD_DEFAULT) : false;
}, 'OAuth Server');
$service = new TokenService($pdoStorage, null, $iniReader->v('accessTokenExpiry'));
$pluginRegistry = new PluginRegistry();
$pluginRegistry->registerDefaultPlugin($basicAuthenticationPlugin);
$service->setPluginRegistry($pluginRegistry);
$service->run()->send();