mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-22 21:19:46 +05:30
Updated password grant example
This commit is contained in:
parent
e7e4892408
commit
bb17abfe26
@ -1,34 +1,50 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use League\OAuth2\Server\Exception\OAuthException;
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
||||||
|
use League\OAuth2\Server\Grant\PasswordGrant;
|
||||||
use League\OAuth2\Server\Server;
|
use League\OAuth2\Server\Server;
|
||||||
|
|
||||||
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
|
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
|
||||||
use OAuth2ServerExamples\Repositories\ClientRepository;
|
use OAuth2ServerExamples\Repositories\ClientRepository;
|
||||||
use OAuth2ServerExamples\Repositories\ScopeRepository;
|
use OAuth2ServerExamples\Repositories\ScopeRepository;
|
||||||
use OAuth2ServerExamples\Repositories\UserRepository;
|
use OAuth2ServerExamples\Repositories\UserRepository;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
|
||||||
|
use Slim\App;
|
||||||
|
use Slim\Http\Request;
|
||||||
|
use Slim\Http\Response;
|
||||||
|
|
||||||
include(__DIR__ . '/../vendor/autoload.php');
|
include(__DIR__ . '/../vendor/autoload.php');
|
||||||
|
|
||||||
// Setup the authorization server
|
// Setup the authorization server
|
||||||
$server = new Server();
|
$server = new Server();
|
||||||
$server->addRepository(new ClientRepository());
|
|
||||||
$server->addRepository(new ScopeRepository());
|
|
||||||
$server->addRepository(new AccessTokenRepository());
|
|
||||||
$server->addRepository(new UserRepository());
|
|
||||||
|
|
||||||
// Enable the password grant
|
// Init our repositories
|
||||||
$server->enableGrantType('PasswordGrant');
|
$clientRepository = new ClientRepository();
|
||||||
|
$scopeRepository = new ScopeRepository();
|
||||||
|
$accessTokenRepository = new AccessTokenRepository();
|
||||||
|
$userRepository = new UserRepository();
|
||||||
|
|
||||||
// Setup app + routing
|
// Enable the client credentials grant on the server
|
||||||
$application = new \Proton\Application();
|
$server->enableGrantType(new PasswordGrant(
|
||||||
$application->post('/access_token', function (Request $request) use ($server) {
|
$userRepository,
|
||||||
|
$clientRepository,
|
||||||
|
$scopeRepository,
|
||||||
|
$accessTokenRepository
|
||||||
|
));
|
||||||
|
|
||||||
|
// App
|
||||||
|
$app = new App([Server::class => $server]);
|
||||||
|
|
||||||
|
$app->post('/access_token', function (Request $request, Response $response) {
|
||||||
|
/** @var Server $server */
|
||||||
|
$server = $this->getContainer()->get(Server::class);
|
||||||
try {
|
try {
|
||||||
return $server->getAccessTokenResponse($request);
|
return $server->respondToRequest($request);
|
||||||
} catch (OAuthException $e) {
|
} catch (OAuthServerException $e) {
|
||||||
return $e->generateHttpResponse();
|
return $e->generateHttpResponse();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $response->withStatus(500)->write($e->getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Run the app
|
$app->run();
|
||||||
$application->run();
|
|
||||||
|
@ -1,22 +1,25 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace OAuth2ServerExamples\Repositories;
|
namespace OAuth2ServerExamples\Repositories;
|
||||||
|
|
||||||
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
|
|
||||||
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
|
||||||
use OAuth2ServerExamples\Entities\UserEntity;
|
use OAuth2ServerExamples\Entities\UserEntity;
|
||||||
|
|
||||||
class UserRepository implements UserRepositoryInterface
|
class UserRepository implements UserRepositoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Get a user
|
* Get a user entity
|
||||||
*
|
*
|
||||||
* @param string $username
|
* @param string $username
|
||||||
* @param string $password
|
* @param string $password
|
||||||
*
|
*
|
||||||
* @return UserEntityInterface
|
* @return \League\OAuth2\Server\Entities\Interfaces\UserEntityInterface
|
||||||
*/
|
*/
|
||||||
public function getByCredentials($username, $password)
|
public function getUserEntityByUserCredentials($username, $password)
|
||||||
{
|
{
|
||||||
|
if ($username === 'alex' && $password === 'whisky') {
|
||||||
return new UserEntity();
|
return new UserEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user