2014-03-09 20:03:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OAuth 2.0 Auth code entity
|
|
|
|
*
|
|
|
|
* @package league/oauth2-server
|
|
|
|
* @author Alex Bilbie <hello@alexbilbie.com>
|
|
|
|
* @copyright Copyright (c) Alex Bilbie
|
|
|
|
* @license http://mit-license.org/
|
2014-03-09 20:05:38 +00:00
|
|
|
* @link https://github.com/thephpleague/oauth2-server
|
2014-03-09 20:03:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace League\OAuth2\Server\Entity;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access token entity class
|
|
|
|
*/
|
2014-05-02 15:12:00 +01:00
|
|
|
class AuthCodeEntity extends AbstractTokenEntity
|
2014-03-09 20:03:05 +00:00
|
|
|
{
|
2014-04-06 19:14:46 +01:00
|
|
|
/**
|
|
|
|
* Redirect URI
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $redirectUri = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the redirect URI for the authorization request
|
2014-05-03 10:53:43 +01:00
|
|
|
* @param string $redirectUri
|
|
|
|
* @return self
|
2014-04-06 19:14:46 +01:00
|
|
|
*/
|
|
|
|
public function setRedirectUri($redirectUri)
|
|
|
|
{
|
|
|
|
$this->redirectUri = $redirectUri;
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2014-04-06 19:14:46 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the redirect URI
|
2014-05-03 10:53:43 +01:00
|
|
|
* @return string
|
2014-04-06 19:14:46 +01:00
|
|
|
*/
|
|
|
|
public function getRedirectUri()
|
|
|
|
{
|
|
|
|
return $this->redirectUri;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-02 15:12:00 +01:00
|
|
|
* Generate a redirect URI
|
2014-04-06 19:14:46 +01:00
|
|
|
* @param string $state The state parameter if set by the client
|
|
|
|
* @param string $queryDelimeter The query delimiter ('?' for auth code grant, '#' for implicit grant)
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generateRedirectUri($state = null, $queryDelimeter = '?')
|
|
|
|
{
|
|
|
|
$uri = $this->getRedirectUri();
|
|
|
|
$uri .= (strstr($this->getRedirectUri(), $queryDelimeter) === false) ? $queryDelimeter : '&';
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2014-04-06 19:14:46 +01:00
|
|
|
return $uri.http_build_query([
|
|
|
|
'code' => $this->getToken(),
|
|
|
|
'state' => $state
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2014-03-09 20:03:05 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getSession()
|
|
|
|
{
|
2014-05-02 15:12:00 +01:00
|
|
|
if ($this->session instanceof SessionEntity) {
|
2014-03-09 20:03:05 +00:00
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
2014-05-02 15:12:00 +01:00
|
|
|
$this->session = $this->server->getStorage('session')->getByAuthCode($this);
|
2014-05-03 10:53:43 +01:00
|
|
|
|
2014-03-09 20:03:05 +00:00
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getScopes()
|
|
|
|
{
|
|
|
|
if ($this->scopes === null) {
|
|
|
|
$this->scopes = $this->formatScopes(
|
2014-05-03 11:40:39 +01:00
|
|
|
$this->server->getStorage('auth_code')->getScopes($this)
|
2014-03-09 20:03:05 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->scopes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
$this->server->getStorage('auth_code')->create(
|
|
|
|
$this->getToken(),
|
|
|
|
$this->getExpireTime(),
|
|
|
|
$this->getSession()->getId()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Associate the scope with the token
|
|
|
|
foreach ($this->getScopes() as $scope) {
|
2014-05-03 11:40:39 +01:00
|
|
|
$this->server->getStorage('auth_code')->associateScope($this, $scope);
|
2014-03-09 20:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function expire()
|
|
|
|
{
|
2014-05-03 11:40:39 +01:00
|
|
|
$this->server->getStorage('auth_code')->delete($this);
|
2014-03-09 20:03:05 +00:00
|
|
|
}
|
|
|
|
}
|