Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ You can implement your own cache strategy by creating a new class that implement

You can customize the way you handle the users in your application by creating your own `UserRepository`. This class should implement the `Auth0\Login\Contract\Auth0UserRepository` contract. Please see the [Custom User Handling section of the Laravel Quickstart](https://auth0.com/docs/quickstart/webapp/laravel#optional-custom-user-handling) for the latest example.

### Using auth guard

To protect APIs using an access token generated by Auth0, there is an `auth0` API guard provided ([Laravel documentation on guards](https://laravel.com/docs/7.x/authentication#adding-custom-guards)). To use this guard, add it to `config/auth.php` with the driver `auth0`:
```
'guards' => [
...
'auth0' => [
'driver' => 'auth0',
'provider' => 'auth0',
],
],

'providers' => [
...
'auth0' => [
'driver' => 'auth0',
],
],
```

Once that has been added, add the guard to the middleware of any API route and check authentication during the request:
```
// get user
auth('auth0')->user();
// check if logged in
auth('auth0')->check();
// protect routes via middleware use
Route::group(['middleware' => 'auth:auth0'], function () {});
```

## Installation

Install this plugin into a new or existing project using [Composer](https://getcomposer.org/doc/00-intro.md):
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"require": {
"php": "^7.1",
"auth0/auth0-php": "^7.0",
"illuminate/support": "5.* | ^6.0",
"illuminate/contracts": "5.* | ^6.0"
"illuminate/support": "5.* | ^6.0 | ^7.0",
"illuminate/contracts": "5.* | ^6.0 | ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^7",
"phpunit/phpunit": "^7|^8|^9",
"squizlabs/php_codesniffer": "^3.2",
"phpcompatibility/php-compatibility": "^8.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"orchestra/testbench": "^3.8"
"orchestra/testbench": "^3.8|^4.0|^5.0"
},
"scripts": {
"test": "SHELL_INTERACTIVE=1 \"vendor/bin/phpunit\" --coverage-text ",
Expand Down
6 changes: 5 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
Expand All @@ -19,4 +19,8 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<ini name="session.use_cookies" value="false"/>
<ini name="session.cache_limiter" value="false"/>
</php>
</phpunit>
12 changes: 12 additions & 0 deletions src/Auth0/Login/LoginServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace Auth0\Login;

use Auth0\Login\Contract\Auth0UserRepository as Auth0UserRepositoryContract;
use Auth0\Login\Repository\Auth0UserRepository;
use Auth0\SDK\API\Helpers\ApiClient;
use Auth0\SDK\API\Helpers\InformationHeaders;
use Auth0\SDK\Store\StoreInterface;
use Illuminate\Auth\RequestGuard;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;

class LoginServiceProvider extends ServiceProvider
Expand All @@ -21,6 +25,12 @@ public function boot()
return $app->make(Auth0UserProvider::class);
});

\Auth::extend('auth0', function ($app, $name, $config) {
return new RequestGuard(function (Request $request, Auth0UserProvider $provider) {
return $provider->retrieveByCredentials(['api_token' => $request->bearerToken()]);
}, $app['request'], $app['auth']->createUserProvider($config['provider']));
});

$this->publishes([
__DIR__.'/../../config/config.php' => config_path('laravel-auth0.php'),
]);
Expand Down Expand Up @@ -48,6 +58,8 @@ public function register()
return new LaravelSessionStore();
});

$this->app->bind(Auth0UserRepositoryContract::class, Auth0UserRepository::class);

// Bind the auth0 name to a singleton instance of the Auth0 Service
$this->app->singleton(Auth0Service::class, function ($app) {
return new Auth0Service(
Expand Down
21 changes: 20 additions & 1 deletion tests/Auth0ServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Auth0\Login\Tests;

use Auth0\Login\Auth0JWTUser;
use Auth0\Login\Auth0Service;
use Auth0\Login\Facade\Auth0 as Auth0Facade;
use Auth0\Login\LoginServiceProvider as Auth0ServiceProvider;
Expand All @@ -14,7 +15,7 @@ class Auth0ServiceTest extends OrchestraTestCase
{
public static $defaultConfig;

public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$defaultConfig = [
Expand Down Expand Up @@ -85,6 +86,17 @@ public function testThatServiceCanUseLaravelCache()
$service->decodeJWT(uniqid());
}

public function testThatGuardAuthenticatesUsers()
{
$this->assertTrue(\Auth('auth0')->guest());

$user = new Auth0JWTUser((object)['sub' => 'x']);

\Auth('auth0')->setUser($user);

$this->assertTrue(\Auth('auth0')->check());
}

/*
* Test suite helpers
*/
Expand All @@ -100,4 +112,11 @@ protected function getPackageAliases($app)
'Auth0' => Auth0Facade::class,
];
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('auth.guards.auth0', ['driver' => 'auth0', 'provider' => 'auth0']);
$app['config']->set('auth.providers.auth0', ['driver' => 'auth0']);
$app['config']->set('laravel-auth0', self::$defaultConfig);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Auth0JWTUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Auth0JWTUserTest extends TestCase
*/
protected $auth0JwtUser;

public function setUp()
public function setUp(): void
{
parent::setUp();
$this->auth0JwtUser = new Auth0JWTUser((object)[
Expand Down
7 changes: 0 additions & 7 deletions tests/bootstrap.php

This file was deleted.