oauth2-server/src/AbstractServer.php

113 lines
3.2 KiB
PHP
Raw Normal View History

2014-01-10 23:00:12 +05:30
<?php
/**
* OAuth 2.0 Abstract Server
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
2014-03-10 01:04:23 +05:30
* @copyright Copyright (c) Alex Bilbie
2014-01-10 23:00:12 +05:30
* @license http://mit-license.org/
2014-03-10 01:35:38 +05:30
* @link https://github.com/thephpleague/oauth2-server
2014-01-10 23:00:12 +05:30
*/
namespace League\OAuth2\Server;
2015-04-05 21:33:13 +05:30
use League\Container\Container;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use League\Event\EmitterAwareInterface;
use League\Event\EmitterTrait;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
2014-01-10 23:00:12 +05:30
use Symfony\Component\HttpFoundation\Request;
/**
* OAuth 2.0 Resource Server
*/
2015-04-05 21:33:13 +05:30
abstract class AbstractServer implements ContainerAwareInterface, EmitterAwareInterface
2014-01-10 23:00:12 +05:30
{
2015-04-05 21:33:13 +05:30
use EmitterTrait, ContainerAwareTrait;
2014-01-10 23:00:12 +05:30
/**
* The request object
*
2014-11-07 06:16:02 +05:30
* @var \Symfony\Component\HttpFoundation\Request
2014-01-10 23:00:12 +05:30
*/
protected $request;
/**
2015-04-05 21:33:13 +05:30
* Setup the server
*/
public function __construct()
{
2015-04-05 21:33:13 +05:30
$this->setContainer(new Container());
$this->getContainer()->singleton('emitter', $this->getEmitter());
$this->getContainer()->addServiceProvider('League\OAuth2\Server\ServiceProviders\ClientCredentialsGrantServerProvider');
}
2014-01-10 23:00:12 +05:30
/**
* Sets the Request Object
2014-12-10 18:40:35 +05:30
*
2014-01-10 23:00:12 +05:30
* @param \Symfony\Component\HttpFoundation\Request The Request Object
2014-12-10 18:40:35 +05:30
*
2014-01-10 23:00:12 +05:30
* @return self
2015-04-05 21:33:13 +05:30
* @deprecated
2014-01-10 23:00:12 +05:30
*/
2014-09-22 11:26:15 +05:30
public function setRequest($request)
2014-01-10 23:00:12 +05:30
{
$this->request = $request;
2014-05-03 15:23:43 +05:30
2014-01-10 23:00:12 +05:30
return $this;
}
/**
* Gets the Request object. It will create one from the globals if one is not set.
2014-12-10 18:40:35 +05:30
*
2014-01-10 23:00:12 +05:30
* @return \Symfony\Component\HttpFoundation\Request
2015-04-05 21:33:13 +05:30
* @deprecated
2014-01-10 23:00:12 +05:30
*/
public function getRequest()
{
if ($this->request === null) {
2014-05-02 19:44:25 +05:30
$this->request = Request::createFromGlobals();
2014-01-10 23:00:12 +05:30
}
return $this->request;
}
/**
2015-04-05 21:33:13 +05:30
* Add a repository to the server
2014-12-10 18:40:35 +05:30
*
2015-04-05 21:33:13 +05:30
* @param RepositoryInterface $repository
*/
2015-04-05 21:33:13 +05:30
public function addRepository(RepositoryInterface $repository)
{
2015-04-05 21:33:13 +05:30
switch ($repository) {
case ($repository instanceof AccessTokenRepositoryInterface):
$this->getContainer()->add('AccessTokenRepository', $repository);
break;
case ($repository instanceof ClientRepositoryInterface):
$this->getContainer()->add('ClientRepository', $repository);
break;
case ($repository instanceof ScopeRepositoryInterface):
$this->getContainer()->add('ScopeRepository', $repository);
break;
}
2014-01-10 23:00:12 +05:30
}
2014-04-23 21:32:50 +05:30
/**
2015-04-05 21:33:13 +05:30
* Get a hydrated grant
2014-12-10 18:40:35 +05:30
*
2015-04-05 21:33:13 +05:30
* @param string $grant
* @param \DateInterval $tokenTTL
2014-12-10 18:40:35 +05:30
*
2015-04-05 21:33:13 +05:30
* @return \League\OAuth2\Server\Repositories\RepositoryInterface
* @deprecated
2014-12-28 02:16:46 +05:30
*/
2015-04-05 21:33:13 +05:30
public function getGrant($grant, \DateInterval $tokenTTL)
2014-12-28 02:16:46 +05:30
{
2015-04-05 21:33:13 +05:30
return $this->getContainer()->get($grant, [$this->responseType, $tokenTTL]);
2014-12-28 02:16:46 +05:30
}
2014-05-03 15:23:43 +05:30
}