1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth\Testing; |
4
|
|
|
|
5
|
|
|
use Laravel\Passport\Client; |
6
|
|
|
use Illuminate\Support\Facades\App; |
7
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
8
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
9
|
|
|
|
10
|
|
|
trait MultiauthActions |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The route to generate the access token. The default value is the standard |
14
|
|
|
* route from Laravel\Passport. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $oauthTokenRoute = 'oauth/token'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @codeCoverageIgnore |
22
|
|
|
*/ |
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
$this->artisan('passport:install'); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set the the Authorization header with an access token created using |
32
|
|
|
* Laravel Passport. The method `ẁithHeader` of trait `MakesHttpRequests` is |
33
|
|
|
* avaiable after version 5.5 of Laravel Framework. |
34
|
|
|
* |
35
|
|
|
* @todo Change way to issue token from $this->json() to creating accessing |
36
|
|
|
* AccessTokenController@issueToken directly. |
37
|
|
|
* @todo Pass this method to PassportMultiauth::actingAs(). |
38
|
|
|
* |
39
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
40
|
|
|
* @param string $scope |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
10 |
|
public function multiauthActingAs(Authenticatable $user, $scope = '') |
44
|
|
|
{ |
45
|
10 |
|
if ((float) App::version() < 5.5) { |
46
|
1 |
|
throw new \RuntimeException('The method is only available to Laravel >= 5.5. To older versions try use $this->multiauthAccessToken() to get access token.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
9 |
|
$this->withHeader('Authorization', $this->multiauthAccessToken($user, $scope)); |
|
|
|
|
50
|
|
|
|
51
|
8 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get multiauth header to be used on request to get access token. |
56
|
|
|
* |
57
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
58
|
|
|
* @param string $scope |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
9 |
|
public function multiauthAccessToken(Authenticatable $user, $scope = '') |
62
|
|
|
{ |
63
|
|
|
// @todo Change to specific repository |
64
|
9 |
|
$client = Client::where('personal_access_client', false) |
65
|
9 |
|
->where('revoked', false) |
66
|
9 |
|
->first(); |
67
|
|
|
|
68
|
9 |
|
if (! $client) { |
69
|
1 |
|
throw new ModelNotFoundException('Laravel\Passport password grant not found. Please run `passport:install` to generate client.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
8 |
|
$provider = $this->getUserProvider($user); |
73
|
|
|
|
74
|
|
|
$params = [ |
75
|
8 |
|
'grant_type' => 'password', |
76
|
8 |
|
'client_id' => $client->id, |
77
|
8 |
|
'client_secret' => $client->secret, |
78
|
8 |
|
'username' => $user->email, |
|
|
|
|
79
|
8 |
|
'password' => 'secret', |
80
|
8 |
|
'scope' => $scope, |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
// If model to be authenticated don't is the default provider |
84
|
8 |
|
if (! $this->isDefaultProvider($provider)) { |
85
|
4 |
|
$params = array_merge($params, ['provider' => $provider]); |
86
|
|
|
} |
87
|
|
|
|
88
|
8 |
|
$response = $this->json('POST', $this->oauthTokenRoute, $params); |
|
|
|
|
89
|
|
|
|
90
|
8 |
|
$accessToken = json_decode($response->getContent())->access_token; |
91
|
|
|
|
92
|
8 |
|
return 'Bearer '.$accessToken; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the user provider on configs. |
97
|
|
|
* |
98
|
|
|
* @todo Move to class specialized in check auth configs. |
99
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
8 |
|
protected function getUserProvider(Authenticatable $user) |
103
|
|
|
{ |
104
|
8 |
|
$provider = ''; |
105
|
8 |
|
foreach (config('auth.providers') as $p => $config) { |
106
|
8 |
|
if ($user instanceof $config['model']) { |
107
|
8 |
|
$provider = $p; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
8 |
|
return $provider; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check if provider is the default provider used by Laravel\Passport. |
116
|
|
|
* |
117
|
|
|
* @todo Move to class specialized in check auth configs. |
118
|
|
|
* @param string $provider |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
8 |
|
protected function isDefaultProvider($provider) |
122
|
|
|
{ |
123
|
8 |
|
return config('auth.guards.api.provider') === $provider; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.