Getting Started With FOSOAuthServerBundle
=========================================
## Prerequisites
This version of the bundle requires Symfony 2.8.
If you are using Symfony 2.0.x, please use the 1.1.1 release of the bundle (or lower), and follow
[this documentation](https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/1.1.1/README.md).
#### Translations
If you wish to use default texts provided in this bundle, you have to make sure you have translator enabled in your config:
# app/config/config.yml
framework:
translator: { fallback: en }
For more information about translations, check [Symfony documentation](http://symfony.com/doc/current/book/translation.html).
## Installation
Installation is a quick 5 steps process:
1. Download FOSOAuthServerBundle
2. Enable the Bundle
3. Create your model class
4. Configure your application's security.yml
5. Configure the FOSOAuthServerBundle
### Step 1: Install FOSOAuthServerBundle
The preferred way to install this bundle is to rely on [Composer](http://getcomposer.org).
Just check on [Packagist](http://packagist.org/packages/friendsofsymfony/oauth-server-bundle) the version you want to install (in the following example, we used "dev-master") and add it to your `composer.json`:
``` js
{
"require": {
// ...
"friendsofsymfony/oauth-server-bundle": "dev-master"
}
}
```
### Step 2: Enable the bundle
Finally, enable the bundle in the kernel:
``` php
If you override the __construct() method in your classes, be sure
> to call parent::__construct(), as the base class depends on
> this to initialize some fields.
#### Doctrine ORM classes
If you're persisting your data via the Doctrine ORM, then your classes
should live in the `Entity` namespace of your bundle and look like this to
start:
``` php
```
``` php
client;
}
public function setClient(ClientInterface $client)
{
$this->client = $client;
}
}
```
``` xml
```
``` php
client;
}
public function setClient(ClientInterface $client)
{
$this->client = $client;
}
}
```
``` xml
```
``` php
client;
}
public function setClient(ClientInterface $client)
{
$this->client = $client;
}
}
```
``` xml
```
### Step 4: Configure your application's security.yml
In order for Symfony's security component to use the FOSOAuthServerBundle, you must
tell it to do so in the `security.yml` file. The `security.yml` file is where the
basic configuration for the security for your application is contained.
Below is a minimal example of the configuration necessary to use the FOSOAuthServerBundle
in your application:
``` yaml
# app/config/security.yml
security:
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
# Add your favorite authentication process here
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: false # can be omitted as its default value
access_control:
- { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }
```
The URLs under `/api` will use OAuth2 to authenticate users.
#### Anonymous access
Sometimes you need to allow your api to be accessed without authorization. In order to do that lets adjust
above-mentioned example configuration.
``` yaml
# app/config/security.yml
security:
firewalls:
oauth_token:
pattern: ^/oauth/v2/token
security: false
oauth_authorize:
pattern: ^/oauth/v2/auth
# Add your favorite authentication process here
api:
pattern: ^/api
fos_oauth: true
stateless: true
anonymous: true # note that anonymous access is now enabled
# also note absence of "access_control" section
```
From now on all of your api resources can be accessed without authorization. But what if one or more of them should be
secured anyway or/and require presence of authenticated user? It's easy! You can do that manually by adding few lines of
code at the beginning of all of your secured actions like in the example below:
``` php
// [...]
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class YourApiController extends Controller
{
public function getSecureResourceAction()
{
# this is it
if (false === $this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
throw new AccessDeniedException();
}
// [...]
}
```
### Step 5: Configure FOSOAuthServerBundle
Import the routing.yml configuration file in app/config/routing.yml:
``` yaml
# app/config/routing.yml
fos_oauth_server_token:
resource: "@FOSOAuthServerBundle/Resources/config/routing/token.xml"
fos_oauth_server_authorize:
resource: "@FOSOAuthServerBundle/Resources/config/routing/authorize.xml"
```
Add FOSOAuthServerBundle settings in app/config/config.yml:
``` yaml
# app/config/config.yml
fos_oauth_server:
db_driver: orm # Drivers available: orm or mongodb
client_class: Acme\ApiBundle\Entity\Client
access_token_class: Acme\ApiBundle\Entity\AccessToken
refresh_token_class: Acme\ApiBundle\Entity\RefreshToken
auth_code_class: Acme\ApiBundle\Entity\AuthCode
```
If you're authenticating users, don't forget to set the user provider.
Here's an example using the FOSUserBundle user provider:
``` yaml
# app/config/config.yml
fos_oauth_server:
...
service:
user_provider: fos_user.user_provider.username
```
## Creating A Client
### Console Command
The most convenient way to create a client is to use the console command.
$ php app/console fos:oauth-server:create-client --redirect-uri="..." --grant-type="..."
Note: you can use `--redirect-uri` and `--grant-type` multiple times to add additional values.
### Programatically
Before you can generate tokens, you need to create a Client using the ClientManager.
``` php
$clientManager = $this->container->get('fos_oauth_server.client_manager.default');
$client = $clientManager->createClient();
$client->setRedirectUris(array('http://www.example.com'));
$client->setAllowedGrantTypes(array('token', 'authorization_code'));
$clientManager->updateClient($client);
```
Once you have created a client, you need to pass its `publicId` to the authorize endpoint. You also need
to specify a redirect uri as well as a response type.
```php
return $this->redirect($this->generateUrl('fos_oauth_server_authorize', array(
'client_id' => $client->getPublicId(),
'redirect_uri' => 'http://www.example.com',
'response_type' => 'code'
)));
```
## Usage
The `token` endpoint is at `/oauth/v2/token` by default (see `Resources/config/routing/token.xml`).
The `authorize` endpoint is at `/oauth/v2/auth` by default (see `Resources/config/routing/authorize.xml`).
## Next steps
[A Note About Security](a_note_about_security.md)
[Configuration Reference](configuration_reference.md)
[Dealing With Scopes](dealing_with_scopes.md)
[Extending the Authorization page](extending_the_authorization_page.md)
[Extending the Model](extending_the_model.md)
[The OAuthEvent class](the_oauth_event_class.md)
[Adding Grant Extensions](adding_grant_extensions.md)
[Custom DB Driver](custom_db_driver.md)