Working on that OAuth2
This commit is contained in:
parent
a834692235
commit
2ec85c469b
10 changed files with 266 additions and 101 deletions
55
src/OAuth2/Resource.php
Normal file
55
src/OAuth2/Resource.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Pickles\OAuth2;
|
||||
|
||||
use \League\OAuth2\Server\AuthorizationServer;
|
||||
use \League\OAuth2\Server\Grant\PasswordGrant;
|
||||
use \Pickles\App\Model\User;
|
||||
|
||||
class Resource extends \Pickles\Resource
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
switch ($_REQUEST['request'])
|
||||
{
|
||||
case 'oauth/access_token':
|
||||
try
|
||||
{
|
||||
$server = new AuthorizationServer;
|
||||
|
||||
$server->setSessionStorage(new SessionStorage);
|
||||
$server->setAccessTokenStorage(new AccessTokenStorage);
|
||||
$server->setClientStorage(new ClientStorage);
|
||||
$server->setScopeStorage(new ScopeStorage);
|
||||
|
||||
$passwordGrant = new PasswordGrant;
|
||||
$passwordGrant->setVerifyCredentialsCallback(function ($username, $password)
|
||||
{
|
||||
$user = new User(['email' => $username]);
|
||||
|
||||
return $user->count()
|
||||
&& password_verify($password, $user->record['password']);
|
||||
});
|
||||
|
||||
$server->addGrantType($passwordGrant);
|
||||
|
||||
// @todo Add grant types listed in the config. Password is always added
|
||||
|
||||
$response = $server->issueAccessToken();
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
// @todo Set error code's accordingly.
|
||||
|
||||
throw new \Exception($e->getMessage(), $e->httpStatusCode);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('Not Found.', 404);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue