Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/auth0/symfony

Symfony SDK for Auth0 Authentication and Management APIs.
https://github.com/auth0/symfony

dx-sdk php symfony

Last synced: 3 days ago
JSON representation

Symfony SDK for Auth0 Authentication and Management APIs.

Awesome Lists containing this project

README

        

![auth0/symfony](https://cdn.auth0.com/website/sdks/banners/jwt-auth-bundle-banner.png)

Symfony SDK for [Auth0](https://auth0.com) Authentication and Management APIs.

:books: [Documentation](#documentation) - :rocket: [Getting Started](#getting-started) - :speech_balloon: [Feedback](#feedback)

## Documentation

- [Docs site](https://www.auth0.com/docs) — explore our docs site and learn more about Auth0.

## Getting Started

### Requirements

- [PHP](http://php.net/) 8.1+
- [Symfony](https://symfony.com/) 6.4 LTS or 7
- Symfony 7 support is community-contributed and presently experimental.

> Please review our [support policy](#support-policy) to learn when language and framework versions will exit support in the future.

### Installation

Add the dependency to your application with [Composer](https://getcomposer.org/):

```
composer require auth0/symfony
```

### Configure Auth0

Create a **Regular Web Application** in the [Auth0 Dashboard](https://manage.auth0.com/#/applications). Verify that the "Token Endpoint Authentication Method" is set to `POST`.

Next, configure the callback and logout URLs for your application under the "Application URIs" section of the "Settings" page:

- **Allowed Callback URLs**: URL of your application where Auth0 will redirect to during authentication, e.g., `http://localhost:8000/callback`.
- **Allowed Logout URLs**: URL of your application where Auth0 will redirect to after logout, e.g., `http://localhost:8000/login`.

Note the **Domain**, **Client ID**, and **Client Secret**. These values will be used later.

### Configure the SDK

After installation, you should find a new file in your application, `config/packages/auth0.yaml`. If this file isn't present, please create it manually.

The following is an example configuration that will use environment variables to assign values. You should avoid storing sensitive credentials directly in this file, as it will often be committed to version control.

```yaml
auth0:
sdk:
domain: "%env(trim:string:AUTH0_DOMAIN)%"
client_id: "%env(trim:string:AUTH0_CLIENT_ID)%"
client_secret: "%env(trim:string:AUTH0_CLIENT_SECRET)%"
cookie_secret: "%kernel.secret%"

# custom_domain: "%env(trim:string:AUTH0_CUSTOM_DOMAIN)%"

# audiences:
# - "%env(trim:string:AUTH0_API_AUDIENCE)%"

# token_cache: cache.auth0_token_cache
# management_token_cache: cache.auth0_management_token_cache

scopes:
- openid
- profile
- email
- offline_access

authenticator:
routes:
callback: "%env(string:AUTH0_ROUTE_CALLBACK)%"
success: "%env(string:AUTH0_ROUTE_SUCCESS)%"
failure: "%env(string:AUTH0_ROUTE_FAILURE)%"
login: "%env(string:AUTH0_ROUTE_LOGIN)%"
logout: "%env(string:AUTH0_ROUTE_LOGOUT)%"
```

### Configure your `.env` file

Create or open a `.env.local` file within your application directory, and add the following lines:

```ini
#
# ↓ Refer to your Auth0 application details (https://manage.auth0.com/#/applications) for these values.
#

# Your Auth0 application domain
AUTH0_DOMAIN=...

# Your Auth0 application client ID
AUTH0_CLIENT_ID=...

# Your Auth0 application client secret
AUTH0_CLIENT_SECRET=...

# Optional. Your Auth0 custom domain, if you have one. (https://manage.auth0.com/#/custom_domains)
AUTH0_CUSTOM_DOMAIN=...

# Optional. Your Auth0 API identifier/audience, if used. (https://manage.auth0.com/#/apis)
AUTH0_API_AUDIENCE=...

#
# ↓ These routes will be used by the SDK to direct traffic during authentication.
#

# The route that SDK will redirect to after authentication:
AUTH0_ROUTE_CALLBACK=callback

# The route that will trigger the authentication process:
AUTH0_ROUTE_LOGIN=login

# The route that the SDK will redirect to after a successful authentication:
AUTH0_ROUTE_SUCCESS=private

# The route that the SDK will redirect to after a failed authentication:
AUTH0_ROUTE_FAILURE=public

# The route that the SDK will redirect to after a successful logout:
AUTH0_ROUTE_LOGOUT=public
```

Please ensure this `.env.local` file is included in your `.gitignore`. It should never be committed to version control.

### Configure your `security.yaml` file

Open your application's `config/packages/security.yaml` file, and update it based on the following example:

```yaml
security:
providers:
auth0_provider:
id: Auth0\Symfony\Security\UserProvider

firewalls:
auth0:
pattern: ^/private$ # A pattern example for stateful (session-based authentication) route requests
provider: auth0_provider
custom_authenticators:
- auth0.authenticator
api:
pattern: ^/api # A pattern example for stateless (token-based authorization) route requests
stateless: true
provider: auth0_provider
custom_authenticators:
- auth0.authorizer
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true

access_control:
- { path: ^/api$, roles: PUBLIC_ACCESS } # PUBLIC_ACCESS is a special role that allows everyone to access the path.
- { path: ^/api/scoped$, roles: ROLE_USING_TOKEN } # The ROLE_USING_TOKEN role is added by the Auth0 SDK to any request that includes a valid access token.
- { path: ^/api/scoped$, roles: ROLE_READ_MESSAGES } # This route will expect the given access token to have the `read:messages` scope in order to access it.
```

### Update your `config/bundle.php`

The SDK bundle should be automatically detected and registered by [Symfony Flex](https://symfony.com/doc/current/setup.html#symfony-flex) projects, but you may need to add the Auth0Bundle to your application's bundle registry. Either way, it's a good idea to register the bundle anyway, just to be safe.

```php
['all' => true],
];
```

### Optional: Add Authentication helper routes

The SDK includes a number of pre-built HTTP controllers that can be used to handle authentication. These controllers are not required, but can be helpful in getting started. In many cases, these may provide all the functionality you need to integrate Auth0 into your application, providing a plug-and-play solution.

To use these, open your application's `config/routes.yaml` file, and add the following lines:

```yaml
login: # Send the user to Auth0 for authentication.
path: /login
controller: Auth0\Symfony\Controllers\AuthenticationController::login

callback: # This user will be returned here from Auth0 after authentication; this is a special route that completes the authentication process. After this, the user will be redirected to the route configured as `AUTH0_ROUTE_SUCCESS` in your .env file.
path: /callback
controller: Auth0\Symfony\Controllers\AuthenticationController::callback

logout: # This route will clear the user's session and return them to the route configured as `AUTH0_ROUTE_LOGOUT` in your .env file.
path: /logout
controller: Auth0\Symfony\Controllers\AuthenticationController::logout
```

### Recommended: Configure caching

The SDK provides two caching properties in it's configuration: `token_cache` and `management_token_cache`. These are compatible with any PSR-6 cache implementation, of which Symfony offers several out of the box.

These are used to store JSON Web Key Sets (JWKS) results for validating access token signatures and generated management API tokens, respectively. We recommended configuring this feature to improve your application's performance by reducing the number of network requests the SDK needs to make. It will also greatly help in avoiding hitting rate-limiting conditions, if you're making frequent Management API requests.

The following is an example `config/packages/cache.yaml` file that would configure the SDK to use a Redis backend for caching:

```yaml
framework:
cache:
prefix_seed: auth0_symfony_sample

app: cache.adapter.redis
default_redis_provider: redis://localhost

pools:
auth0_token_cache: { adapter: cache.adapter.redis }
auth0_management_token_cache: { adapter: cache.adapter.redis }
```

Please review [the Symfony cache documentation](https://symfony.com/doc/current/components/cache.html#cache-component-psr6-caching) for adapter-specific configuration options. Please note that the SDK does not currently support Symfony's "Cache Contract" adapter type.

### Example: Retrieving the User

The following example shows how to retrieve the authenticated user within a controller. For this example, we'll create a mock `ExampleController` class that is accessible from a route at `/private`.

Add a route to your application's `config/routes.yaml` file:

```yaml
private:
path: /private
controller: App\Controller\ExampleController::private
```

Now update or create a `src/Controller/ExampleController.php` class to include the following code:

```php

' . print_r($this->getUser(), true) . '
Logout