1

I am trying to configure FOSOAuthServerBundle with FOSUserBundle using the documentation of FOSOAuthServerBundle. This is the config:

config.yml

fos_user:
    db_driver: orm
    firewall_name: oauth_token #main
    user_class: Minn\UserBundle\Entity\User

fos_oauth_server:
    db_driver: orm
    client_class:        Minn\UserBundle\Entity\Client
    access_token_class:  Minn\UserBundle\Entity\AccessToken
    refresh_token_class: Minn\UserBundle\Entity\RefreshToken
    auth_code_class:     Minn\UserBundle\Entity\AuthCode
    service:
        user_provider: fos_user.user_manager # this is added to fos_oauth to use fos_user for authentication
        options:
            supported_scopes: api

security.yml

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_USER : ROLE_API
        ROLE_ADMIN: ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern:      ^/(_(profiler|wdt)|css|images|js)/
            security:     false

        oauth_token:
            pattern:      ^/oauth/v2/token
            security:     false

        api_doc:
            pattern:      ^/api/doc
            security:     false

        api:
            pattern:      ^/api
            fos_oauth:    true
            stateless:    true

    access_control:
        - { path: ^/api, roles: [ IS_AUTHENTICATED_FULLY ] }

A this point, it seems to be working as testing the config with the creation of symfony command worked well.

The testing command:

<?php

namespace Minn\UserBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Acme\OAuthServerBundle\Document\Client;

class CreateOAuthClientCommand extends ContainerAwareCommand {

    protected function configure() {
        $this
                ->setName('oauth:client:create')
                ->setDescription('Creates a new client')
                ->addOption(
                        'redirect-uri', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.', null
                )
                ->addOption(
                        'grant-type', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Sets allowed grant type for client. Use this option multiple times to set multiple grant types..', null
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
        $client = $clientManager->createClient();
        $client->setRedirectUris($input->getOption('redirect-uri'));
        $client->setAllowedGrantTypes($input->getOption('grant-type'));
        $clientManager->updateClient($client);
        $output->writeln(
                sprintf(
                        'Added a new client with public id <info>%s</info>, secret <info>%s</info>', $client->getPublicId(), $client->getSecret()
                )
        );
    }

}
  • Run the following symfony command:

    php app/console oauth:client:create --redirect-uri="http://localhost/minnapi/web/app_dev.php/" --grant-type="authorization_code" --grant-type="password" --grant-type="refresh-token" --grant-type="token" --grant-type="client_credentials"
    
  • the output of the command is:

    Added a new client with public id 5_552osbf54k4c0kow00ko8ww8kkgcwgg4g4okkgc0wcww0ggsw8, secret 10kv0z11wr688o8kws4wg08scs48o4o8o8cg004c44wcgcgc4s
    

    Please be noted that the command created a record in the table client witch is as follows:

    INSERT INTO `minn_client` (`id`, `name`, `random_id`, `redirect_uris`, `secret`, `allowed_grant_types`) VALUES
    (5, NULL, '552osbf54k4c0kow00ko8ww8kkgcwgg4g4okkgc0wcww0ggsw8', 'a:1:{i:0;s:41:"http://localhost/minnapi/web/app_dev.php/";}', '10kv0z11wr688o8kws4wg08scs48o4o8o8cg004c44wcgcgc4s', 'a:5:{i:0;s:18:"authorization_code";i:1;s:8:"password";i:2;s:13:"refresh-token";i:3;s:5:"token";i:4;s:18:"client_credentials";}');
    
  • Execute the following request in your browser

    http://localhost/minnapi/web/app_dev.php/oauth/v2/token?client_id=5_552osbf54k4c0kow00ko8ww8kkgcwgg4g4okkgc0wcww0ggsw8&client_secret=10kv0z11wr688o8kws4wg08scs48o4o8o8cg004c44wcgcgc4s&grant_type=client_credentials
    

    The returned answer of the browser is:

    {"access_token":"Njg5OWUzZmI5Yjg5MWFlYTZkOWNmMWIwNGMwNDNmZDhkZmEwZDhjMDM4OTcyNzZiNzRiMTNiZjBlOGMyMDk0OA","expires_in":3600,"token_type":"bearer","scope":"api"}
    

Now, the problems come when I tried to check it with an action in a controller as proposed by the documentation:

The testing action

/**
 * @Route("/connect")
 */
public function connectAction(){
    // 1. creation of a client (manually)
    $clientManager = $this->get('fos_oauth_server.client_manager.default');
    $client = $clientManager->createClient();
    $client->setRedirectUris(array('http://localhost/minnapi/web/app_dev.php/'));
    $client->setAllowedGrantTypes(array('token', 'authorization_code'));
    $clientManager->updateClient($client);

    return $this->redirect($this->generateUrl('fos_oauth_server_authorize', array(
        'client_id'     => $client->getPublicId(),
        'redirect_uri'  => 'http://localhost/minnapi/web/app_dev.php/',
        'response_type' => 'api'
    )));
}

The error I got is:

Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Call to a member function getUser() on null" at /home/amine/NetBeansProjects/minnapi/vendor/friendsofsymfony/oauth-server-bundle/Controller/AuthorizeController.php line 58 Context: { "exception": "Object(Symfony\Component\Debug\Exception\FatalErrorException)" }

Is there any idea about the error?

Thanks

Amine Jallouli
  • 3,919
  • 8
  • 36
  • 73
  • I feel the problem is coming from the firewall. Does anyone has an idea? – Amine Jallouli Aug 01 '16 at 04:41
  • I have problems with this too. Have you had any success? I can see that your testing action tries to make a call to the auth-endpoint (oauth/v2/auth) whereas the manual call goes to the token-endpoint. – sebastian Mar 02 '17 at 09:47
  • 1
    yes, if you are just needing to get your API secured, then JWT will be more than enough. So, I recommend this example https://github.com/auth0-blog/angularjs-jwt-authentication-tutorial working as a charm... Else, I wish you good luck! – Amine Jallouli Mar 05 '17 at 03:48

0 Answers0