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