44
55use App \Models \User ;
66use Illuminate \Auth \Passwords \PasswordBrokerManager ;
7+ use Illuminate \Contracts \Auth \Authenticatable ;
78use Illuminate \Cookie \CookieValuePrefix ;
89use Illuminate \Database \Eloquent \Factories \Factory ;
910use Illuminate \Foundation \Testing \TestCase as BaseTestCase ;
@@ -397,21 +398,22 @@ protected function actingAsApiUser($email, $scopes = ['read', 'write'], $guard =
397398 * @param array $params
398399 * @param Client|null $client
399400 */
400- protected function getAccessToken (string $ grant_type = 'client_credentials ' , array $ scopes = ['read ' , 'write ' ], array $ params = [], ?Client $ client = null )
401+ protected function getAccessToken (
402+ string $ grant_type = 'client_credentials ' ,
403+ array $ scopes = ['read ' , 'write ' ],
404+ array $ params = [],
405+ ?Client $ client = null
406+ )
401407 {
402408 if (empty ($ client )) {
403409 // create a new client
404410 $ user = $ this ->getActiveAdminUser ('demo-user@javaabu.com ' );
405- $ client = (new ClientRepository ())->create (
406- $ user ->id ,
407- 'Test Client ' ,
408- 'http://localhost '
409- );
411+ $ client = $ this ->getOAuthClient ($ grant_type , 'users ' , $ user );
410412 }
411413
412414 $ request_params = array_merge ([
413415 'client_id ' => $ client ->id ,
414- 'client_secret ' => $ client ->secret ,
416+ 'client_secret ' => $ client ->plainSecret ?: $ client -> secret ,
415417 'grant_type ' => $ grant_type ,
416418 'scope ' => implode (' ' , $ scopes ),
417419 ], $ params );
@@ -428,6 +430,46 @@ protected function getAccessToken(string $grant_type = 'client_credentials', arr
428430 return ($ response ->json ())['access_token ' ];
429431 }
430432
433+ protected function getOAuthClient (
434+ string $ grant_type = 'client_credentials ' ,
435+ ?string $ provider = null ,
436+ ?Authenticatable $ user = null
437+ ): Client
438+ {
439+ $ repository = new ClientRepository ();
440+ $ client = null ;
441+
442+ // check if Passport 12
443+ if (is_callable ([$ repository , 'create ' ])) {
444+ // create a new client using old Passport 12 method
445+ $ client = $ repository ->create (
446+ $ user ?->id,
447+ 'Test Client ' ,
448+ 'http://localhost ' ,
449+ $ provider ,
450+ password: $ grant_type == 'password '
451+ );
452+ } else {
453+ if ($ grant_type === 'client_credentials ' ) {
454+ $ client = $ repository ->createClientCredentialsGrantClient (
455+ 'Test Client '
456+ );
457+ } elseif ($ grant_type === 'personal_access ' ) {
458+ $ client = $ repository ->createPersonalAccessGrantClient (
459+ 'Test Client ' ,
460+ $ provider
461+ );
462+ } else {
463+ $ client = $ repository ->createPasswordGrantClient (
464+ 'Test Client ' ,
465+ $ provider
466+ );
467+ }
468+ }
469+
470+ return $ client ;
471+ }
472+
431473 /**
432474 * Get client access token
433475 * @param array $scopes
@@ -491,7 +533,7 @@ protected function getOAuthCookie($user)
491533 // initialize the CSRF Token
492534 session ()->start ();
493535
494- $ identifier = ($ user && $ user ->is_active ) ? $ user ->getPassportCookieIdentifier () : null ;
536+ $ identifier = ($ user && $ user ->is_active ) ? $ user ->getPassportCookieIdentifier () : '' ;
495537
496538 $ cookie = $ cookie_factory ->make ($ identifier , session ()->token ());
497539
0 commit comments