Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanimir Stoyanov committed May 8, 2016
0 parents commit dd1da15
Show file tree
Hide file tree
Showing 43 changed files with 2,874 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea

20 changes: 20 additions & 0 deletions Components/Oauth2/Entities/AccessTokenEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Entities;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait;
use League\OAuth2\Server\Entities\Traits\EntityTrait;
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait;

class AccessTokenEntity implements AccessTokenEntityInterface
{
use AccessTokenTrait, TokenEntityTrait, EntityTrait;
}
29 changes: 29 additions & 0 deletions Components/Oauth2/Entities/ClientEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Entities;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\Traits\ClientTrait;
use League\OAuth2\Server\Entities\Traits\EntityTrait;

class ClientEntity implements ClientEntityInterface
{
use EntityTrait, ClientTrait;

public function setName($name)
{
$this->name = $name;
}

public function setRedirectUri($uri)
{
$this->redirectUri = $uri;
}
}
19 changes: 19 additions & 0 deletions Components/Oauth2/Entities/RefreshTokenEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Entities;

use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Entities\Traits\EntityTrait;
use League\OAuth2\Server\Entities\Traits\RefreshTokenTrait;

class RefreshTokenEntity implements RefreshTokenEntityInterface
{
use RefreshTokenTrait, EntityTrait;
}
23 changes: 23 additions & 0 deletions Components/Oauth2/Entities/ScopeEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Entities;

use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Entities\Traits\EntityTrait;

class ScopeEntity implements ScopeEntityInterface
{
use EntityTrait;

public function jsonSerialize()
{
return $this->getIdentifier();
}
}
32 changes: 32 additions & 0 deletions Components/Oauth2/Entities/UserEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Entities;

use League\OAuth2\Server\Entities\UserEntityInterface;

class UserEntity implements UserEntityInterface
{
private $user;

public function __construct($userArray)
{
$this->user = $userArray;
}

/**
* Return the user's identifier.
*
* @return mixed
*/
public function getIdentifier()
{
return $this->user['id'];
}
}
25 changes: 25 additions & 0 deletions Components/Oauth2/GenerateResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Phalcon2Rest\Components\Oauth2;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use Psr\Http\Message\ResponseInterface;

class GenerateResult implements \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface {

public function setAccessToken(AccessTokenEntityInterface $accessToken)
{
// TODO: Implement setAccessToken() method.
}

public function setRefreshToken(RefreshTokenEntityInterface $refreshToken)
{
// TODO: Implement setRefreshToken() method.
}

public function generateHttpResponse(ResponseInterface $response)
{
// TODO: Implement generateHttpResponse() method.
}
}
58 changes: 58 additions & 0 deletions Components/Oauth2/Repositories/AccessTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Repositories;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use Phalcon2Rest\Components\Oauth2\Entities\AccessTokenEntity;

class AccessTokenRepository implements AccessTokenRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
{
// Some logic here to save the access token to a database
}

/**
* {@inheritdoc}
*/
public function revokeAccessToken($tokenId)
{
// Some logic here to revoke the access token
}

/**
* {@inheritdoc}
*/
public function isAccessTokenRevoked($tokenId)
{
return false; // Access token hasn't been revoked
}

/**
* {@inheritdoc}
*/
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
{

$accessToken = new AccessTokenEntity();
$accessToken->setClient($clientEntity);
foreach ($scopes as $scope) {
$accessToken->addScope($scope);
}
$accessToken->setUserIdentifier($userIdentifier);

return $accessToken;
}
}
64 changes: 64 additions & 0 deletions Components/Oauth2/Repositories/ClientRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Repositories;

use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use Phalcon\Security;
use Phalcon2Rest\Components\Oauth2\Entities\ClientEntity;
use Phalcon\Di\FactoryDefault as Di;
use Phalcon2Rest\Models\Clients;

class ClientRepository implements ClientRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function getClientEntity($clientIdentifier, $grantType, $clientSecret = null, $mustValidateSecret = true)
{
$di = new Di();
/** @var Security $security */
$security = $di->getShared('security');
$client = Clients::query()
->where("id = :id:")
->bind([
'id' => $clientIdentifier
])
->limit(1)
->execute()
->toArray();
$correctDetails = false;
if (count($client) === 1) {
$client = current($client);
if ($mustValidateSecret) {

if ($security->checkHash($clientSecret, $client['secret'])) {
$correctDetails = true;
} else {
$security->hash(rand());

}
} else {
$correctDetails = true;
}
} else {
// prevent timing attacks
$security->hash(rand());
}

if ($correctDetails) {
$clientEntity = new ClientEntity();
$clientEntity->setIdentifier($clientIdentifier);
$clientEntity->setName($client['name']);
$clientEntity->setRedirectUri($client['redirect_url']);
return $clientEntity;
}
return null;
}
}
49 changes: 49 additions & 0 deletions Components/Oauth2/Repositories/RefreshTokenRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Repositories;

use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use Phalcon2Rest\Components\Oauth2\Entities\RefreshTokenEntity;

class RefreshTokenRepository implements RefreshTokenRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntityInterface)
{
// Some logic to persist the refresh token in a database
}

/**
* {@inheritdoc}
*/
public function revokeRefreshToken($tokenId)
{
// Some logic to revoke the refresh token in a database
}

/**
* {@inheritdoc}
*/
public function isRefreshTokenRevoked($tokenId)
{
return false; // The refresh token has not been revoked
}

/**
* {@inheritdoc}
*/
public function getNewRefreshToken()
{
return new RefreshTokenEntity();
}
}
53 changes: 53 additions & 0 deletions Components/Oauth2/Repositories/ScopeRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* @author Alex Bilbie <[email protected]>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
*
* @link https://github.com/thephpleague/oauth2-server
*/

namespace Phalcon2Rest\Components\Oauth2\Repositories;

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use Phalcon2Rest\Components\Oauth2\Entities\ScopeEntity;

class ScopeRepository implements ScopeRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function getScopeEntityByIdentifier($scopeIdentifier)
{
$scopes = [
'basic' => [
'description' => 'Basic details about you',
],
'email' => [
'description' => 'Your email address',
],
];

if (array_key_exists($scopeIdentifier, $scopes) === false) {
return;
}

$scope = new ScopeEntity();
$scope->setIdentifier($scopeIdentifier);

return $scope;
}

/**
* {@inheritdoc}
*/
public function finalizeScopes(
array $scopes,
$grantType,
ClientEntityInterface $clientEntity,
$userIdentifier = null
) {
return $scopes;
}
}
Loading

0 comments on commit dd1da15

Please sign in to comment.