mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-01-03 10:41:51 +05:30
Removed old examples
This commit is contained in:
parent
f357602090
commit
a16a1dbb7d
@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Model;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
class Users
|
||||
{
|
||||
public function get($username = null)
|
||||
{
|
||||
$query = Capsule::table('users')->select(['username', 'password', 'name', 'email', 'photo']);
|
||||
|
||||
if ($username !== null) {
|
||||
$query->where('username', '=', $username);
|
||||
}
|
||||
|
||||
$result = $query->get();
|
||||
|
||||
if (count($result) > 0) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Storage;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use League\OAuth2\Server\Entity\AccessTokenEntity;
|
||||
use League\OAuth2\Server\Entity\ScopeEntity;
|
||||
use League\OAuth2\Server\Storage\AbstractStorage;
|
||||
use League\OAuth2\Server\Storage\AccessTokenInterface;
|
||||
|
||||
class AccessTokenStorage extends AbstractStorage implements AccessTokenInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($token)
|
||||
{
|
||||
$result = Capsule::table('oauth_access_tokens')
|
||||
->where('access_token', $token)
|
||||
->get();
|
||||
|
||||
if (count($result) === 1) {
|
||||
$token = (new AccessTokenEntity($this->server))
|
||||
->setId($result[0]['access_token'])
|
||||
->setExpireTime($result[0]['expire_time']);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getScopes(AccessTokenEntity $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->getId())
|
||||
->get();
|
||||
|
||||
$response = [];
|
||||
|
||||
if (count($result) > 0) {
|
||||
foreach ($result as $row) {
|
||||
$scope = (new ScopeEntity($this->server))->hydrate([
|
||||
'id' => $row['id'],
|
||||
'description' => $row['description'],
|
||||
]);
|
||||
$response[] = $scope;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create($token, $expireTime, $sessionId)
|
||||
{
|
||||
Capsule::table('oauth_access_tokens')
|
||||
->insert([
|
||||
'access_token' => $token,
|
||||
'session_id' => $sessionId,
|
||||
'expire_time' => $expireTime,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function associateScope(AccessTokenEntity $token, ScopeEntity $scope)
|
||||
{
|
||||
Capsule::table('oauth_access_token_scopes')
|
||||
->insert([
|
||||
'access_token' => $token->getId(),
|
||||
'scope' => $scope->getId(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete(AccessTokenEntity $token)
|
||||
{
|
||||
Capsule::table('oauth_access_token_scopes')
|
||||
->where('access_token', $token->getId())
|
||||
->delete();
|
||||
}
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Storage;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use League\OAuth2\Server\Entity\AuthCodeEntity;
|
||||
use League\OAuth2\Server\Entity\ScopeEntity;
|
||||
use League\OAuth2\Server\Storage\AbstractStorage;
|
||||
use League\OAuth2\Server\Storage\AuthCodeInterface;
|
||||
|
||||
class AuthCodeStorage extends AbstractStorage implements AuthCodeInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($code)
|
||||
{
|
||||
$result = Capsule::table('oauth_auth_codes')
|
||||
->where('auth_code', $code)
|
||||
->where('expire_time', '>=', time())
|
||||
->get();
|
||||
|
||||
if (count($result) === 1) {
|
||||
$token = new AuthCodeEntity($this->server);
|
||||
$token->setId($result[0]['auth_code']);
|
||||
$token->setRedirectUri($result[0]['client_redirect_uri']);
|
||||
$token->setExpireTime($result[0]['expire_time']);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
public function create($token, $expireTime, $sessionId, $redirectUri)
|
||||
{
|
||||
Capsule::table('oauth_auth_codes')
|
||||
->insert([
|
||||
'auth_code' => $token,
|
||||
'client_redirect_uri' => $redirectUri,
|
||||
'session_id' => $sessionId,
|
||||
'expire_time' => $expireTime,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getScopes(AuthCodeEntity $token)
|
||||
{
|
||||
$result = Capsule::table('oauth_auth_code_scopes')
|
||||
->select(['oauth_scopes.id', 'oauth_scopes.description'])
|
||||
->join('oauth_scopes', 'oauth_auth_code_scopes.scope', '=', 'oauth_scopes.id')
|
||||
->where('auth_code', $token->getId())
|
||||
->get();
|
||||
|
||||
$response = [];
|
||||
|
||||
if (count($result) > 0) {
|
||||
foreach ($result as $row) {
|
||||
$scope = (new ScopeEntity($this->server))->hydrate([
|
||||
'id' => $row['id'],
|
||||
'description' => $row['description'],
|
||||
]);
|
||||
$response[] = $scope;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function associateScope(AuthCodeEntity $token, ScopeEntity $scope)
|
||||
{
|
||||
Capsule::table('oauth_auth_code_scopes')
|
||||
->insert([
|
||||
'auth_code' => $token->getId(),
|
||||
'scope' => $scope->getId(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete(AuthCodeEntity $token)
|
||||
{
|
||||
Capsule::table('oauth_auth_codes')
|
||||
->where('auth_code', $token->getId())
|
||||
->delete();
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Storage;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use League\OAuth2\Server\Entity\ClientEntity;
|
||||
use League\OAuth2\Server\Entity\SessionEntity;
|
||||
use League\OAuth2\Server\Storage\AbstractStorage;
|
||||
use League\OAuth2\Server\Storage\ClientInterface;
|
||||
|
||||
class ClientStorage extends AbstractStorage implements ClientInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($clientId, $clientSecret = null, $redirectUri = null, $grantType = null)
|
||||
{
|
||||
$query = Capsule::table('oauth_clients')
|
||||
->select('oauth_clients.*')
|
||||
->where('oauth_clients.id', $clientId);
|
||||
|
||||
if ($clientSecret !== null) {
|
||||
$query->where('oauth_clients.secret', $clientSecret);
|
||||
}
|
||||
|
||||
if ($redirectUri) {
|
||||
$query->join('oauth_client_redirect_uris', 'oauth_clients.id', '=', 'oauth_client_redirect_uris.client_id')
|
||||
->select(['oauth_clients.*', 'oauth_client_redirect_uris.*'])
|
||||
->where('oauth_client_redirect_uris.redirect_uri', $redirectUri);
|
||||
}
|
||||
|
||||
$result = $query->get();
|
||||
|
||||
if (count($result) === 1) {
|
||||
$client = new ClientEntity($this->server);
|
||||
$client->hydrate([
|
||||
'id' => $result[0]['id'],
|
||||
'name' => $result[0]['name'],
|
||||
]);
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBySession(SessionEntity $session)
|
||||
{
|
||||
$result = Capsule::table('oauth_clients')
|
||||
->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->hydrate([
|
||||
'id' => $result[0]['id'],
|
||||
'name' => $result[0]['name'],
|
||||
]);
|
||||
|
||||
return $client;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Storage;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use League\OAuth2\Server\Entity\RefreshTokenEntity;
|
||||
use League\OAuth2\Server\Storage\AbstractStorage;
|
||||
use League\OAuth2\Server\Storage\RefreshTokenInterface;
|
||||
|
||||
class RefreshTokenStorage extends AbstractStorage implements RefreshTokenInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($token)
|
||||
{
|
||||
$result = Capsule::table('oauth_refresh_tokens')
|
||||
->where('refresh_token', $token)
|
||||
->get();
|
||||
|
||||
if (count($result) === 1) {
|
||||
$token = (new RefreshTokenEntity($this->server))
|
||||
->setId($result[0]['refresh_token'])
|
||||
->setExpireTime($result[0]['expire_time'])
|
||||
->setAccessTokenId($result[0]['access_token']);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create($token, $expireTime, $accessToken)
|
||||
{
|
||||
Capsule::table('oauth_refresh_tokens')
|
||||
->insert([
|
||||
'refresh_token' => $token,
|
||||
'access_token' => $accessToken,
|
||||
'expire_time' => $expireTime,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function delete(RefreshTokenEntity $token)
|
||||
{
|
||||
Capsule::table('oauth_refresh_tokens')
|
||||
->where('refresh_token', $token->getId())
|
||||
->delete();
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Storage;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use League\OAuth2\Server\Entity\ScopeEntity;
|
||||
use League\OAuth2\Server\Storage\AbstractStorage;
|
||||
use League\OAuth2\Server\Storage\ScopeInterface;
|
||||
|
||||
class ScopeStorage extends AbstractStorage implements ScopeInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function get($scope, $grantType = null, $clientId = null)
|
||||
{
|
||||
$result = Capsule::table('oauth_scopes')
|
||||
->where('id', $scope)
|
||||
->get();
|
||||
|
||||
if (count($result) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
return (new ScopeEntity($this->server))->hydrate([
|
||||
'id' => $result[0]['id'],
|
||||
'description' => $result[0]['description'],
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Storage;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
use League\OAuth2\Server\Entity\AccessTokenEntity;
|
||||
use League\OAuth2\Server\Entity\AuthCodeEntity;
|
||||
use League\OAuth2\Server\Entity\ScopeEntity;
|
||||
use League\OAuth2\Server\Entity\SessionEntity;
|
||||
use League\OAuth2\Server\Storage\AbstractStorage;
|
||||
use League\OAuth2\Server\Storage\SessionInterface;
|
||||
|
||||
class SessionStorage extends AbstractStorage implements SessionInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getByAccessToken(AccessTokenEntity $accessToken)
|
||||
{
|
||||
$result = Capsule::table('oauth_sessions')
|
||||
->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->getId())
|
||||
->get();
|
||||
|
||||
if (count($result) === 1) {
|
||||
$session = new SessionEntity($this->server);
|
||||
$session->setId($result[0]['id']);
|
||||
$session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getByAuthCode(AuthCodeEntity $authCode)
|
||||
{
|
||||
$result = Capsule::table('oauth_sessions')
|
||||
->select(['oauth_sessions.id', 'oauth_sessions.owner_type', 'oauth_sessions.owner_id', 'oauth_sessions.client_id', 'oauth_sessions.client_redirect_uri'])
|
||||
->join('oauth_auth_codes', 'oauth_auth_codes.session_id', '=', 'oauth_sessions.id')
|
||||
->where('oauth_auth_codes.auth_code', $authCode->getId())
|
||||
->get();
|
||||
|
||||
if (count($result) === 1) {
|
||||
$session = new SessionEntity($this->server);
|
||||
$session->setId($result[0]['id']);
|
||||
$session->setOwner($result[0]['owner_type'], $result[0]['owner_id']);
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getScopes(SessionEntity $session)
|
||||
{
|
||||
$result = Capsule::table('oauth_sessions')
|
||||
->select('oauth_scopes.*')
|
||||
->join('oauth_session_scopes', 'oauth_sessions.id', '=', 'oauth_session_scopes.session_id')
|
||||
->join('oauth_scopes', 'oauth_scopes.id', '=', 'oauth_session_scopes.scope')
|
||||
->where('oauth_sessions.id', $session->getId())
|
||||
->get();
|
||||
|
||||
$scopes = [];
|
||||
|
||||
foreach ($result as $scope) {
|
||||
$scopes[] = (new ScopeEntity($this->server))->hydrate([
|
||||
'id' => $scope['id'],
|
||||
'description' => $scope['description'],
|
||||
]);
|
||||
}
|
||||
|
||||
return $scopes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null)
|
||||
{
|
||||
$id = Capsule::table('oauth_sessions')
|
||||
->insertGetId([
|
||||
'owner_type' => $ownerType,
|
||||
'owner_id' => $ownerId,
|
||||
'client_id' => $clientId,
|
||||
]);
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function associateScope(SessionEntity $session, ScopeEntity $scope)
|
||||
{
|
||||
Capsule::table('oauth_session_scopes')
|
||||
->insert([
|
||||
'session_id' => $session->getId(),
|
||||
'scope' => $scope->getId(),
|
||||
]);
|
||||
}
|
||||
}
|
@ -1,135 +0,0 @@
|
||||
<?php
|
||||
|
||||
use League\OAuth2\Server\ResourceServer;
|
||||
use Orno\Http\Exception\NotFoundException;
|
||||
use Orno\Http\Request;
|
||||
use Orno\Http\Response;
|
||||
use RelationalExample\Model;
|
||||
use RelationalExample\Storage;
|
||||
|
||||
include __DIR__.'/vendor/autoload.php';
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request())->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
|
||||
);
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request())->createFromGlobals();
|
||||
$router = new \Orno\Route\RouteCollection();
|
||||
|
||||
// GET /tokeninfo
|
||||
$router->get('/tokeninfo', function (Request $request) use ($server) {
|
||||
|
||||
$accessToken = $server->getAccessToken();
|
||||
$session = $server->getSessionStorage()->getByAccessToken($accessToken);
|
||||
$token = [
|
||||
'owner_id' => $session->getOwnerId(),
|
||||
'owner_type' => $session->getOwnerType(),
|
||||
'access_token' => $accessToken,
|
||||
'client_id' => $session->getClient()->getId(),
|
||||
'scopes' => $accessToken->getScopes(),
|
||||
];
|
||||
|
||||
return new Response(json_encode($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->getAccessToken()->hasScope('email')) {
|
||||
$user['email'] = $result['email'];
|
||||
}
|
||||
|
||||
if ($server->getAccessToken()->hasScope('photo')) {
|
||||
$user['photo'] = $result['photo'];
|
||||
}
|
||||
|
||||
$users[] = $user;
|
||||
}
|
||||
|
||||
return new Response(json_encode($users));
|
||||
});
|
||||
|
||||
// GET /users/{username}
|
||||
$router->get('/users/{username}', function (Request $request, Response $response, array $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->getAccessToken()->hasScope('email')) {
|
||||
$user['email'] = $result[0]['email'];
|
||||
}
|
||||
|
||||
if ($server->getAccessToken()->hasScope('photo')) {
|
||||
$user['photo'] = $result[0]['photo'];
|
||||
}
|
||||
|
||||
return new Response(json_encode($user));
|
||||
});
|
||||
|
||||
$dispatcher = $router->getDispatcher();
|
||||
|
||||
try {
|
||||
// Check that access token is present
|
||||
$server->isValidRequest(false);
|
||||
|
||||
// A successful response
|
||||
$response = $dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getPathInfo()
|
||||
);
|
||||
} catch (\Orno\Http\Exception $e) {
|
||||
// A failed response
|
||||
$response = $e->getJsonResponse();
|
||||
$response->setContent(json_encode(['status_code' => $e->getStatusCode(), 'message' => $e->getMessage()]));
|
||||
} catch (\League\OAuth2\Server\Exception\OAuthException $e) {
|
||||
$response = new Response(json_encode([
|
||||
'error' => $e->errorType,
|
||||
'message' => $e->getMessage(),
|
||||
]), $e->httpStatusCode);
|
||||
|
||||
foreach ($e->getHttpHeaders() as $header) {
|
||||
$response->headers($header);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$response = new Orno\Http\Response();
|
||||
$response->setStatusCode(500);
|
||||
$response->setContent(json_encode(['status_code' => 500, 'message' => $e->getMessage()]));
|
||||
} finally {
|
||||
// Return the response
|
||||
$response->headers->set('Content-type', 'application/json');
|
||||
$response->send();
|
||||
}
|
@ -1,117 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Orno\Http\Request;
|
||||
use Orno\Http\Response;
|
||||
use RelationalExample\Storage;
|
||||
|
||||
include __DIR__.'/vendor/autoload.php';
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request())->createFromGlobals();
|
||||
$router = new \Orno\Route\RouteCollection();
|
||||
$router->setStrategy(\Orno\Route\RouteStrategyInterface::RESTFUL_STRATEGY);
|
||||
|
||||
// Set up the OAuth 2.0 authorization server
|
||||
$server = new \League\OAuth2\Server\AuthorizationServer();
|
||||
$server->setSessionStorage(new Storage\SessionStorage());
|
||||
$server->setAccessTokenStorage(new Storage\AccessTokenStorage());
|
||||
$server->setRefreshTokenStorage(new Storage\RefreshTokenStorage());
|
||||
$server->setClientStorage(new Storage\ClientStorage());
|
||||
$server->setScopeStorage(new Storage\ScopeStorage());
|
||||
$server->setAuthCodeStorage(new Storage\AuthCodeStorage());
|
||||
|
||||
$authCodeGrant = new \League\OAuth2\Server\Grant\AuthCodeGrant();
|
||||
$server->addGrantType($authCodeGrant);
|
||||
|
||||
$refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
|
||||
$server->addGrantType($refrehTokenGrant);
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request())->createFromGlobals();
|
||||
$router = new \Orno\Route\RouteCollection();
|
||||
|
||||
$router->get('/authorize', function (Request $request) use ($server) {
|
||||
|
||||
// First ensure the parameters in the query string are correct
|
||||
|
||||
try {
|
||||
$authParams = $server->getGrantType('authorization_code')->checkAuthorizeParams();
|
||||
} catch (\Exception $e) {
|
||||
return new Response(
|
||||
json_encode([
|
||||
'error' => $e->errorType,
|
||||
'message' => $e->getMessage(),
|
||||
]),
|
||||
$e->httpStatusCode,
|
||||
$e->getHttpHeaders()
|
||||
);
|
||||
}
|
||||
|
||||
// Normally at this point you would show the user a sign-in screen and ask them to authorize the requested scopes
|
||||
|
||||
// ...
|
||||
|
||||
// ...
|
||||
|
||||
// ...
|
||||
|
||||
// Create a new authorize request which will respond with a redirect URI that the user will be redirected to
|
||||
|
||||
$redirectUri = $server->getGrantType('authorization_code')->newAuthorizeRequest('user', 1, $authParams);
|
||||
|
||||
$response = new Response('', 200, [
|
||||
'Location' => $redirectUri
|
||||
]);
|
||||
|
||||
return $response;
|
||||
});
|
||||
|
||||
$router->post('/access_token', function (Request $request) use ($server) {
|
||||
|
||||
try {
|
||||
$response = $server->issueAccessToken();
|
||||
|
||||
return new Response(json_encode($response), 200);
|
||||
} catch (\Exception $e) {
|
||||
return new Response(
|
||||
json_encode([
|
||||
'error' => $e->errorType,
|
||||
'message' => $e->getMessage(),
|
||||
]),
|
||||
$e->httpStatusCode,
|
||||
$e->getHttpHeaders()
|
||||
);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$dispatcher = $router->getDispatcher();
|
||||
|
||||
try {
|
||||
// A successful response
|
||||
$response = $dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getPathInfo()
|
||||
);
|
||||
} catch (\Orno\Http\Exception $e) {
|
||||
// A failed response
|
||||
$response = $e->getJsonResponse();
|
||||
$response->setContent(json_encode(['status_code' => $e->getStatusCode(), 'message' => $e->getMessage()]));
|
||||
} catch (\League\OAuth2\Server\Exception\OAuthException $e) {
|
||||
$response = new Response(json_encode([
|
||||
'error' => $e->errorType,
|
||||
'message' => $e->getMessage(),
|
||||
]), $e->httpStatusCode);
|
||||
|
||||
foreach ($e->getHttpHeaders() as $header) {
|
||||
$response->headers($header);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$response = new Orno\Http\Response();
|
||||
$response->setStatusCode(500);
|
||||
$response->setContent(json_encode(['status_code' => 500, 'message' => $e->getMessage()]));
|
||||
} finally {
|
||||
// Return the response
|
||||
$response->headers->set('Content-type', 'application/json');
|
||||
$response->send();
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
{
|
||||
"require": {
|
||||
"illuminate/database": "4.1.*",
|
||||
"orno/route": "1.*",
|
||||
"ircmaxell/password-compat": "1.0.2",
|
||||
"league/event": "0.2.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"League\\OAuth2\\Server\\": "../../src/",
|
||||
"RelationalExample\\": "."
|
||||
},
|
||||
"files": [
|
||||
"config/db.php"
|
||||
]
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Config;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
include __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
$capsule = new Capsule();
|
||||
|
||||
$capsule->addConnection([
|
||||
'driver' => 'sqlite',
|
||||
'database' => __DIR__.'/oauth2.sqlite3',
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
]);
|
||||
|
||||
$capsule->setAsGlobal();
|
@ -1,249 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace RelationalExample\Config;
|
||||
|
||||
use Illuminate\Database\Capsule\Manager as Capsule;
|
||||
|
||||
include __DIR__.'/../vendor/autoload.php';
|
||||
|
||||
@unlink(__DIR__.'/oauth2.sqlite3');
|
||||
touch(__DIR__.'/oauth2.sqlite3');
|
||||
|
||||
Capsule::statement('PRAGMA foreign_keys = ON');
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
print 'Creating users table'.PHP_EOL;
|
||||
|
||||
Capsule::schema()->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 client redirect uris table'.PHP_EOL;
|
||||
|
||||
Capsule::schema()->create('oauth_client_redirect_uris', function ($table) {
|
||||
$table->increments('id');
|
||||
$table->string('client_id');
|
||||
$table->string('redirect_uri');
|
||||
});
|
||||
|
||||
Capsule::table('oauth_client_redirect_uris')->insert([
|
||||
'client_id' => 'testclient',
|
||||
'redirect_uri' => 'http://example.com/redirect',
|
||||
]);
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
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')->unsigned();
|
||||
$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')->unsigned();
|
||||
$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('expire_time');
|
||||
$table->string('access_token');
|
||||
|
||||
$table->foreign('access_token')->references('access_token')->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')->unsigned();
|
||||
$table->integer('expire_time');
|
||||
$table->string('client_redirect_uri');
|
||||
|
||||
$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')->unsigned();
|
||||
$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')->unsigned();
|
||||
$table->integer('session_id')->unsigned();
|
||||
$table->string('scope');
|
||||
|
||||
$table->foreign('session_id')->references('id')->on('oauth_sessions')->onDelete('cascade');
|
||||
$table->foreign('scope')->references('id')->on('oauth_scopes')->onDelete('cascade');
|
||||
});
|
@ -1,97 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Orno\Http\Request;
|
||||
use Orno\Http\Response;
|
||||
use RelationalExample\Model;
|
||||
use RelationalExample\Storage;
|
||||
|
||||
include __DIR__.'/vendor/autoload.php';
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request())->createFromGlobals();
|
||||
$router = new \Orno\Route\RouteCollection();
|
||||
$router->setStrategy(\Orno\Route\RouteStrategyInterface::RESTFUL_STRATEGY);
|
||||
|
||||
// Set up the OAuth 2.0 authorization server
|
||||
$server = new \League\OAuth2\Server\AuthorizationServer();
|
||||
$server->setSessionStorage(new Storage\SessionStorage());
|
||||
$server->setAccessTokenStorage(new Storage\AccessTokenStorage());
|
||||
$server->setRefreshTokenStorage(new Storage\RefreshTokenStorage());
|
||||
$server->setClientStorage(new Storage\ClientStorage());
|
||||
$server->setScopeStorage(new Storage\ScopeStorage());
|
||||
$server->setAuthCodeStorage(new Storage\AuthCodeStorage());
|
||||
|
||||
$clientCredentials = new \League\OAuth2\Server\Grant\ClientCredentialsGrant();
|
||||
$server->addGrantType($clientCredentials);
|
||||
|
||||
$passwordGrant = new \League\OAuth2\Server\Grant\PasswordGrant();
|
||||
$passwordGrant->setVerifyCredentialsCallback(function ($username, $password) {
|
||||
$result = (new Model\Users())->get($username);
|
||||
if (count($result) !== 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (password_verify($password, $result[0]['password'])) {
|
||||
return $username;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
$server->addGrantType($passwordGrant);
|
||||
|
||||
$refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
|
||||
$server->addGrantType($refrehTokenGrant);
|
||||
|
||||
// Routing setup
|
||||
$request = (new Request())->createFromGlobals();
|
||||
$router = new \Orno\Route\RouteCollection();
|
||||
|
||||
$router->post('/access_token', function (Request $request) use ($server) {
|
||||
|
||||
try {
|
||||
$response = $server->issueAccessToken();
|
||||
|
||||
return new Response(json_encode($response), 200);
|
||||
} catch (\Exception $e) {
|
||||
return new Response(
|
||||
json_encode([
|
||||
'error' => $e->errorType,
|
||||
'message' => $e->getMessage(),
|
||||
]),
|
||||
$e->httpStatusCode,
|
||||
$e->getHttpHeaders()
|
||||
);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
$dispatcher = $router->getDispatcher();
|
||||
|
||||
try {
|
||||
// A successful response
|
||||
$response = $dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getPathInfo()
|
||||
);
|
||||
} catch (\Orno\Http\Exception $e) {
|
||||
// A failed response
|
||||
$response = $e->getJsonResponse();
|
||||
$response->setContent(json_encode(['status_code' => $e->getStatusCode(), 'message' => $e->getMessage()]));
|
||||
} catch (\League\OAuth2\Server\Exception\OAuthException $e) {
|
||||
$response = new Response(json_encode([
|
||||
'error' => $e->errorType,
|
||||
'message' => $e->getMessage(),
|
||||
]), $e->httpStatusCode);
|
||||
|
||||
foreach ($e->getHttpHeaders() as $header) {
|
||||
$response->headers($header);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$response = new Orno\Http\Response();
|
||||
$response->setStatusCode(500);
|
||||
$response->setContent(json_encode(['status_code' => 500, 'message' => $e->getMessage()]));
|
||||
} finally {
|
||||
// Return the response
|
||||
$response->headers->set('Content-type', 'application/json');
|
||||
$response->send();
|
||||
}
|
Loading…
Reference in New Issue
Block a user