@@ -280,6 +280,139 @@ public function test_authentication_fails_if_callback_returns_false()
280
280
281
281
$ user = $ requestGuard ->setRequest ($ request )->user ();
282
282
$ this ->assertNull ($ user );
283
+
284
+ Sanctum::$ accessTokenAuthenticationCallback = null ;
285
+ }
286
+
287
+ public function test_authentication_is_successful_with_token_in_custom_header ()
288
+ {
289
+ $ this ->loadLaravelMigrations (['--database ' => 'testbench ' ]);
290
+ $ this ->artisan ('migrate ' , ['--database ' => 'testbench ' ])->run ();
291
+
292
+ $ factory = Mockery::mock (AuthFactory::class);
293
+
294
+ $ guard = new Guard ($ factory , null );
295
+
296
+ $ webGuard = Mockery::mock (stdClass::class);
297
+
298
+ $ factory ->shouldReceive ('guard ' )
299
+ ->with ('web ' )
300
+ ->andReturn ($ webGuard );
301
+
302
+ $ webGuard ->shouldReceive ('user ' )->once ()->andReturn (null );
303
+
304
+ $ request = Request::create ('/ ' , 'GET ' );
305
+ $ request ->headers ->set ('X-Auth-Token ' , 'test ' );
306
+
307
+ $ user = User::forceCreate ([
308
+ 'name ' => 'Taylor Otwell ' ,
309
+ 'email ' => 'taylor@laravel.com ' ,
310
+ 'password ' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi ' ,
311
+ 'remember_token ' => Str::random (10 ),
312
+ ]);
313
+
314
+ $ token = PersonalAccessToken::forceCreate ([
315
+ 'tokenable_id ' => $ user ->id ,
316
+ 'tokenable_type ' => get_class ($ user ),
317
+ 'name ' => 'Test ' ,
318
+ 'token ' => hash ('sha256 ' , 'test ' ),
319
+ ]);
320
+
321
+ Sanctum::getAccessTokenFromRequestUsing (function (Request $ request ) {
322
+ return $ request ->header ('X-Auth-Token ' );
323
+ });
324
+
325
+ $ returnedUser = $ guard ->__invoke ($ request );
326
+
327
+ $ this ->assertEquals ($ user ->id , $ returnedUser ->id );
328
+ $ this ->assertEquals ($ token ->id , $ returnedUser ->currentAccessToken ()->id );
329
+ $ this ->assertInstanceOf (DateTimeInterface::class, $ returnedUser ->currentAccessToken ()->last_used_at );
330
+
331
+ Sanctum::$ accessTokenRetrievalCallback = null ;
332
+ }
333
+
334
+ public function test_authentication_fails_with_token_in_authorization_header_when_using_custom_header ()
335
+ {
336
+ $ this ->loadLaravelMigrations (['--database ' => 'testbench ' ]);
337
+ $ this ->artisan ('migrate ' , ['--database ' => 'testbench ' ])->run ();
338
+
339
+ $ factory = Mockery::mock (AuthFactory::class);
340
+
341
+ $ guard = new Guard ($ factory , null );
342
+
343
+ $ webGuard = Mockery::mock (stdClass::class);
344
+
345
+ $ factory ->shouldReceive ('guard ' )
346
+ ->with ('web ' )
347
+ ->andReturn ($ webGuard );
348
+
349
+ $ webGuard ->shouldReceive ('user ' )->once ()->andReturn (null );
350
+
351
+ $ request = Request::create ('/ ' , 'GET ' );
352
+ $ request ->headers ->set ('Authorization ' , 'Bearer test ' );
353
+
354
+ $ user = User::forceCreate ([
355
+ 'name ' => 'Taylor Otwell ' ,
356
+ 'email ' => 'taylor@laravel.com ' ,
357
+ 'password ' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi ' ,
358
+ 'remember_token ' => Str::random (10 ),
359
+ ]);
360
+
361
+ $ token = PersonalAccessToken::forceCreate ([
362
+ 'tokenable_id ' => $ user ->id ,
363
+ 'tokenable_type ' => get_class ($ user ),
364
+ 'name ' => 'Test ' ,
365
+ 'token ' => hash ('sha256 ' , 'test ' ),
366
+ ]);
367
+
368
+ Sanctum::getAccessTokenFromRequestUsing (function (Request $ request ) {
369
+ return $ request ->header ('X-Auth-Token ' );
370
+ });
371
+
372
+ $ returnedUser = $ guard ->__invoke ($ request );
373
+
374
+ $ this ->assertNull ($ returnedUser );
375
+
376
+ Sanctum::$ accessTokenRetrievalCallback = null ;
377
+ }
378
+
379
+ public function test_authentication_fails_with_token_in_custom_header_when_using_default_authorization_header ()
380
+ {
381
+ $ this ->loadLaravelMigrations (['--database ' => 'testbench ' ]);
382
+ $ this ->artisan ('migrate ' , ['--database ' => 'testbench ' ])->run ();
383
+
384
+ $ factory = Mockery::mock (AuthFactory::class);
385
+
386
+ $ guard = new Guard ($ factory , null );
387
+
388
+ $ webGuard = Mockery::mock (stdClass::class);
389
+
390
+ $ factory ->shouldReceive ('guard ' )
391
+ ->with ('web ' )
392
+ ->andReturn ($ webGuard );
393
+
394
+ $ webGuard ->shouldReceive ('user ' )->once ()->andReturn (null );
395
+
396
+ $ request = Request::create ('/ ' , 'GET ' );
397
+ $ request ->headers ->set ('X-Auth-Token ' , 'test ' );
398
+
399
+ $ user = User::forceCreate ([
400
+ 'name ' => 'Taylor Otwell ' ,
401
+ 'email ' => 'taylor@laravel.com ' ,
402
+ 'password ' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi ' ,
403
+ 'remember_token ' => Str::random (10 ),
404
+ ]);
405
+
406
+ $ token = PersonalAccessToken::forceCreate ([
407
+ 'tokenable_id ' => $ user ->id ,
408
+ 'tokenable_type ' => get_class ($ user ),
409
+ 'name ' => 'Test ' ,
410
+ 'token ' => hash ('sha256 ' , 'test ' ),
411
+ ]);
412
+
413
+ $ returnedUser = $ guard ->__invoke ($ request );
414
+
415
+ $ this ->assertNull ($ returnedUser );
283
416
}
284
417
285
418
protected function getPackageProviders ($ app )
0 commit comments