From c5aee314056f887383065dad9b75b4a679461fe8 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Fri, 9 May 2014 10:46:59 +0100 Subject: [PATCH] First commit of relational example --- .gitignore | 1 + examples/relational/Model/Users.php | 25 + .../relational/Storage/AccessTokenStorage.php | 93 +++ .../relational/Storage/AuthCodeStorage.php | 17 + examples/relational/Storage/ClientStorage.php | 41 ++ .../Storage/RefreshTokenStorage.php | 33 ++ examples/relational/Storage/ScopeStorage.php | 17 + .../relational/Storage/SessionStorage.php | 78 +++ examples/relational/api.php | 121 ++++ examples/relational/composer.json | 15 + examples/relational/composer.lock | 559 ++++++++++++++++++ examples/relational/config/db.php | 18 + examples/relational/config/init.php | 235 ++++++++ phpunit.xml | 2 +- 14 files changed, 1254 insertions(+), 1 deletion(-) create mode 100644 examples/relational/Model/Users.php create mode 100644 examples/relational/Storage/AccessTokenStorage.php create mode 100644 examples/relational/Storage/AuthCodeStorage.php create mode 100644 examples/relational/Storage/ClientStorage.php create mode 100644 examples/relational/Storage/RefreshTokenStorage.php create mode 100644 examples/relational/Storage/ScopeStorage.php create mode 100644 examples/relational/Storage/SessionStorage.php create mode 100644 examples/relational/api.php create mode 100644 examples/relational/composer.json create mode 100644 examples/relational/composer.lock create mode 100644 examples/relational/config/db.php create mode 100644 examples/relational/config/init.php diff --git a/.gitignore b/.gitignore index e26a243d..05ccc8c0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ /testing /examples/relational/vendor /examples/relational/config/oauth2.sqlite3 +/examples/relational/composer.lock /examples/nosql/vendor /examples/nosql/config/oauth2.sqlite3 \ No newline at end of file diff --git a/examples/relational/Model/Users.php b/examples/relational/Model/Users.php new file mode 100644 index 00000000..6e8f76de --- /dev/null +++ b/examples/relational/Model/Users.php @@ -0,0 +1,25 @@ +select(['username', 'name', 'email', 'photo']); + + if ($username !== null) { + $query->where('username', '=', $username); + } + + $result = $query->get(); + + if (count($result) > 0) { + return $result; + } + + return null; + } +} diff --git a/examples/relational/Storage/AccessTokenStorage.php b/examples/relational/Storage/AccessTokenStorage.php new file mode 100644 index 00000000..7339118c --- /dev/null +++ b/examples/relational/Storage/AccessTokenStorage.php @@ -0,0 +1,93 @@ +where('access_token', $token) + ->where('expire_time', '>=', time()) + ->get(); + + if (count($result) === 1) { + $token = new AccessTokenEntity($this->server); + $token->setExpireTime($result[0]['expire_time']); + $token->setToken($result[0]['access_token']); + + return $token; + } + + return null; + } + + /** + * {@inheritdoc} + */ + public function getByRefreshToken(RefreshTokenEntity $refreshToken) + { + die(var_dump(__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function getScopes(AbstractTokenEntity $token) + { + $result = Capsule::table('oauth_access_token_scopes') + ->select(['oauth_scopes.id', 'oauth_scopes.description']) + ->join('oauth_scopes', 'oauth_access_token_scopes.scope', '=', 'oauth_scopes.id') + ->where('access_token', $token->getToken()) + ->get(); + + $response = []; + + if (count($result) > 0) { + foreach ($result as $row) { + $scope = new ScopeEntity($this->server); + $scope->setId($row['id']); + $scope->setDescription($row['description']); + $response[] = $scope; + } + } + + return $response; + } + + /** + * {@inheritdoc} + */ + public function create($token, $expireTime, $sessionId) + { + die(var_dump(__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function associateScope(AbstractTokenEntity $token, ScopeEntity $scope) + { + die(var_dump(__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function delete(AbstractTokenEntity $token) + { + die(var_dump(__METHOD__, func_get_args())); + } +} diff --git a/examples/relational/Storage/AuthCodeStorage.php b/examples/relational/Storage/AuthCodeStorage.php new file mode 100644 index 00000000..d4d76304 --- /dev/null +++ b/examples/relational/Storage/AuthCodeStorage.php @@ -0,0 +1,17 @@ +select(['oauth_clients.id', 'oauth_clients.name']) + ->join('oauth_sessions', 'oauth_clients.id', '=', 'oauth_sessions.client_id') + ->where('oauth_sessions.id', $session->getId()) + ->get(); + + if (count($result) === 1) { + $client = new ClientEntity($this->server); + $client->setId($result[0]['id']); + $client->setName($result[0]['name']); + + return $client; + } + } +} diff --git a/examples/relational/Storage/RefreshTokenStorage.php b/examples/relational/Storage/RefreshTokenStorage.php new file mode 100644 index 00000000..27b81dc7 --- /dev/null +++ b/examples/relational/Storage/RefreshTokenStorage.php @@ -0,0 +1,33 @@ +select(['oauth_sessions.id', 'oauth_sessions.owner_type', 'oauth_sessions.owner_id', 'oauth_sessions.client_id', 'oauth_sessions.client_redirect_uri']) + ->join('oauth_access_tokens', 'oauth_access_tokens.session_id', '=', 'oauth_sessions.id') + ->where('oauth_access_tokens.access_token', $accessToken->getToken()) + ->get(); + + if (count($result) === 1) { + // die(var_dump($result)); + $session = new SessionEntity($this->server); + $session->setId($result[0]['id']); + $session->setOwner($result[0]['owner_type'], $result[0]['owner_id']); + + return $session; + } + + return null; + } + + /** + * {@inheritdoc} + */ + public function getByAuthCode(AuthCodeEntity $authCode) + { + die(var_dump(__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function getScopes(SessionEntity $session) + { + die(var_dump(__CLASS__.'::'.__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null) + { + die(var_dump(__CLASS__.'::'.__METHOD__, func_get_args())); + } + + /** + * {@inheritdoc} + */ + public function associateScope(SessionEntity $session, ScopeEntity $scope) + { + die(var_dump(__CLASS__.'::'.__METHOD__, func_get_args())); + } +} diff --git a/examples/relational/api.php b/examples/relational/api.php new file mode 100644 index 00000000..4219e6cd --- /dev/null +++ b/examples/relational/api.php @@ -0,0 +1,121 @@ +createFromGlobals(); +$router = new \Orno\Route\RouteCollection; +$router->setStrategy(\Orno\Route\RouteStrategyInterface::RESTFUL_STRATEGY); + +// Set up the OAuth 2.0 resource server +$sessionStorage = new Storage\SessionStorage(); +$accessTokenStorage = new Storage\AccessTokenStorage(); +$clientStorage = new Storage\ClientStorage(); +$scopeStorage = new Storage\ScopeStorage(); + +$server = new ResourceServer( + $sessionStorage, + $accessTokenStorage, + $clientStorage, + $scopeStorage +); + +$server->setRequest($request); + +// Check that access token is present +try { + $server->isValidRequest(false); +} catch (\League\OAuth2\Server\Exception\OAuthException $e) { + + foreach ($e->getHttpHeaders() as $header) { + header($header); + } + + echo json_encode([ + 'error' => $e->errorType, + 'message' => $e->getMessage() + ]); + + exit; +} + +// GET /tokeninfo +$router->get('/tokeninfo', function (Request $request) use ($server) { + + $token = [ + 'owner_id' => $server->getOwnerId(), + 'owner_type' => $server->getOwnerType(), + 'access_token' => $server->getAccessToken(), + 'client_id' => $server->getClientId(), + 'scopes' => $server->getScopes() + ]; + + return new JsonResponse($token); + +}); + +// GET /users +$router->get('/users', function (Request $request) use ($server) { + + $results = (new Model\Users())->get(); + + $users = []; + + foreach ($results as $result) { + $user = [ + 'username' => $result['username'], + 'name' => $result['name'] + ]; + + if ($server->hasScope('email')) { + $user['email'] = $result['email']; + } + + if ($server->hasScope('photo')) { + $user['photo'] = $result['photo']; + } + + $users[] = $user; + } + + return new JsonResponse($users); +}); + +// GET /users/{username} +$router->get('/users/{username}', function (Request $request, $args) use ($server) { + + $result = (new Model\Users())->get($args['username']); + + if (count($result) === 0) { + throw new NotFoundException(); + } + + $user = [ + 'username' => $result[0]['username'], + 'name' => $result[0]['name'] + ]; + + if ($server->hasScope('email')) { + $user['email'] = $result[0]['email']; + } + + if ($server->hasScope('photo')) { + $user['photo'] = $result[0]['photo']; + } + + return new JsonResponse($user); +}); + +$dispatcher = $router->getDispatcher(); +$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo()); +$response->send(); diff --git a/examples/relational/composer.json b/examples/relational/composer.json new file mode 100644 index 00000000..01421e39 --- /dev/null +++ b/examples/relational/composer.json @@ -0,0 +1,15 @@ +{ + "require": { + "illuminate/database": "4.1.*", + "orno/route": "1.*" + }, + "autoload": { + "psr-4": { + "League\\OAuth2\\Server\\": "../../src/", + "RelationalExample\\": "." + }, + "files": [ + "config/db.php" + ] + } +} \ No newline at end of file diff --git a/examples/relational/composer.lock b/examples/relational/composer.lock new file mode 100644 index 00000000..96e1720a --- /dev/null +++ b/examples/relational/composer.lock @@ -0,0 +1,559 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" + ], + "hash": "abcfdbc3594f0666218cab21fbbf0829", + "packages": [ + { + "name": "fzaninotto/faker", + "version": "v1.3.0", + "source": { + "type": "git", + "url": "https://github.com/fzaninotto/Faker.git", + "reference": "1d143fd8caf4d264602450bc01d7484af788706b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fzaninotto/Faker/zipball/1d143fd8caf4d264602450bc01d7484af788706b", + "reference": "1d143fd8caf4d264602450bc01d7484af788706b", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.0-dev" + } + }, + "autoload": { + "psr-0": { + "Faker": "src/", + "Faker\\PHPUnit": "test/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "François Zaninotto" + } + ], + "description": "Faker is a PHP library that generates fake data for you.", + "keywords": [ + "data", + "faker", + "fixtures" + ], + "time": "2013-12-16 21:56:48" + }, + { + "name": "illuminate/container", + "version": "v4.1.28", + "target-dir": "Illuminate/Container", + "source": { + "type": "git", + "url": "https://github.com/illuminate/container.git", + "reference": "51d590fd7e301f56c567459a26343a8288330f69" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/container/zipball/51d590fd7e301f56c567459a26343a8288330f69", + "reference": "51d590fd7e301f56c567459a26343a8288330f69", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "4.0.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "psr-0": { + "Illuminate\\Container": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com", + "homepage": "https://github.com/taylorotwell", + "role": "Developer" + } + ], + "time": "2014-03-22 20:29:54" + }, + { + "name": "illuminate/database", + "version": "v4.1.28", + "target-dir": "Illuminate/Database", + "source": { + "type": "git", + "url": "https://github.com/illuminate/database.git", + "reference": "c84f39734c6bfe7fbf694ff8dc33d15605553de2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/database/zipball/c84f39734c6bfe7fbf694ff8dc33d15605553de2", + "reference": "c84f39734c6bfe7fbf694ff8dc33d15605553de2", + "shasum": "" + }, + "require": { + "illuminate/container": "4.1.*", + "illuminate/events": "4.1.*", + "illuminate/support": "4.1.*", + "nesbot/carbon": "1.*", + "php": ">=5.3.0" + }, + "require-dev": { + "illuminate/cache": "4.1.*", + "illuminate/console": "4.1.*", + "illuminate/filesystem": "4.1.*", + "illuminate/pagination": "4.1.*", + "illuminate/support": "4.1.*", + "mockery/mockery": "0.9.*", + "phpunit/phpunit": "4.0.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "psr-0": { + "Illuminate\\Database": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com", + "homepage": "https://github.com/taylorotwell", + "role": "Developer" + } + ], + "keywords": [ + "database", + "laravel", + "orm", + "sql" + ], + "time": "2014-04-11 20:10:04" + }, + { + "name": "illuminate/events", + "version": "v4.1.28", + "target-dir": "Illuminate/Events", + "source": { + "type": "git", + "url": "https://github.com/illuminate/events.git", + "reference": "ef6ae5962134a35667f24349bc2c9b8ad1e20c8b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/events/zipball/ef6ae5962134a35667f24349bc2c9b8ad1e20c8b", + "reference": "ef6ae5962134a35667f24349bc2c9b8ad1e20c8b", + "shasum": "" + }, + "require": { + "illuminate/container": "4.1.*", + "illuminate/support": "4.1.*", + "php": ">=5.3.0" + }, + "require-dev": { + "mockery/mockery": "0.9.*", + "phpunit/phpunit": "4.0.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "psr-0": { + "Illuminate\\Events": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com", + "homepage": "https://github.com/taylorotwell", + "role": "Developer" + } + ], + "time": "2014-03-22 20:12:48" + }, + { + "name": "illuminate/support", + "version": "v4.1.28", + "target-dir": "Illuminate/Support", + "source": { + "type": "git", + "url": "https://github.com/illuminate/support.git", + "reference": "69b248f240719e8d764ee6ec0384819fa57d60bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/illuminate/support/zipball/69b248f240719e8d764ee6ec0384819fa57d60bb", + "reference": "69b248f240719e8d764ee6ec0384819fa57d60bb", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "jeremeamia/superclosure": "1.0.*", + "mockery/mockery": "0.9.*", + "patchwork/utf8": "1.1.*", + "phpunit/phpunit": "4.0.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "psr-0": { + "Illuminate\\Support": "" + }, + "files": [ + "Illuminate/Support/helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com", + "homepage": "https://github.com/taylorotwell", + "role": "Developer" + } + ], + "time": "2014-03-22 20:29:54" + }, + { + "name": "nesbot/carbon", + "version": "1.8.0", + "source": { + "type": "git", + "url": "https://github.com/briannesbitt/Carbon.git", + "reference": "21c4cb4301969c7d85aee8a62eefdfa881413af0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/21c4cb4301969c7d85aee8a62eefdfa881413af0", + "reference": "21c4cb4301969c7d85aee8a62eefdfa881413af0", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*" + }, + "type": "library", + "autoload": { + "psr-0": { + "Carbon": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Brian Nesbitt", + "email": "brian@nesbot.com", + "homepage": "http://nesbot.com" + } + ], + "description": "A simple API extension for DateTime.", + "homepage": "https://github.com/briannesbitt/Carbon", + "keywords": [ + "date", + "datetime", + "time" + ], + "time": "2014-01-07 05:10:44" + }, + { + "name": "nikic/fast-route", + "version": "v0.1.0", + "source": { + "type": "git", + "url": "https://github.com/nikic/FastRoute.git", + "reference": "29c5bf70254d8ce59414646de3e9e43aafc9735e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nikic/FastRoute/zipball/29c5bf70254d8ce59414646de3e9e43aafc9735e", + "reference": "29c5bf70254d8ce59414646de3e9e43aafc9735e", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "type": "library", + "autoload": { + "files": [ + "src/bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Nikita Popov", + "email": "nikic@php.net" + } + ], + "description": "Fast request router for PHP", + "keywords": [ + "router", + "routing" + ], + "time": "2014-03-04 15:05:38" + }, + { + "name": "orno/di", + "version": "v2.0.6", + "source": { + "type": "git", + "url": "https://github.com/orno/di.git", + "reference": "e8d29523da316125e67a6d39d477da90dc7be656" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/orno/di/zipball/e8d29523da316125e67a6d39d477da90dc7be656", + "reference": "e8d29523da316125e67a6d39d477da90dc7be656", + "shasum": "" + }, + "require": { + "php": ">=5.4" + }, + "suggest": { + "orno/cache": "Enables caching of reflection based resolution", + "orno/config": "Allows useage of config files rather than interacting with the container directly" + }, + "type": "library", + "autoload": { + "psr-0": { + "Orno\\Di": "src/", + "OrnoTest": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Phil Bennett", + "email": "philipobenito@gmail.com" + } + ], + "description": "Orno Di is a small but powerful dependency injection container with automatic resolution of dependencies.", + "keywords": [ + "container", + "dependency", + "di", + "injection", + "orno" + ], + "time": "2014-04-03 08:23:38" + }, + { + "name": "orno/http", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/orno/http.git", + "reference": "65d37f6f92f3357c234a592fdd57b2f54c2ef514" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/orno/http/zipball/65d37f6f92f3357c234a592fdd57b2f54c2ef514", + "reference": "65d37f6f92f3357c234a592fdd57b2f54c2ef514", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "symfony/http-foundation": "2.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "Orno\\Http\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Phil Bennett", + "email": "philipobenito@gmail.com" + } + ], + "description": "A wrapper for Symfony\\HttpFoundation with some encapsulation and convenience methods.", + "homepage": "http://github.com/orno/http", + "keywords": [ + "http", + "orno", + "request", + "response" + ], + "time": "2014-03-05 10:22:10" + }, + { + "name": "orno/route", + "version": "v1.0.2", + "source": { + "type": "git", + "url": "https://github.com/orno/route.git", + "reference": "af2aee8314e6e700b4d69dc61f5c67ae030741e9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/orno/route/zipball/af2aee8314e6e700b4d69dc61f5c67ae030741e9", + "reference": "af2aee8314e6e700b4d69dc61f5c67ae030741e9", + "shasum": "" + }, + "require": { + "nikic/fast-route": "0.1.*", + "orno/di": "2.*", + "orno/http": "1.*", + "php": ">=5.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "Orno\\Route\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Phil Bennett", + "email": "hello@happyaccidents.me" + } + ], + "description": "Fast routing package combining Nikita Popov's FastRoute and Orno\\Di to provide route dispatching to dependency injected controllers.", + "keywords": [ + "FastRoute", + "di", + "dispatcher", + "orno", + "route" + ], + "time": "2014-04-13 13:46:11" + }, + { + "name": "symfony/http-foundation", + "version": "v2.4.4", + "target-dir": "Symfony/Component/HttpFoundation", + "source": { + "type": "git", + "url": "https://github.com/symfony/HttpFoundation.git", + "reference": "22c4dee84271ad0cd08d19f26d89f2878e11159b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/HttpFoundation/zipball/22c4dee84271ad0cd08d19f26d89f2878e11159b", + "reference": "22c4dee84271ad0cd08d19f26d89f2878e11159b", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "symfony/expression-language": "~2.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.4-dev" + } + }, + "autoload": { + "psr-0": { + "Symfony\\Component\\HttpFoundation\\": "" + }, + "classmap": [ + "Symfony/Component/HttpFoundation/Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Symfony Community", + "homepage": "http://symfony.com/contributors" + } + ], + "description": "Symfony HttpFoundation Component", + "homepage": "http://symfony.com", + "time": "2014-04-18 21:02:05" + } + ], + "packages-dev": [ + + ], + "aliases": [ + + ], + "minimum-stability": "stable", + "stability-flags": [ + + ], + "platform": [ + + ], + "platform-dev": [ + + ] +} diff --git a/examples/relational/config/db.php b/examples/relational/config/db.php new file mode 100644 index 00000000..80eaf687 --- /dev/null +++ b/examples/relational/config/db.php @@ -0,0 +1,18 @@ +addConnection([ + 'driver' => 'sqlite', + 'database' => __DIR__.'/oauth2.sqlite3', + 'charset' => 'utf8', + 'collation' => 'utf8_unicode_ci' +]); + +$capsule->setAsGlobal(); diff --git a/examples/relational/config/init.php b/examples/relational/config/init.php new file mode 100644 index 00000000..9bf4b7f9 --- /dev/null +++ b/examples/relational/config/init.php @@ -0,0 +1,235 @@ +create('users', function ($table) { + $table->increments('id'); + $table->string('username'); + $table->string('password'); + $table->string('name'); + $table->string('email'); + $table->string('photo'); +}); + +Capsule::table('users')->insert([ + 'username' => 'alexbilbie', + 'password' => password_hash('whisky', PASSWORD_DEFAULT), + 'name' => 'Alex Bilbie', + 'email' => 'hello@alexbilbie.com', + 'photo' => 'https://s.gravatar.com/avatar/14902eb1dac66b8458ebbb481d80f0a3' +]); + +Capsule::table('users')->insert([ + 'username' => 'philsturgeon', + 'password' => password_hash('cider', PASSWORD_DEFAULT), + 'name' => 'Phil Sturgeon', + 'email' => 'email@philsturgeon.co.uk', + 'photo' => 'https://s.gravatar.com/avatar/14df293d6c5cd6f05996dfc606a6a951' +]); + +/******************************************************************************/ + +print 'Creating clients table'.PHP_EOL; + +Capsule::schema()->create('oauth_clients', function ($table) { + $table->string('id'); + $table->string('secret'); + $table->string('name'); + $table->primary('id'); +}); + +Capsule::table('oauth_clients')->insert([ + 'id' => 'testclient', + 'secret' => 'secret', + 'name' => 'Test Client' +]); + +/******************************************************************************/ + +print 'Creating scopes table'.PHP_EOL; + +Capsule::schema()->create('oauth_scopes', function ($table) { + $table->string('id'); + $table->string('description'); + $table->primary('id'); +}); + +Capsule::table('oauth_scopes')->insert([ + 'id' => 'basic', + 'description' => 'Basic details about your account' +]); + +Capsule::table('oauth_scopes')->insert([ + 'id' => 'email', + 'description' => 'Your email address' +]); + +Capsule::table('oauth_scopes')->insert([ + 'id' => 'photo', + 'description' => 'Your photo' +]); + +/******************************************************************************/ + +print 'Creating sessions table'.PHP_EOL; + +Capsule::schema()->create('oauth_sessions', function ($table) { + $table->increments('id'); + $table->string('owner_type'); + $table->string('owner_id'); + $table->string('client_id'); + $table->string('client_redirect_uri')->nullable(); + + $table->foreign('client_id')->references('id')->on('oauth_clients')->onDelete('cascade'); +}); + +Capsule::table('oauth_sessions')->insert([ + 'owner_type' => 'client', + 'owner_id' => 'testclient', + 'client_id' => 'testclient' +]); + +Capsule::table('oauth_sessions')->insert([ + 'owner_type' => 'user', + 'owner_id' => '1', + 'client_id' => 'testclient' +]); + +Capsule::table('oauth_sessions')->insert([ + 'owner_type' => 'user', + 'owner_id' => '2', + 'client_id' => 'testclient' +]); + +/******************************************************************************/ + +print 'Creating access tokens table'.PHP_EOL; + +Capsule::schema()->create('oauth_access_tokens', function ($table) { + $table->string('access_token')->primary(); + $table->integer('session_id'); + $table->integer('expire_time'); + + $table->foreign('session_id')->references('id')->on('oauth_sessions')->onDelete('cascade'); +}); + +Capsule::table('oauth_access_tokens')->insert([ + 'access_token' => 'iamgod', + 'session_id' => '1', + 'expire_time' => time() + 86400 +]); + +Capsule::table('oauth_access_tokens')->insert([ + 'access_token' => 'iamalex', + 'session_id' => '2', + 'expire_time' => time() + 86400 +]); + +Capsule::table('oauth_access_tokens')->insert([ + 'access_token' => 'iamphil', + 'session_id' => '3', + 'expire_time' => time() + 86400 +]); + +/******************************************************************************/ + +print 'Creating refresh tokens table'.PHP_EOL; + +Capsule::schema()->create('oauth_refresh_tokens', function ($table) { + $table->string('refresh_token')->primary(); + $table->integer('session_id'); + $table->integer('expire_time'); + $table->string('access_token'); + + $table->foreign('session_id')->references('id')->on('oauth_sessions')->onDelete('cascade'); + $table->foreign('access_token')->references('id')->on('oauth_access_tokens')->onDelete('cascade'); +}); + +/******************************************************************************/ + +print 'Creating auth codes table'.PHP_EOL; + +Capsule::schema()->create('oauth_auth_codes', function ($table) { + $table->string('auth_code')->primary(); + $table->integer('session_id'); + $table->integer('expire_time'); + + $table->foreign('session_id')->references('id')->on('oauth_sessions')->onDelete('cascade'); +}); + +/******************************************************************************/ + +print 'Creating oauth access token scopes table'.PHP_EOL; + +Capsule::schema()->create('oauth_access_token_scopes', function ($table) { + $table->increments('id'); + $table->string('access_token'); + $table->string('scope'); + + $table->foreign('access_token')->references('access_token')->on('oauth_access_tokens')->onDelete('cascade'); + $table->foreign('scope')->references('id')->on('oauth_scopes')->onDelete('cascade'); +}); + +Capsule::table('oauth_access_token_scopes')->insert([ + 'access_token' => 'iamgod', + 'scope' => 'basic' +]); + +Capsule::table('oauth_access_token_scopes')->insert([ + 'access_token' => 'iamgod', + 'scope' => 'email' +]); + +Capsule::table('oauth_access_token_scopes')->insert([ + 'access_token' => 'iamgod', + 'scope' => 'photo' +]); + +Capsule::table('oauth_access_token_scopes')->insert([ + 'access_token' => 'iamphil', + 'scope' => 'email' +]); + +Capsule::table('oauth_access_token_scopes')->insert([ + 'access_token' => 'iamalex', + 'scope' => 'photo' +]); + +/******************************************************************************/ + +print 'Creating oauth auth code scopes table'.PHP_EOL; + +Capsule::schema()->create('oauth_auth_code_scopes', function ($table) { + $table->increments('id'); + $table->string('auth_code'); + $table->string('scope'); + + $table->foreign('auth_code')->references('auth_code')->on('oauth_auth_codes')->onDelete('cascade'); + $table->foreign('scope')->references('id')->on('oauth_scopes')->onDelete('cascade'); +}); + +/******************************************************************************/ + +print 'Creating oauth session scopes table'.PHP_EOL; + +Capsule::schema()->create('oauth_session_scopes', function ($table) { + $table->increments('id'); + $table->string('session_id'); + $table->string('scope'); + + $table->foreign('session_id')->references('id')->on('oauth_sessions')->onDelete('cascade'); + $table->foreign('scope')->references('id')->on('oauth_scopes')->onDelete('cascade'); +}); diff --git a/phpunit.xml b/phpunit.xml index 23497901..75670ca4 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -11,7 +11,7 @@ - +