First commit of new examples

This commit is contained in:
Alex Bilbie 2015-04-05 17:02:43 +01:00
parent f1da0d2943
commit 164de644e9
7 changed files with 1263 additions and 0 deletions

3
examples/README.md Normal file
View File

@ -0,0 +1,3 @@
# Example implementations
Just run `composer install --no-dev` in this directory to get started.

View File

@ -0,0 +1,23 @@
<?php
use Symfony\Component\HttpFoundation\Request;
include (__DIR__.'/../vendor/autoload.php');
// Setup the authorization server
$server = new \League\OAuth2\Server\Server();
$server->addRepository(new \OAuth2ServerExamples\Repositories\ClientRepository());
$server->addRepository(new \OAuth2ServerExamples\Repositories\ScopeRepository());
$server->addRepository(new \OAuth2ServerExamples\Repositories\AccessTokenRepository());
// Enable the client credentials grant which will return access tokens that last for 24 hours
$server->enableGrantType('ClientCredentialsGrant', null, new \DateInterval('PT24H'));
// Setup the routing
$application = new \Proton\Application();
$application->post('/access_token', function (Request $request) use ($server) {
return $server->getAccessTokenResponse($request);
});
// Run the app
$application->run();

13
examples/composer.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "",
"require": {
"alexbilbie/proton": "~1.4",
"illuminate/database": "~5.0"
},
"autoload": {
"psr-4": {
"League\\OAuth2\\Server\\": "../src/",
"OAuth2ServerExamples\\": "src/"
}
}
}

1070
examples/composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,65 @@
<?php
namespace OAuth2ServerExamples\Repositories;
use League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\Interfaces\ScopeEntityInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
class AccessTokenRepository implements AccessTokenRepositoryInterface
{
/**
* Get an instance of Entity\AccessTokenEntity
*
* @param string $tokenIdentifier The access token identifier
*
* @return \League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface
*/
public function get($tokenIdentifier)
{
// TODO: Implement get() method.
}
/**
* Get the scopes for an access token
*
* @param \League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface $token
*
* @return \League\OAuth2\Server\Entities\Interfaces\ScopeEntityInterface[]
*/
public function getScopes(AccessTokenEntityInterface $token)
{
// TODO: Implement getScopes() method.
}
/**
* Creates a new access token
*
* @param \League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface $accessTokenEntity
*/
public function create(AccessTokenEntityInterface $accessTokenEntity)
{
// TODO: Implement create() method.
}
/**
* Associate a scope with an access token
*
* @param \League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface $accessTokenEntityInterface
* @param \League\OAuth2\Server\Entities\Interfaces\ScopeEntityInterface $scope
*/
public function associateScope(AccessTokenEntityInterface $accessTokenEntityInterface, ScopeEntityInterface $scope)
{
// TODO: Implement associateScope() method.
}
/**
* Delete an access token
*
* @param \League\OAuth2\Server\Entities\Interfaces\AccessTokenEntityInterface $accessToken
*/
public function delete(AccessTokenEntityInterface $accessToken)
{
// TODO: Implement delete() method.
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace OAuth2ServerExamples\Repositories;
use League\OAuth2\Server\Entities\ClientEntity;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
class ClientRepository implements ClientRepositoryInterface
{
/**
* Get a client
*
* @param string $clientIdentifier The client's identifier
* @param string $clientSecret The client's secret (default = "null")
* @param string $redirectUri The client's redirect URI (default = "null")
* @param string $grantType The grant type used (default = "null")
*
* @return \League\OAuth2\Server\Entities\Interfaces\ClientEntityInterface
*/
public function get($clientIdentifier, $clientSecret = null, $redirectUri = null, $grantType = null)
{
$clients = [
'myawesomeapp' => [
'secret' => password_hash('abc123', PASSWORD_BCRYPT),
'name' => 'My Awesome App',
'redirect_uri' => ''
]
];
// Check if client is registered
if (array_key_exists($clientIdentifier, $clients) === false) {
return null;
}
// Check if client secret is valid
if ($clientSecret !== null && password_verify($clientSecret, $clients[$clientIdentifier]['secret']) === false) {
return null;
}
// Check if redirect URI is valid
if ($redirectUri !== null && $redirectUri !== $clients[$clientIdentifier]['redirectUri']) {
return null;
}
$client = new ClientEntity();
$client->setIdentifier($clientIdentifier);
$client->setName($clients[$clientIdentifier]['name']);
$client->setSecret($clients[$clientIdentifier]['secret']);
return $client;
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace OAuth2ServerExamples\Repositories;
use League\OAuth2\Server\Entities\ScopeEntity;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
class ScopeRepository implements ScopeRepositoryInterface
{
/**
* Return information about a scope
*
* @param string $scopeIdentifier The scope identifier
* @param string $grantType The grant type used in the request (default = "null")
* @param string $clientId The client sending the request (default = "null")
*
* @return \League\OAuth2\Server\Entities\Interfaces\ScopeEntityInterface
*/
public function get($scopeIdentifier, $grantType = null, $clientId = null)
{
$scopes = [
'basic' => [
'description' => 'Basic details about you'
],
'email' => [
'description' => 'Your email address'
]
];
if (array_key_exists($scopeIdentifier, $scopes) === false) {
return null;
}
$scope = new ScopeEntity();
$scope->setIdentifier($scopeIdentifier);
return $scope;
}
}