Symfony API: Authorization

Community maintained

Sample Project

Download a sample project specific to this tutorial configured with your Auth0 API Keys.

System Requirements
  • PHP 5.5
  • Symfony 3.2
Show requirements

This tutorial shows you how to use the authorization features in the OAuth 2.0 framework to limit access to your or third-party applications. For more information, read the API authorization documentation.

Create a Resource Server (API)

In the APIs section of the Auth0 dashboard, click Create API. Provide a name and an identifier for your API. You will use the identifier as an audience later, when you are configuring the Access Token verification. For Signing Algorithm, select RS256.

Create API

Add API Authorization

To restrict access to the resources served by your API, check the incoming requests for valid authorization information. The authorization information is stored in the Access Token created for the user and needs to be sent in the Authorization header. To see if the token is valid, check it against the JSON Web Key Set (JWKS) for your Auth0 account. To learn more about validating Access Tokens, read the Verify Access Tokens tutorial.

Install the Dependencies

Protecting a Symfony API with Auth0 requires the jwt-auth-bundle package. Install it using composer.

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you. See Composer's getting started doc for information on how to use it.

composer require auth0/jwt-auth-bundle:"^3.0"

v3.x.x of the jwt-auth-bundle provides compatibility with Symfony 3. For Symfony 2.x support, use the v1 branch.

Add the Bundle to AppKernel.php

// app/AppKernel.php

class AppKernel extends Kernel
    public function registerBundles()
        $bundles = array(

            // ...
            new \Auth0\JWTAuthBundle\JWTAuthBundle(),


        // ...
        return $bundles;

Add Configuration Values

Add your Auth0 domain and API audience to the config.yml file located in app/config.

    domain: YOUR_AUTH0_DOMAIN
    authorized_issuer: https://YOUR_AUTH0_DOMAIN/
    api_identifier: {YOUR_API_IDENTIFIER}

Set Up the User and UserProvider

Create your User and UserProvider.

The UserProvider must implement the JWTUserProviderInterface (see /source/AppBundle/Security/A0UserProvider). This class should implement two methods:

  • loadUserByJWT: receives the decoded JWT access_token and returns a User.

  • getAnonymousUser: returns an anonymous user that represents an unauthenticated one (usually represented by the role IS_AUTHENTICATED_ANONYMOUSLY).

Both of the above methods can throw an AuthenticationException. If this exception is thrown by loadUserByJWT, the it will treat the user as being anonymous. If getAnonymousUser throw this exception, a 401 Unauthorized will be returned.

Provide configuration for the UserProvider in the services.yml file located in app/config.

        class: AppBundle\Security\A0UserProvider
        arguments: ["@jwt_auth.auth0_service"]

Set Up the SecurityProvider

Modify the security.yml file located in app/config such that it contains the following:

  • The UserProvider
  • The secured area that you want to authenticate using an access_token
  • The access_control section with the roles needed for each route

            pattern: ^/api
            stateless: true
                authenticator: jwt_auth.jwt_authenticator

        - { path: ^/api/private-scoped, roles: ROLE_OAUTH_READER }
        - { path: ^/api/private, roles: ROLE_OAUTH_AUTHENTICATED }
        - { path: ^/api/public, roles: IS_AUTHENTICATED_ANONYMOUSLY }

Set Up a Protected Route

// src/AppBundle/Controller/SecuredController.php

// ...
class SecuredController extends Controller
     * @Route("/api/public", name="public")
    // This route doesn't need authentication
    public function publicAction()
        return new JsonResponse(array(
          'message' => "Hello from a public endpoint! You don't need to be authenticated to see this."
     * @Route("/api/private", name="private")
    public function privateAction()
        return new JsonResponse(array(
          'message' => "Hello from a private endpoint! You need to be authenticated to see this."
     * @Route("/api/private-scoped", name="privatescoped")
    public function privateScopedAction()
        return new JsonResponse(array(
          'message' => "Hello from a private endpoint! You need to be authenticated and have a scope of read:messages to see this."

Configuring Scopes

Scopes provide a way for you to define which resources should be accessible by the user holding a given access_token. For example, you might choose to permit read access to a messages resource if a user has a manager access level, or a write access to that resource if they are an administrator. The route defined above expects a scope of read:messages to be present in the payload of the access_token.

To configure scopes in your Auth0 dashboard, navigate to your API and choose the Scopes tab. In this area you can apply any scopes you wish, including one called read:messages, which will be used in this example.

With this configuration in place, only access_tokens which have a scope of read:messages will be allowed to access this endpoint.

Use Auth0 for FREECreate free Account