I want to do server-to-server authentication for my API using OAuth2 and Symfony2 (Symfony3 actually). I am using FOSOAuthServerBundle.
The remote server will not be making requests on behalf of any user, so I believe client_credentials is the proper grant type to use.
I have created a client and am able to obtain an access token. However, I am having an issue with protecting an endpoint with a client_credentials token. This is what is in my security.yml
firewalls:
api:
pattern: ^/api/v1
fos_oauth: true
stateless: true
anonymous: false # can be omitted as its default value
access_control:
- { path: ^/api/v1, roles: [ IS_AUTHENTICATED_FULLY ] }
When I try to access something in /api/v1 using the Bearer Authorization header, I get an error
Full authentication is required to access this resource
Here is a sample request:
GET /app_dev.php/api/v1/user HTTP/1.1
Host: local.dev
Authorization: Bearer NDFhMzViZjQ2YjMyYjFlNzBjZTZiMTU2ZjdhY2I4ZmZhZjY2MmVkMjU3NzNjNDE2NGI2YzEzMWFjZGQ5MzE4NA
I assume the problem is that since I am using client_credentials there is no user and thus IS_FULLY_AUTHENTICATED is not true. If that is the case, how can I authenticate the client without a user?
Note: I have also tried removing the lines
access_control:
- { path: ^/api/v1, roles: [ IS_AUTHENTICATED_FULLY ] }
then trying to access the client details in the controller with the following:
$tokenManager = $this->get('fos_oauth_server.access_token_manager.default');
$accessToken = $tokenManager->findTokenByToken(
$this->get('security.token_storage')->getToken()->getToken()
);
$client = $accessToken->getClient();
but then I get the error:
Attempted to call an undefined method named "getToken" of class "Symfony\Component\Security\Core\Authentication\Token\AnonymousToken"