|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use Laravel\Passport\Client; |
|
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
7
|
|
|
use Illuminate\Foundation\Testing\Concerns\MakesHttpRequests; |
|
8
|
|
|
use Illuminate\Foundation\Testing\Concerns\InteractsWithConsole; |
|
9
|
|
|
|
|
10
|
|
|
trait MultiauthActions |
|
11
|
|
|
{ |
|
12
|
|
|
use MakesHttpRequests, InteractsWithConsole; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The route to generate the access token. The default value is the standard |
|
16
|
|
|
* route from Laravel\Passport. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $oauthTokenRoute = 'oauth/token'; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp() |
|
23
|
|
|
{ |
|
24
|
|
|
parent::setUp(); |
|
25
|
|
|
|
|
26
|
|
|
$this->artisan('passport:install'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set the the Authorization header with an access token created using |
|
31
|
|
|
* Laravel Passport. |
|
32
|
|
|
* |
|
33
|
|
|
* @todo Change way to issue token from $this->json() to creating accessing |
|
34
|
|
|
* AccessTokenController@issueToken directly. |
|
35
|
|
|
* @todo Pass this method to PassportMultiauth::actingAs(). |
|
36
|
|
|
* |
|
37
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
|
38
|
|
|
* @param string $scope |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
|
|
public function multiauthActingAs(Authenticatable $user, $scope = '') |
|
42
|
|
|
{ |
|
43
|
|
|
$client = Client::where('personal_access_client', false) |
|
44
|
|
|
->where('revoked', false) |
|
45
|
|
|
->first(); |
|
46
|
|
|
|
|
47
|
|
|
$provider = ''; |
|
48
|
|
|
foreach (config('auth.providers') as $p => $config) { |
|
49
|
|
|
if ($user instanceof $config['model']) { |
|
50
|
|
|
$provider = $p; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$params = [ |
|
55
|
|
|
'grant_type' => 'password', |
|
56
|
|
|
'client_id' => $client->id, |
|
57
|
|
|
'client_secret' => $client->secret, |
|
58
|
|
|
'username' => $user->email, |
|
|
|
|
|
|
59
|
|
|
'password' => 'secret', |
|
60
|
|
|
'scope' => $scope, |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
// If model to be authenticated don't has the default provider |
|
64
|
|
|
if (config('auth.guards.api.provider') !== $provider) { |
|
65
|
|
|
$params = array_merge($params, ['provider' => $provider]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$response = $this->json('POST', $this->oauthTokenRoute, $params); |
|
69
|
|
|
$accessToken = json_decode($response->original)->access_token; |
|
70
|
|
|
|
|
71
|
|
|
$this->withHeader('Authorization', 'Bearer '.$accessToken); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: