1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth; |
4
|
|
|
|
5
|
|
|
use Laravel\Passport\Token; |
6
|
|
|
use Laravel\Passport\HasApiTokens as BaseHasApiTokens; |
7
|
|
|
use Laravel\Passport\PersonalAccessTokenFactory; |
8
|
|
|
use Illuminate\Container\Container; |
9
|
|
|
use SMartins\PassportMultiauth\Config\AuthConfigHelper; |
10
|
|
|
|
11
|
|
|
trait HasMultiAuthApiTokens |
12
|
|
|
{ |
13
|
|
|
use BaseHasApiTokens; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Get all of the access tokens for the user relating with Provider. |
17
|
|
|
* |
18
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
19
|
|
|
*/ |
20
|
1 |
|
public function tokens() |
21
|
|
|
{ |
22
|
1 |
|
return $this->hasMany(Token::class, 'user_id') |
|
|
|
|
23
|
|
|
->join('oauth_access_token_providers', function ($join) { |
24
|
1 |
|
$join->on( |
25
|
1 |
|
'oauth_access_tokens.id', '=', 'oauth_access_token_id' |
26
|
1 |
|
)->where('oauth_access_token_providers.provider', '=', AuthConfigHelper::getUserProvider($this)); |
|
|
|
|
27
|
1 |
|
})->orderBy('created_at', 'desc') |
28
|
1 |
|
->select('oauth_access_tokens.*') |
29
|
1 |
|
->get(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new personal access token for the user and create . |
34
|
|
|
* |
35
|
|
|
* @param string $name |
36
|
|
|
* @param array $scopes |
37
|
|
|
* @return \Laravel\Passport\PersonalAccessTokenResult |
38
|
|
|
*/ |
39
|
3 |
|
public function createToken($name, array $scopes = []) |
40
|
|
|
{ |
41
|
|
|
// Backup default provider |
42
|
3 |
|
$defaultProvider = config('auth.guards.api.provider'); |
43
|
|
|
|
44
|
3 |
|
$userProvider = AuthConfigHelper::getUserProvider($this); |
|
|
|
|
45
|
|
|
|
46
|
|
|
// Change config to when the token is created set the provider from model creating the token. |
47
|
3 |
|
config(['auth.guards.api.provider' => $userProvider]); |
48
|
|
|
|
49
|
3 |
|
$token = Container::getInstance()->make(PersonalAccessTokenFactory::class)->make( |
50
|
3 |
|
$this->getKey(), $name, $scopes |
|
|
|
|
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
// Reset config to defaults |
54
|
3 |
|
config(['auth.guards.api.provider' => $defaultProvider]); |
55
|
|
|
|
56
|
3 |
|
return $token; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|