Skip to content

Commit 5a29f98

Browse files
authored
implement auth0 guard (#166)
Add the auth0 guard middleware
1 parent 392a192 commit 5a29f98

File tree

7 files changed

+70
-12
lines changed

7 files changed

+70
-12
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@ You can implement your own cache strategy by creating a new class that implement
4646

4747
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.
4848

49+
### Using auth guard
50+
51+
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`:
52+
```
53+
'guards' => [
54+
...
55+
'auth0' => [
56+
'driver' => 'auth0',
57+
'provider' => 'auth0',
58+
],
59+
],
60+
61+
'providers' => [
62+
...
63+
'auth0' => [
64+
'driver' => 'auth0',
65+
],
66+
],
67+
```
68+
69+
Once that has been added, add the guard to the middleware of any API route and check authentication during the request:
70+
```
71+
// get user
72+
auth('auth0')->user();
73+
// check if logged in
74+
auth('auth0')->check();
75+
// protect routes via middleware use
76+
Route::group(['middleware' => 'auth:auth0'], function () {});
77+
```
78+
4979
## Installation
5080

5181
Install this plugin into a new or existing project using [Composer](https://getcomposer.org/doc/00-intro.md):

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"illuminate/contracts": "5.* | ^6.0 | ^7.0"
1111
},
1212
"require-dev": {
13-
"phpunit/phpunit": "^7",
13+
"phpunit/phpunit": "^7|^8|^9",
1414
"squizlabs/php_codesniffer": "^3.2",
1515
"phpcompatibility/php-compatibility": "^8.1",
1616
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
17-
"orchestra/testbench": "^3.8"
17+
"orchestra/testbench": "^3.8|^4.0|^5.0"
1818
},
1919
"scripts": {
2020
"test": "SHELL_INTERACTIVE=1 \"vendor/bin/phpunit\" --coverage-text ",

phpunit.xml.dist

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="tests/bootstrap.php"
2+
<phpunit bootstrap="vendor/autoload.php"
33
backupGlobals="false"
44
backupStaticAttributes="false"
55
colors="true"
@@ -19,4 +19,8 @@
1919
<directory suffix=".php">src/</directory>
2020
</whitelist>
2121
</filter>
22+
<php>
23+
<ini name="session.use_cookies" value="false"/>
24+
<ini name="session.cache_limiter" value="false"/>
25+
</php>
2226
</phpunit>

src/Auth0/Login/LoginServiceProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Auth0\Login;
44

5+
use Auth0\Login\Contract\Auth0UserRepository as Auth0UserRepositoryContract;
6+
use Auth0\Login\Repository\Auth0UserRepository;
57
use Auth0\SDK\API\Helpers\ApiClient;
68
use Auth0\SDK\API\Helpers\InformationHeaders;
79
use Auth0\SDK\Store\StoreInterface;
10+
use Illuminate\Auth\RequestGuard;
11+
use Illuminate\Http\Request;
812
use Illuminate\Support\ServiceProvider;
913

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

28+
\Auth::extend('auth0', function ($app, $name, $config) {
29+
return new RequestGuard(function (Request $request, Auth0UserProvider $provider) {
30+
return $provider->retrieveByCredentials(['api_token' => $request->bearerToken()]);
31+
}, $app['request'], $app['auth']->createUserProvider($config['provider']));
32+
});
33+
2434
$this->publishes([
2535
__DIR__.'/../../config/config.php' => config_path('laravel-auth0.php'),
2636
]);
@@ -48,6 +58,8 @@ public function register()
4858
return new LaravelSessionStore();
4959
});
5060

61+
$this->app->bind(Auth0UserRepositoryContract::class, Auth0UserRepository::class);
62+
5163
// Bind the auth0 name to a singleton instance of the Auth0 Service
5264
$this->app->singleton(Auth0Service::class, function ($app) {
5365
return new Auth0Service(

tests/Auth0ServiceTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Auth0\Login\Tests;
33

4+
use Auth0\Login\Auth0JWTUser;
45
use Auth0\Login\Auth0Service;
56
use Auth0\Login\Facade\Auth0 as Auth0Facade;
67
use Auth0\Login\LoginServiceProvider as Auth0ServiceProvider;
@@ -14,7 +15,7 @@ class Auth0ServiceTest extends OrchestraTestCase
1415
{
1516
public static $defaultConfig;
1617

17-
public static function setUpBeforeClass()
18+
public static function setUpBeforeClass(): void
1819
{
1920
parent::setUpBeforeClass();
2021
self::$defaultConfig = [
@@ -85,6 +86,17 @@ public function testThatServiceCanUseLaravelCache()
8586
$service->decodeJWT(uniqid());
8687
}
8788

89+
public function testThatGuardAuthenticatesUsers()
90+
{
91+
$this->assertTrue(\Auth('auth0')->guest());
92+
93+
$user = new Auth0JWTUser((object)['sub' => 'x']);
94+
95+
\Auth('auth0')->setUser($user);
96+
97+
$this->assertTrue(\Auth('auth0')->check());
98+
}
99+
88100
/*
89101
* Test suite helpers
90102
*/
@@ -100,4 +112,11 @@ protected function getPackageAliases($app)
100112
'Auth0' => Auth0Facade::class,
101113
];
102114
}
115+
116+
protected function getEnvironmentSetUp($app)
117+
{
118+
$app['config']->set('auth.guards.auth0', ['driver' => 'auth0', 'provider' => 'auth0']);
119+
$app['config']->set('auth.providers.auth0', ['driver' => 'auth0']);
120+
$app['config']->set('laravel-auth0', self::$defaultConfig);
121+
}
103122
}

tests/Unit/Auth0JWTUserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Auth0JWTUserTest extends TestCase
1212
*/
1313
protected $auth0JwtUser;
1414

15-
public function setUp()
15+
public function setUp(): void
1616
{
1717
parent::setUp();
1818
$this->auth0JwtUser = new Auth0JWTUser((object)[

tests/bootstrap.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)