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; |
25
|
|
|
use EntityTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
3 |
|
public function getIdentifier() |
31
|
|
|
{ |
32
|
3 |
|
if (empty($this->identifier)) { |
33
|
1 |
|
$this->initIdentifier(); |
34
|
|
|
} |
35
|
3 |
|
return $this->identifier; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function doAuthentication() |
39
|
|
|
{ |
40
|
|
|
$this->initAccessToken(); |
41
|
|
|
$this->doAuthenticationTrait(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function checkAccessToken() |
45
|
|
|
{ |
46
|
|
|
if (empty($this->access_token)) { |
47
|
|
|
$this->initAccessToken(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function initAccessToken() |
52
|
|
|
{ |
53
|
|
|
$token = $this->token(); |
54
|
|
|
$token->setPrivateKey(app('hello.keys.private')); |
55
|
|
|
$this->access_token = $token->getIdentifier(); |
56
|
|
|
$this->access_jwt = $token->__toString(); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
protected function initIdentifier() |
60
|
|
|
{ |
61
|
1 |
|
$this->setIdentifier(UsersResolvers::identifier($this)); |
|
|
|
|
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return \ByTIC\Hello\Models\AccessTokens\Token|null |
66
|
|
|
*/ |
67
|
|
|
protected function generateToken() |
68
|
|
|
{ |
69
|
|
|
$userTokens = ModelsHelper::accessTokens()->getValidUserTokens($this, ClientsManager::get()); |
70
|
|
|
if (count($userTokens) < 1) { |
71
|
|
|
return $this->createToken($this->generateTokenName()); |
72
|
|
|
} |
73
|
|
|
return $userTokens->current(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|