Skip to content

Commit 85ecea8

Browse files
committed
wip
1 parent 0df81a5 commit 85ecea8

File tree

8 files changed

+120
-32
lines changed

8 files changed

+120
-32
lines changed

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ $timing->getDuration();
219219
```
220220

221221

222-
## Sanctum dev auth
222+
## Dev auth
223223

224224
Each `api` has authentication, and testing it via HTTP Client (ie postman) we spend a lot of time to login users and copy the token, put in the next request and so on. Well now Laravel Developer provides an easy way to authenticate users in `local` env using `testing` token:
225225

@@ -228,31 +228,36 @@ Each `api` has authentication, and testing it via HTTP Client (ie postman) we sp
228228

229229
'api' => [
230230
//...
231-
\Binarcode\LaravelDeveloper\Middleware\DevSanctumAuthMiddleware::class,
231+
\Binarcode\LaravelDeveloper\Middleware\DevAuthMiddleware::class,
232232
]
233233
```
234234

235235
And send the request with the `Authorization` header value `testing`.
236236

237-
Note: Make sure the `DevSanctumAuthMiddleware` is placed before the `api` middleware.
237+
Note: Make sure the `DevAuthMiddleware` is placed before the `api` middleware.
238238

239239
### Customize resolved user
240240

241241
By default, the first entry (usually user) from your config model `app.providers.users.model` will be used, however, you can customize that.
242242

243-
In any of yours service providers, or in the same place you inject the `DevSanctumAuthMiddleware` you can provide a callback which resolves the user instance:
243+
In any of yours service providers, or in the same place you inject the `DevAuthMiddleware` you can provide a callback which resolves the user instance:
244244

245245
```php
246246
use App\Models\User;
247-
use Binarcode\LaravelDeveloper\Middleware\DevSanctumAuthMiddleware;
247+
use Binarcode\LaravelDeveloper\Middleware\DevAuthMiddleware;
248248

249249
'middleware' => [
250-
DevSanctumAuthMiddleware::resolveUserUsing(function() {
250+
DevAuthMiddleware::resolveUserUsing(function() {
251251
return User::first();
252252
});
253253
'api',
254254
],
255255
```
256+
257+
### Changing Bearer
258+
259+
If you're using laravel sanctum, and want to explicitely use / generate a Bearer for the resolved user, you can use the `\Binarcode\LaravelDeveloper\Middleware\DevAuthMiddleware::class` instead, which follow the same syntax as the `DevAuthMiddleware`.
260+
256261
## Testing
257262

258263
``` bash

config/laravel-developer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@
1919
* The default notification class used to send notifications.
2020
*/
2121
'notification' => \Binarcode\LaravelDeveloper\Notifications\DevNotification::class,
22+
23+
'auth' => [
24+
'bearer' => 'testing',
25+
],
2226
];

src/LaravelDeveloperServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function boot()
2929

3030
public function register()
3131
{
32-
$this->mergeConfigFrom(__DIR__ . '/../config/laravel-developer.php', 'laravel-developer');
32+
$this->mergeConfigFrom(__DIR__ . '/../config/laravel-developer.php', 'developer');
3333
}
3434

3535
public static function migrationFileExists(string $migrationFileName): bool
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Contracts\Auth\Authenticatable;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\App;
9+
use Illuminate\Support\Facades\Auth;
10+
11+
class DevAuthMiddleware
12+
{
13+
/**
14+
* @var Closure
15+
*/
16+
public static $resolveUser;
17+
18+
/**
19+
* Handle an incoming request.
20+
*
21+
* @param \Illuminate\Http\Request $request
22+
* @param \Closure $next
23+
* @return mixed
24+
*/
25+
public function handle(Request $request, Closure $next)
26+
{
27+
if (! App::environment('local')) {
28+
return $next($request);
29+
}
30+
31+
if ($request->header('Authorization') !== 'Bearer '. config('developer.auth.bearer', 'testing')) {
32+
return $next($request);
33+
}
34+
35+
if (is_callable(static::$resolveUser)) {
36+
$user = call_user_func(static::$resolveUser, $request);
37+
} else {
38+
/** * @var string $class */
39+
$class = config('app.providers.users.model');
40+
41+
$user = $class::query()->first();
42+
}
43+
44+
if ($user instanceof Authenticatable) {
45+
Auth::setUser($user);
46+
}
47+
48+
return $next($request);
49+
}
50+
51+
public static function resolveUserUsing(Closure $resolveUser): string
52+
{
53+
static::$resolveUser = $resolveUser;
54+
55+
return static::class;
56+
}
57+
}

src/Middleware/DevSanctumAuthMiddleware.php

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,8 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Facades\App;
88

9-
class DevSanctumAuthMiddleware
9+
class DevSanctumAuthMiddleware extends DevAuthMiddleware
1010
{
11-
/**
12-
* @var Closure
13-
*/
14-
public static $resolveUser;
15-
16-
/**
17-
* Handle an incoming request.
18-
*
19-
* @param \Illuminate\Http\Request $request
20-
* @param \Closure $next
21-
* @return mixed
22-
*/
2311
public function handle(Request $request, Closure $next)
2412
{
2513
if (! App::environment('local')) {
@@ -58,11 +46,4 @@ public function handle(Request $request, Closure $next)
5846

5947
return $next($request);
6048
}
61-
62-
public static function resolveUserUsing(Closure $resolveUser): string
63-
{
64-
static::$resolveUser = $resolveUser;
65-
66-
return static::class;
67-
}
6849
}

tests/Fixtures/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Laravel\Sanctum\PersonalAccessToken;
66

7-
class User
7+
class User extends \Illuminate\Foundation\Auth\User
88
{
99
use \Laravel\Sanctum\HasApiTokens;
1010

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Binarcode\LaravelDeveloper\Tests\Middleware;
4+
5+
use Binarcode\LaravelDeveloper\Middleware\DevAuthMiddleware;
6+
use Binarcode\LaravelDeveloper\Tests\Fixtures\User;
7+
use Binarcode\LaravelDeveloper\Tests\TestCase;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Support\Facades\App;
10+
use Illuminate\Support\Facades\Auth;
11+
use Symfony\Component\HttpFoundation\HeaderBag;
12+
13+
class DevAuthMiddlewareTest extends TestCase
14+
{
15+
public function test_can_authenticate_using_testing()
16+
{
17+
$user = new User;
18+
DevAuthMiddleware::resolveUserUsing(fn() => $user);
19+
20+
$middleware = new DevAuthMiddleware;
21+
22+
$request = tap(new Request(), function (Request $request) {
23+
/** * @var HeaderBag $bag */
24+
$bag = $request->headers;
25+
26+
$bag->add([
27+
'Authorization' => "Bearer testing",
28+
]);
29+
});
30+
31+
$next = function ($request) {
32+
return $request;
33+
};
34+
35+
App::partialMock()
36+
->expects('environment')
37+
->andReturnTrue();
38+
39+
$middleware->handle($request, $next);
40+
41+
$this->assertAuthenticatedAs($user);
42+
}
43+
}

tests/Middleware/DevSanctumAuthMiddlewareTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
class DevSanctumAuthMiddlewareTest extends TestCase
1313
{
14-
public function test_can_authenticate_using_testing()
14+
public function test_can_authenticate_and_change_bearer_using_testing()
1515
{
16-
DevSanctumAuthMiddleware::resolveUserUsing(function () {
17-
return new User();
18-
});
16+
DevSanctumAuthMiddleware::resolveUserUsing(fn() => new User);
1917

2018
$middleware = new DevSanctumAuthMiddleware();
2119

0 commit comments

Comments
 (0)