Skip to content

Commit

Permalink
Added support for Refresh token and updated the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Miha Vrhovnik committed Mar 22, 2012
1 parent 3bdef5e commit 3fa289b
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 18 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getConfigTreeBuilder()
->scalarNode('user_provider_service')->end()
->scalarNode('oauth_client_class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth_access_token_class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth_refresh_token_class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth_auth_code_class')->isRequired()->cannotBeEmpty()->end()
->arrayNode('oauth_options')
->useAttributeAsKey('key')
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/FOSOAuthServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public function load(array $configs, ContainerBuilder $container)
if (isset($config['user_provider_service'])) {
$container
->getDefinition('fos_oauth_server.server_service.storage.default')
->replaceArgument(3, new Reference($config['user_provider_service']))
->replaceArgument(4, new Reference($config['user_provider_service']))
;
}

$container->setParameter('fos_oauth_server.model.client.class', $config['oauth_client_class']);
$container->setParameter('fos_oauth_server.model.access.token.class', $config['oauth_access_token_class']);
$container->setParameter('fos_oauth_server.model.refresh.token.class', $config['oauth_refresh_token_class']);
$container->setParameter('fos_oauth_server.model.auth.code.class', $config['oauth_auth_code_class']);
$container->setParameter('fos_oauth_server.server_service.options', $config['oauth_options']);
}
Expand Down
18 changes: 18 additions & 0 deletions Document/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Document;

use FOS\OAuthServerBundle\Model\RefreshToken as BaseRefreshToken;

class RefreshToken extends BaseRefreshToken
{
}
18 changes: 18 additions & 0 deletions Document/RefreshTokenManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Document;

use FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface;

class RefreshTokenManager extends TokenManager implements RefreshTokenManagerInterface
{
}
18 changes: 18 additions & 0 deletions Entity/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Entity;

use FOS\OAuthServerBundle\Model\RefreshToken as BaseRefreshToken;

class RefreshToken extends BaseRefreshToken
{
}
18 changes: 18 additions & 0 deletions Entity/RefreshTokenManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Entity;

use FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface;

class RefreshTokenManager extends TokenManager implements RefreshTokenManagerInterface
{
}
16 changes: 16 additions & 0 deletions Model/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Model;

class RefreshToken extends Token implements RefreshTokenInterface
{
}
18 changes: 18 additions & 0 deletions Model/RefreshTokenInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Model;

use OAuth2\Model\IOAuth2RefreshToken;

interface RefreshTokenInterface extends TokenInterface, IOAuth2RefreshToken
{
}
16 changes: 16 additions & 0 deletions Model/RefreshTokenManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the FOSOAuthServerBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\OAuthServerBundle\Model;

interface RefreshTokenManagerInterface extends TokenManagerInterface
{
}
45 changes: 32 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ This bundle needs to persist some classes to a database:

- `Client` (OAuth2 consumers)
- `AccessToken`
- `RefreshToken`
- `AuthCode`

Your first job, then, is to create these classes for your application.
Expand Down Expand Up @@ -148,7 +149,6 @@ class Client extends BaseClient
}
```


``` php
<?php
// src/Acme/ApiBundle/Entity/AccessToken.php
Expand Down Expand Up @@ -176,11 +176,36 @@ class AccessToken extends BaseAccessToken
*/
protected $client;

public function __construct()
{
parent::__construct();
// your own logic
}
}
```

``` php
<?php
// src/Acme/ApiBundle/Entity/AccessToken.php

namespace Acme\ApiBundle\Entity;

use FOS\OAuthServerBundle\Entity\RefreshToken as BaseRefreshToken;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class RefreshToken extends BaseRefreshToken
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\ManyToOne(targetEntity="Client")
* @ORM\JoinColumn(nullable=false)
*/
protected $client;

}
```

Expand Down Expand Up @@ -211,11 +236,6 @@ class AuthCode extends BaseAuthCode
*/
protected $client;

public function __construct()
{
parent::__construct();
// your own logic
}
}
```

