oauth2-server/src/AbstractServer.php

336 lines
7.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;
2014-11-08 23:56:12 +05:30
use League\Event\Emitter;
use League\OAuth2\Server\Storage\AccessTokenInterface;
use League\OAuth2\Server\Storage\AuthCodeInterface;
use League\OAuth2\Server\Storage\ClientInterface;
2014-11-08 23:56:12 +05:30
use League\OAuth2\Server\Storage\RefreshTokenInterface;
use League\OAuth2\Server\Storage\ScopeInterface;
use League\OAuth2\Server\Storage\SessionInterface;
use League\OAuth2\Server\TokenType\TokenTypeInterface;
2014-01-10 23:00:12 +05:30
use Symfony\Component\HttpFoundation\Request;
/**
* OAuth 2.0 Resource Server
*/
abstract class AbstractServer
{
/**
* 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;
/**
* Session storage
2014-12-10 18:40:35 +05:30
*
* @var \League\OAuth2\Server\Storage\SessionInterface
2014-01-10 23:00:12 +05:30
*/
protected $sessionStorage;
/**
* Access token storage
2014-12-10 18:40:35 +05:30
*
* @var \League\OAuth2\Server\Storage\AccessTokenInterface
*/
protected $accessTokenStorage;
/**
* Refresh token storage
2014-12-10 18:40:35 +05:30
*
* @var \League\OAuth2\Server\Storage\RefreshTokenInterface
*/
protected $refreshTokenStorage;
/**
* Auth code storage
2014-12-10 18:40:35 +05:30
*
* @var \League\OAuth2\Server\Storage\AuthCodeInterface
*/
protected $authCodeStorage;
/**
* Scope storage
2014-12-10 18:40:35 +05:30
*
* @var \League\OAuth2\Server\Storage\ScopeInterface
*/
protected $scopeStorage;
/**
* Client storage
2014-12-10 18:40:35 +05:30
*
* @var \League\OAuth2\Server\Storage\ClientInterface
*/
protected $clientStorage;
2014-01-10 23:00:12 +05:30
2014-04-23 21:32:50 +05:30
/**
* Token type
2014-12-10 18:40:35 +05:30
*
2014-11-07 06:16:02 +05:30
* @var \League\OAuth2\Server\TokenType\TokenTypeInterface
2014-04-23 21:32:50 +05:30
*/
protected $tokenType;
/**
* Event emitter
2014-12-10 18:40:35 +05:30
*
2014-11-07 06:16:02 +05:30
* @var \League\Event\Emitter
*/
protected $eventEmitter;
/**
* Abstract server constructor
*/
public function __construct()
{
$this->setEventEmitter();
}
/**
* Set an event emitter
2014-12-10 18:40:35 +05:30
*
* @param object $emitter Event emitter object
*/
public function setEventEmitter($emitter = null)
{
if ($emitter === null) {
2014-11-08 23:56:12 +05:30
$this->eventEmitter = new Emitter();
} else {
$this->eventEmitter = $emitter;
}
}
2014-11-07 06:16:02 +05:30
/**
* Add an event listener to the event emitter
2014-12-10 18:40:35 +05:30
*
2014-11-07 06:16:02 +05:30
* @param string $eventName Event name
* @param callable $listener Callable function or method
*/
public function addEventListener($eventName, callable $listener)
{
$this->eventEmitter->addListener($eventName, $listener);
}
2014-11-07 06:16:02 +05:30
/**
* Returns the event emitter
2014-12-10 18:40:35 +05:30
*
2014-11-07 06:16:02 +05:30
* @return \League\Event\Emitter
*/
public function getEventEmitter()
{
return $this->eventEmitter;
}
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
*/
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
*/
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;
}
/**
* Set the client storage
2014-12-10 18:40:35 +05:30
*
* @param \League\OAuth2\Server\Storage\ClientInterface $storage
*
* @return self
2014-01-10 23:00:12 +05:30
*/
public function setClientStorage(ClientInterface $storage)
2014-01-10 23:00:12 +05:30
{
$storage->setServer($this);
$this->clientStorage = $storage;
return $this;
}
/**
* Set the session storage
2014-12-10 18:40:35 +05:30
*
* @param \League\OAuth2\Server\Storage\SessionInterface $storage
*
* @return self
*/
public function setSessionStorage(SessionInterface $storage)
{
$storage->setServer($this);
$this->sessionStorage = $storage;
return $this;
}
/**
* Set the access token storage
2014-12-10 18:40:35 +05:30
*
* @param \League\OAuth2\Server\Storage\AccessTokenInterface $storage
*
* @return self
*/
public function setAccessTokenStorage(AccessTokenInterface $storage)
{
$storage->setServer($this);
$this->accessTokenStorage = $storage;
return $this;
}
/**
* Set the refresh token storage
2014-12-10 18:40:35 +05:30
*
* @param \League\OAuth2\Server\Storage\RefreshTokenInterface $storage
*
* @return self
*/
public function setRefreshTokenStorage(RefreshTokenInterface $storage)
{
$storage->setServer($this);
$this->refreshTokenStorage = $storage;
return $this;
}
2014-05-03 15:23:43 +05:30
/**
* Set the auth code storage
2014-12-10 18:40:35 +05:30
*
* @param \League\OAuth2\Server\Storage\AuthCodeInterface $storage
*
* @return self
*/
public function setAuthCodeStorage(AuthCodeInterface $storage)
{
$storage->setServer($this);
$this->authCodeStorage = $storage;
return $this;
}
/**
* Set the scope storage
2014-12-10 18:40:35 +05:30
*
* @param \League\OAuth2\Server\Storage\ScopeInterface $storage
*
* @return self
*/
public function setScopeStorage(ScopeInterface $storage)
{
$storage->setServer($this);
$this->scopeStorage = $storage;
return $this;
}
/**
* Return the client storage
2014-12-10 18:40:35 +05:30
*
* @return \League\OAuth2\Server\Storage\ClientInterface
*/
public function getClientStorage()
{
return $this->clientStorage;
}
/**
* Return the scope storage
2014-12-10 18:40:35 +05:30
*
* @return \League\OAuth2\Server\Storage\ScopeInterface
*/
public function getScopeStorage()
{
return $this->scopeStorage;
}
/**
* Return the session storage
2014-12-10 18:40:35 +05:30
*
* @return \League\OAuth2\Server\Storage\SessionInterface
*/
public function getSessionStorage()
{
return $this->sessionStorage;
}
/**
* Return the refresh token storage
2014-12-10 18:40:35 +05:30
*
* @return \League\OAuth2\Server\Storage\RefreshTokenInterface
*/
public function getRefreshTokenStorage()
{
return $this->refreshTokenStorage;
}
/**
* Return the access token storage
2014-12-10 18:40:35 +05:30
*
* @return \League\OAuth2\Server\Storage\AccessTokenInterface
*/
public function getAccessTokenStorage()
{
return $this->accessTokenStorage;
}
/**
* Return the auth code storage
2014-12-10 18:40:35 +05:30
*
* @return \League\OAuth2\Server\Storage\AuthCodeInterface
*/
public function getAuthCodeStorage()
{
return $this->authCodeStorage;
2014-01-10 23:00:12 +05:30
}
2014-04-23 21:32:50 +05:30
/**
* Set the access token type
2014-12-10 18:40:35 +05:30
*
* @param TokenTypeInterface $tokenType The token type
*
2014-04-23 21:32:50 +05:30
* @return void
*/
2014-09-09 18:06:20 +05:30
public function setTokenType(TokenTypeInterface $tokenType)
2014-04-23 21:32:50 +05:30
{
2014-10-01 02:56:34 +05:30
$tokenType->setServer($this);
2014-04-23 21:32:50 +05:30
$this->tokenType = $tokenType;
}
/**
* Get the access token type
2014-12-10 18:40:35 +05:30
*
2014-04-23 21:32:50 +05:30
* @return TokenTypeInterface
*/
2014-07-11 22:57:03 +05:30
public function getTokenType()
2014-04-23 21:32:50 +05:30
{
return $this->tokenType;
}
2014-05-03 15:23:43 +05:30
}