|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Models\Users\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\Auth\Models\Users\Traits\AbstractUserTrait; |
|
6
|
|
|
use ByTIC\Hello\Models\Clients\PersonalAccess\ClientsManager; |
|
7
|
|
|
use ByTIC\Hello\Models\Traits\HasApiTokensTrait; |
|
8
|
|
|
use ByTIC\Hello\Models\Users\Resolvers\UsersResolvers; |
|
9
|
|
|
use ByTIC\Hello\Utility\ModelsHelper; |
|
10
|
|
|
use League\OAuth2\Server\Entities\Traits\EntityTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait UserTrait |
|
14
|
|
|
* @package ByTIC\Hello\Models\Users\ |
|
15
|
|
|
* |
|
16
|
|
|
* @property string $access_token |
|
17
|
|
|
* @property string $access_jwt |
|
18
|
|
|
*/ |
|
19
|
|
|
trait UserTrait |
|
20
|
|
|
{ |
|
21
|
|
|
use AbstractUserTrait { |
|
22
|
|
|
doAuthentication as doAuthenticationTrait; |
|
23
|
|
|
} |
|
24
|
|
|
use HasApiTokensTrait, EntityTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
3 |
|
public function getIdentifier() |
|
30
|
|
|
{ |
|
31
|
3 |
|
if (empty($this->identifier)) { |
|
32
|
1 |
|
$this->initIdentifier(); |
|
33
|
|
|
} |
|
34
|
3 |
|
return $this->identifier; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function doAuthentication() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->initAccessToken(); |
|
40
|
|
|
$this->doAuthenticationTrait(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function checkAccessToken() |
|
44
|
|
|
{ |
|
45
|
|
|
if (empty($this->access_token)) { |
|
46
|
|
|
$this->initAccessToken(); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function initAccessToken() |
|
51
|
|
|
{ |
|
52
|
|
|
$token = $this->token(); |
|
53
|
|
|
$token->setPrivateKey(app('hello.keys.private')); |
|
54
|
|
|
$this->access_token = $token->getIdentifier(); |
|
55
|
|
|
$this->access_jwt = $token->__toString(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
protected function initIdentifier() |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->setIdentifier(UsersResolvers::identifier($this)); |
|
|
|
|
|
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return \ByTIC\Hello\Models\AccessTokens\Token|null |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function generateToken() |
|
67
|
|
|
{ |
|
68
|
|
|
$userTokens = ModelsHelper::accessTokens()->getValidUserTokens($this, ClientsManager::get()); |
|
69
|
|
|
if (count($userTokens) < 1) { |
|
70
|
|
|
return $this->createToken($this->generateTokenName()); |
|
71
|
|
|
} |
|
72
|
|
|
return $userTokens->current(); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|