title: FOSOAuthServerBundle and custom authentication provider
body: <p>Edit: Found the error. There is a tiny little peace of code in <code>\vendor\friendsofsymfony\oauth-server-bundle\FOS\OAuthServerBundle\Resources\config\oauth.xml</code> which says:</p>
  
  <pre><code>&lt;argument type="service" id="fos_oauth_server.user_provider" on-invalid="null" /&gt;
  </code></pre>
  
  <p>So when your service definition in config.yml is wrong or just a typo (from the <a href="https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/blob/master/Resources/doc/index.md" rel="nofollow noreferrer">original docs</a>)</p>
  
  <pre><code>If you're authenticating users, don't forget to set the user provider. Here's an example using the FOSUserBundle user provider:
  
  # app/config/config.yml
  fos_oauth_server:
      ...
  
      service:
          user_provider: fos_user.user_manager
  </code></pre>
  
  <p>you are lost. No error, no warning, the User Provider is just null. Thanks for reading this post anyway. I'll discuss that matter on github.</p>
  
  <p>--</p>
  
  <p>I'm building an OAuth2 server with the <a href="https://github.com/FriendsOfSymfony/FOSOAuthServerBundle" rel="nofollow noreferrer">FOSOAuthServerBundle</a>. Everything is up and running, as long as I request an access_token with grant_type=password and use a form_login with a custom UserProvider.</p>
  
  <p>My security.yml looks like this.</p>
  
  <pre><code>security:
      providers:
          webservice:
              id: webservice_user_provider
  
      firewalls:
          dev:
              pattern: ^/(_(profiler|wdt)|css|images|js)/
              security: false
  
          oauth_authorize:
              pattern:    ^/oauth/v2/auth
              form_login:
                  provider: webservice
                  check_path: login_check
                  login_path: login
              anonymous: true
  
          oauth_token:
              pattern:    ^/oauth/v2/token
              security:   false
  
          api:
              pattern:    ^/api
              fos_oauth:  true
              stateless:  true
  
          default:
              anonymous: ~
  
              form_login:
                  provider: webservice
                  login_path: /login
                  check_path: /login_check
  
              logout:
                  path:   /logout
                  target: /
  
          login:
              pattern:  ^/login$
              security: false
  </code></pre>
  
  <p>As the custom user provider wasn't sufficient for my needs, I implemented a custom Authentication provider following <a href="https://blog.vandenbrand.org/2012/06/19/symfony2-authentication-provider-authenticate-against-webservice/" rel="nofollow noreferrer">this Tutorial</a>. This one is also working, when I try to login with a standard login form. I changed security.yml to</p>
  
  <pre><code>    oauth_authorize:
          pattern:    ^/oauth/v2/auth
          webservice-login:
              provider: webservice
              check_path: login_check
              login_path: /login
          anonymous: true
  
          webservice-login:
              check_path: login_check
              login_path: /login
              provider: webservice
  </code></pre>
  
  <p>webservice-login comes from the SecurityFactory</p>
  
  <pre><code>class SecurityFactory extends FormLoginFactory
  {
      public function getKey()
      {
          return 'webservice-login';
      }
  
      protected function getListenerId()
      {
          return 'security.authentication.listener.form';
      }
  
      protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId)
      {
          $provider = 'security.authentication_provider.als_webservice.'.$id;
          $container
              -&gt;setDefinition($provider, new DefinitionDecorator('security.authentication_provider.als_webservice'))
              -&gt;replaceArgument(1, new Reference($userProviderId))
              -&gt;replaceArgument(3, $id)
          ;
  
          return $provider;
      }
  }
  </code></pre>
  
  <p>When I now try to obtain an access token, I get an error saying: Error: </p>
  
  <pre><code>Call to a member function loadUserByUsername() on a non-object in
  vendor\friendsofsymfony\oauth-server-bundle\FOS\OAuthServerBundle\Storage\OAuthStorage.php at line 165.
  </code></pre>
  
  <p>So basically, the UserProvider is always null.</p>
  
  <p>Does anybody know, what I'm doing wrong here? I'm totally stuck and any help is greatly appreciated!</p>