1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Oauth\ServiceProvider\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\Hello\Utility\ConfigHelper; |
6
|
|
|
use DateInterval; |
7
|
|
|
use League\OAuth2\Server\AuthorizationServer; |
8
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
9
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
10
|
|
|
use League\OAuth2\Server\Repositories\UserRepositoryInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait GrantsTrait |
14
|
|
|
* @package ByTIC\Hello\Oauth\ServiceProvider\Traits |
15
|
|
|
*/ |
16
|
|
|
trait GrantsTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param AuthorizationServer $server |
20
|
|
|
*/ |
21
|
1 |
|
public function registerGrants(AuthorizationServer $server) |
22
|
|
|
{ |
23
|
1 |
|
$grantTypes = $this->getRegisteredGrants(); |
24
|
1 |
|
foreach ($grantTypes as $type => $class) { |
25
|
1 |
|
$method = 'makeGrant' . $type; |
26
|
1 |
|
$this->{$method}($server, $class); |
27
|
|
|
} |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return mixed|\Nip\Config\Config |
32
|
|
|
*/ |
33
|
|
|
protected function getRegisteredGrants() |
34
|
|
|
{ |
35
|
|
|
return ConfigHelper::get('grant_types', []); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param AuthorizationServer $server |
40
|
|
|
* @param string $class |
41
|
|
|
*/ |
42
|
|
|
protected function makeGrantPersonalAccess(AuthorizationServer $server, string $class) |
43
|
|
|
{ |
44
|
|
|
$this->makeGrant( |
45
|
|
|
$server, |
46
|
|
|
$class, |
47
|
|
|
[], |
48
|
|
|
new DateInterval('P1Y') |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param AuthorizationServer $server |
54
|
|
|
* @param string $class |
55
|
|
|
*/ |
56
|
|
|
protected function makeGrantAuthCode(AuthorizationServer $server, string $class) |
57
|
|
|
{ |
58
|
|
|
$this->makeGrant( |
59
|
|
|
$server, |
60
|
|
|
$class, |
61
|
|
|
[ |
62
|
|
|
$this->getContainer()->get(AuthCodeRepositoryInterface::class), |
|
|
|
|
63
|
|
|
$this->getContainer()->get(RefreshTokenRepositoryInterface::class), |
64
|
|
|
ConfigHelper::get('token_ttl') |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param AuthorizationServer $server |
71
|
|
|
* @param string $class |
72
|
|
|
*/ |
73
|
|
|
protected function makeGrantRefreshToken(AuthorizationServer $server, string $class) |
74
|
|
|
{ |
75
|
|
|
$this->makeGrant($server, $class, [ |
76
|
|
|
$this->getContainer()->get(RefreshTokenRepositoryInterface::class) |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param AuthorizationServer $server |
82
|
|
|
* @param string $class |
83
|
|
|
*/ |
84
|
|
|
protected function makeGrantPassword(AuthorizationServer $server, string $class) |
85
|
|
|
{ |
86
|
|
|
$this->makeGrant($server, $class, [ |
87
|
|
|
$this->getContainer()->get(UserRepositoryInterface::class), |
88
|
|
|
$this->getContainer()->get(RefreshTokenRepositoryInterface::class) |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param AuthorizationServer $server |
94
|
|
|
* @param string $class |
95
|
|
|
*/ |
96
|
|
|
protected function makeGrantImplicit(AuthorizationServer $server, string $class) |
97
|
|
|
{ |
98
|
|
|
$this->makeGrant($server, $class, [ |
99
|
|
|
ConfigHelper::get('token_ttl') |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param AuthorizationServer $server |
105
|
|
|
* @param string $class |
106
|
|
|
*/ |
107
|
|
|
protected function makeGrantClientCredentials(AuthorizationServer $server, string $class) |
108
|
|
|
{ |
109
|
|
|
$this->makeGrant($server, $class, []); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param AuthorizationServer $server |
114
|
|
|
* @param string|null $type |
115
|
|
|
* @param array $args |
116
|
|
|
*/ |
117
|
|
|
protected function makeGrant(AuthorizationServer $server, string $type, array $args, $accessTokenTTL = null) |
118
|
|
|
{ |
119
|
|
|
if ($type !== null) { |
|
|
|
|
120
|
|
|
$grant = new $type(...$args); |
121
|
|
|
if ($type !== null && $grant !== null) { |
122
|
|
|
$server->enableGrantType($grant, $accessTokenTTL); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|