Expand Down Expand Up @@ -262,6 +282,7 @@ fos_oauth_server:
db_driver: orm
oauth_client_class: Acme\ApiBundle\Entity\Client
oauth_access_token_class: Acme\ApiBundle\Entity\AccessToken
oauth_refresh_token_class: Acme\ApiBundle\Entity\RefreshToken
oauth_auth_code_class: Acme\ApiBundle\Entity\AuthCode
```

Expand Down Expand Up @@ -299,8 +320,6 @@ if ($form->isValid()) {
## TODO

- More tests
- Add model classes for OAuth2RefreshToken
- Add methods for refresh_token authorization types in the default storage adapter
- Add a default controler for the /authorize endpoint

## Credits
Expand Down
12 changes: 12 additions & 0 deletions Resources/config/doctrine/RefreshToken.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<mapped-superclass name="FOS\OAuthServerBundle\Entity\RefreshToken">
<field name="token" column="token" type="string" />
<field name="expiresAt" column="expires_at" type="integer" nullable="true" />
<field name="scope" column="scope" type="string" nullable="true" />
</mapped-superclass>
</doctrine-mapping>
5 changes: 5 additions & 0 deletions Resources/config/odm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<argument>%fos_oauth_server.model.auth.code.class%</argument>
</service>

<service id="fos_oauth_server.refresh.token.manager.default" class="%fos_oauth_server.model.refresh.token.manager.class%">
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
<argument>%fos_oauth_server.model.refresh.token.class%</argument>
</service>
</services>
</container>
6 changes: 6 additions & 0 deletions Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<parameters>
<parameter key="fos_oauth_server.model.client.manager.class">FOS\OAuthServerBundle\Entity\ClientManager</parameter>
<parameter key="fos_oauth_server.model.access.token.manager.class">FOS\OAuthServerBundle\Entity\AccessTokenManager</parameter>
<parameter key="fos_oauth_server.model.refresh.token.manager.class">FOS\OAuthServerBundle\Entity\RefreshTokenManager</parameter>
<parameter key="fos_oauth_server.model.auth.code.manager.class">FOS\OAuthServerBundle\Entity\AuthCodeManager</parameter>
</parameters>

Expand All @@ -21,6 +22,11 @@
<argument>%fos_oauth_server.model.access.token.class%</argument>
</service>

<service id="fos_oauth_server.refresh.token.manager.default" class="%fos_oauth_server.model.refresh.token.manager.class%">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument>%fos_oauth_server.model.refresh.token.class%</argument>
</service>

<service id="fos_oauth_server.auth.code.manager.default" class="%fos_oauth_server.model.auth.code.manager.class%">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument>%fos_oauth_server.model.auth.code.class%</argument>
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<service id="fos_oauth_server.server_service.storage.default" class="%fos_oauth_server.server_service.storage.class%" public="false">
<argument type="service" id="fos_oauth_server.client.manager" />
<argument type="service" id="fos_oauth_server.access.token.manager" />
<argument type="service" id="fos_oauth_server.refresh.token.manager" />
<argument type="service" id="fos_oauth_server.auth.code.manager" />
<argument>null</argument>
<argument type="service" id="security.encoder_factory" />
Expand Down Expand Up @@ -57,6 +58,7 @@
<!-- Model -->
<service id="fos_oauth_server.client.manager" alias="fos_oauth_server.client.manager.default" />
<service id="fos_oauth_server.access.token.manager" alias="fos_oauth_server.access.token.manager.default" />
<service id="fos_oauth_server.refresh.token.manager" alias="fos_oauth_server.refresh.token.manager.default" />
<service id="fos_oauth_server.auth.code.manager" alias="fos_oauth_server.auth.code.manager.default" />
</services>
</container>
54 changes: 51 additions & 3 deletions Storage/OAuthStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
namespace FOS\OAuthServerBundle\Storage;

use FOS\OAuthServerBundle\Model\AccessTokenManagerInterface;
use FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface;
use FOS\OAuthServerBundle\Model\AuthCodeManagerInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use FOS\OAuthServerBundle\Model\ClientInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use OAuth2\IOAuth2Storage;
use OAuth2\IOAuth2RefreshTokens;
use OAuth2\IOAuth2GrantUser;
use OAuth2\IOAuth2GrantCode;
use OAuth2\Model\IOAuth2Client;

class OAuthStorage implements IOAuth2Storage, IOAuth2GrantUser, IOAuth2GrantCode
class OAuthStorage implements IOAuth2RefreshTokens, IOAuth2GrantUser, IOAuth2GrantCode
{
/**
* @var \FOS\OAuthServerBundle\Model\ClientManagerInterface
Expand All @@ -35,6 +36,11 @@ class OAuthStorage implements IOAuth2Storage, IOAuth2GrantUser, IOAuth2GrantCode
*/
protected $accessTokenManager;

/**
* @var \FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface
*/
protected $refreshTokenManager;

/**
* @var \FOS\OAuthServerBundle\Model\AuthCodeManagerInterface;
*/
Expand All @@ -53,15 +59,17 @@ class OAuthStorage implements IOAuth2Storage, IOAuth2GrantUser, IOAuth2GrantCode
/**
* @param \FOS\OAuthServerBundle\Model\ClientManagerInterface $clientManager
* @param \FOS\OAuthServerBundle\Model\AccessTokenManagerInterface $accessTokenManager
* @param \FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface $refreshTokenManager
* @param \FOS\OAuthServerBundle\Model\AuthCodeManagerInterface $authCodeManager
* @param null|\Symfony\Component\Security\Core\User\UserProviderInterface $userProvider
* @param null|\Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface $encoderFactory
*/
public function __construct(ClientManagerInterface $clientManager, AccessTokenManagerInterface $accessTokenManager,
AuthCodeManagerInterface $authCodeManager, UserProviderInterface $userProvider = null, EncoderFactoryInterface $encoderFactory = null)
RefreshTokenManagerInterface $refreshTokenManager, AuthCodeManagerInterface $authCodeManager, UserProviderInterface $userProvider = null, EncoderFactoryInterface $encoderFactory = null)
{
$this->clientManager = $clientManager;
$this->accessTokenManager = $accessTokenManager;
$this->refreshTokenManager = $refreshTokenManager;
$this->authCodeManager = $authCodeManager;
$this->userProvider = $userProvider;
$this->encoderFactory = $encoderFactory;
Expand Down Expand Up @@ -167,4 +175,44 @@ public function createAuthCode($code, IOAuth2Client $client, $data, $redirect_ur

return $authCode;
}

/**
* {@inheritdoc}
*/
public function getRefreshToken($tokenString)
{
return $this->refreshTokenManager->findTokenByToken($tokenString);
}

/**
* {@inheritdoc}
*/
public function createRefreshToken($tokenString, IOAuth2Client $client, $data, $expires, $scope = NULL)
{
if (!$client instanceof ClientInterface) {
throw new \InvalidArgumentException;
}

$token = $this->refreshTokenManager->createToken();
$token->setToken($tokenString);
$token->setClient($client);
$token->setData($data);
$token->setExpiresAt($expires);
$token->setScope($scope);
$this->refreshTokenManager->updateToken($token);

return $token;
}

/**
* {@inheritdoc}
*/
public function unsetRefreshToken($tokenString)
{
$token = $this->refreshTokenManager->findTokenByToken($tokenString);

if (null !== $token) {
$this->refreshTokenManager->deleteToken($token);
}
}
}
Loading

0 comments on commit 3fa289b

Please sign in to comment.