marketredesign / mrd-auth0-laravel
Wrapper to easily configure Auth0 with a Laravel application
Installs: 11 749
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 4
Requires
- php: ^8.1
- ext-json: *
- auth0/auth0-php: ^8.5
- auth0/login: ~7.7.0
- guzzlehttp/guzzle: ^6.0|^7.0
- illuminate/contracts: ^10.0
- illuminate/support: ^10.0
- marketredesign/newrelic-monolog-enricher: ^3.0
Requires (Dev)
- nunomaduro/larastan: ^2.5
- orchestra/testbench: ^8.3
- phpmd/phpmd: ^2.9
- phpunit/phpunit: ^10.0
- psr-mock/http: ^1.0
- squizlabs/php_codesniffer: ^3.5
- dev-develop
- v3.1.1
- v3.1.0
- v3.0.0
- v2.x-dev
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- dev-feature/v3/oidc
- dev-feature/v3/reintroduce-oidc
- dev-feature/oidc
- dev-feature/v2/oidc
- dev-master
- dev-feature/auth0-7.8
- dev-feature/scopes
This package is auto-updated.
Last update: 2025-01-10 18:06:24 UTC
README
Wrapper to easily configure Auth0 with a Laravel application.
Also includes a logger for NewRelic.
Getting Started
Prerequisites
- PHP 7.4 or higher
- PHP JSON extension
- PHP mbstring extension
- PHP XML extension
- PHP Curl extension
- Laravel 6 or higher
Installing
You can install the package via composer:
composer require marketredesign/mrd-auth0-laravel
For configuration, the default config files can be published using the following command.
php artisan vendor:publish
Select the option for Marketredesign\MrdAuth0Laravel\MrdAuth0LaravelServiceProvider
. This creates the config file
config/mrd-auth0.php
.
Upgrade to v2
See the UPGRADE guide for instructions when updating an application that uses v1 to v2.
Usage
See laravel-auth0 for instructions on how to configure authentication / authorization of users.
Authorizing dataset access
Add the dataset.access
middleware to the API route. Then, make sure the dataset ID is specified using either
dataset_id
or datasetId
. It can be part of the route itself or part of the request data (query param,
request body, etc.)
Requesting machine-to-machine tokens from Auth0
Use Auth0
facade. Can be used to retrieve a machine-to-machine token, only when running in console (e.g. from async
job). The tokens are automatically cached for half their expiration time.
When testing a function that retrieves a m2m token, execute Auth0::fake()
to use a mocked Auth0Repository which does
not make any API calls to Auth0. The fake repository can be influenced using the Auth0::fake...()
functions.
User repository
Use Users
facade. Can be used to retrieve a single user, or multiple users, by ID.
Also includes functionality to retrieve multiple users by email addresses.
When testing a function that uses the UserRepository (or Facade), execute Users::fake()
to use a mocked UserRepository
which does not make any API calls to Auth0. The fake repository can be influenced using Users::fake...()
methods.
Dataset repository
Use Datasets
facade. Can be used to retrieve authorized datasets for the current user making the API request.
When testing a function that uses the DatasetRepository (or Datasets facade), execute Datasets::fake()
to use a mocked
version of the DatasetRepository that does not make any API calls to the underlying user tool API. The fake repository
can be influenced using the Datasets::fake...()
methods.
Logging to NewRelic
Create a new logger in the config/logging.php
file (example code below) and make sure that the NEWRELIC_LICENSE_KEY
is set. Then, this logger can be selected as any other logger, for example by setting the LOG_CHANNEL
to 'newrelic'
.
This logger looks for optional app.repository
and app.version
config values to log along with every
logged message, so add those fields to the config/app.php
file if you want this to be included.
Example code
Logger for in config/logging.php
:
'newrelic' => [ 'driver' => 'custom', 'via' => \Marketredesign\MrdAuth0Laravel\Logging\NewRelicLogger::class, 'license_key' => env('NEWRELIC_LICENSE_KEY'), ],
Optional app.repository
and app.version
config values for in config/app.php
:
/* |-------------------------------------------------------------------------- | Repository Name |-------------------------------------------------------------------------- | | The name of the repository this application is an instance of. | Used for example when logging to NewRelic. | */ 'repository' => 'your-repository-name-here', /* |-------------------------------------------------------------------------- | Application Version |-------------------------------------------------------------------------- | | Version name of the code currently. When developing, this will be local. | When the code is being built, a version.txt document at the root should | be created containing the version number (or other build specification | such as the commit hash), which is then loaded into this config variable. | */ 'version' => file_exists('../version.txt') ? file('../version.txt')[0] : 'local',
Running the tests
Simply run:
vendor/bin/phpunit
Authors
- Marijn van der Horst - Initial work
See also the list of contributors who participated in this project.
License
The MIT License (MIT). Please see License File for more information.