mirror of
https://github.com/elyby/oauth2-server.git
synced 2025-05-31 14:12:07 +05:30
First commit of new examples
This commit is contained in:
65
examples/src/Repositories/AccessTokenRepository.php
Normal file
65
examples/src/Repositories/AccessTokenRepository.php
Normal 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.
|
||||
}
|
||||
}
|
51
examples/src/Repositories/ClientRepository.php
Normal file
51
examples/src/Repositories/ClientRepository.php
Normal 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;
|
||||
}
|
||||
}
|
38
examples/src/Repositories/ScopeRepository.php
Normal file
38
examples/src/Repositories/ScopeRepository.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user