|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Majora\Bundle\OAuthServerBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Majora\Component\OAuth\Entity\AccessToken; |
|
6
|
|
|
use Majora\Component\OAuth\Entity\RefreshToken; |
|
7
|
|
|
use Majora\Component\OAuth\Model\AccessTokenInterface; |
|
8
|
|
|
use Majora\Component\OAuth\Model\RefreshTokenInterface; |
|
9
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
10
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Majora OAuth server bundle semantical configuration class. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Configuration implements ConfigurationInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
* |
|
20
|
|
|
* @see http://symfony.com/doc/current/components/config/definition.html |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getConfigTreeBuilder() |
|
23
|
|
|
{ |
|
24
|
|
|
$treeBuilder = new TreeBuilder(); |
|
25
|
|
|
$treeBuilder->root('majora_oauth_server') |
|
26
|
|
|
->children() |
|
27
|
|
|
->scalarNode('secret') |
|
28
|
|
|
->isRequired() |
|
29
|
|
|
->cannotBeEmpty() |
|
30
|
|
|
->end() |
|
31
|
|
|
->append($this->createTokenNode( |
|
32
|
|
|
'access_token', |
|
33
|
|
|
AccessToken::class, |
|
34
|
|
|
AccessTokenInterface::class, |
|
35
|
|
|
AccessTokenInterface::DEFAULT_TTL |
|
36
|
|
|
)) |
|
37
|
|
|
->append($this->createTokenNode( |
|
38
|
|
|
'refresh_token', |
|
39
|
|
|
RefreshToken::class, |
|
40
|
|
|
RefreshTokenInterface::class, |
|
41
|
|
|
RefreshTokenInterface::DEFAULT_TTL |
|
42
|
|
|
)) |
|
43
|
|
|
->arrayNode('application') |
|
44
|
|
|
->isRequired() |
|
45
|
|
|
->children() |
|
46
|
|
|
->append($this->createDriverStrategyNode('loader')) |
|
47
|
|
|
->append($this->createDriverStrategyNode('repository')) |
|
48
|
|
|
->end() |
|
49
|
|
|
->end() |
|
50
|
|
|
->arrayNode('account') |
|
51
|
|
|
->isRequired() |
|
52
|
|
|
->children() |
|
53
|
|
|
->append($this->createDriverStrategyNode('loader')) |
|
54
|
|
|
->append($this->createDriverStrategyNode('repository')) |
|
55
|
|
|
->end() |
|
56
|
|
|
->end() |
|
57
|
|
|
->end() |
|
58
|
|
|
; |
|
59
|
|
|
|
|
60
|
|
|
return $treeBuilder; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create a token configuration node. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $tokenName |
|
67
|
|
|
* @param string $defaultClass |
|
68
|
|
|
* @param string $tokenInterface |
|
69
|
|
|
* @param string $defaultTtl |
|
70
|
|
|
* |
|
71
|
|
|
* @return Node |
|
72
|
|
|
*/ |
|
73
|
|
|
private function createTokenNode($tokenName, $defaultClass, $tokenInterface, $defaultTtl) |
|
74
|
|
|
{ |
|
75
|
|
|
$builder = new TreeBuilder(); |
|
76
|
|
|
$node = $builder->root($tokenName); |
|
77
|
|
|
$node |
|
78
|
|
|
->addDefaultsIfNotSet() |
|
79
|
|
|
->children() |
|
80
|
|
|
->integerNode('ttl') |
|
81
|
|
|
->defaultValue($defaultTtl) |
|
82
|
|
|
->end() |
|
83
|
|
|
->scalarNode('class') |
|
84
|
|
|
->cannotBeEmpty() |
|
85
|
|
|
->defaultValue($defaultClass) |
|
86
|
|
|
->validate() |
|
87
|
|
|
->ifTrue(function ($accessTokenClass) use ($tokenInterface) { |
|
88
|
|
|
return !( |
|
89
|
|
|
class_exists($accessTokenClass, true) |
|
90
|
|
|
&& (new \ReflectionClass($accessTokenClass)) |
|
91
|
|
|
->implementsInterface($tokenInterface) |
|
92
|
|
|
); |
|
93
|
|
|
}) |
|
94
|
|
|
->thenInvalid(sprintf( |
|
95
|
|
|
'Provided access_token configuration has to be a valid class which implements %s.', |
|
96
|
|
|
$tokenInterface |
|
97
|
|
|
)) |
|
98
|
|
|
->end() |
|
99
|
|
|
->end() |
|
100
|
|
|
->append($this->createDriverStrategyNode('loader')) |
|
101
|
|
|
->append($this->createDriverStrategyNode('repository')) |
|
102
|
|
|
->end() |
|
103
|
|
|
; |
|
104
|
|
|
|
|
105
|
|
|
return $node; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function createDriverStrategyNode($strategyName) |
|
109
|
|
|
{ |
|
110
|
|
|
$builder = new TreeBuilder(); |
|
111
|
|
|
$node = $builder->root($strategyName); |
|
112
|
|
|
$node |
|
113
|
|
|
->addDefaultsIfNotSet() |
|
114
|
|
|
->children() |
|
115
|
|
|
->scalarNode('id')->end() |
|
116
|
|
|
->arrayNode('orm') |
|
117
|
|
|
->children() |
|
118
|
|
|
->scalarNode('entity_manager') |
|
119
|
|
|
->defaultValue('default') |
|
120
|
|
|
->cannotBeEmpty() |
|
121
|
|
|
->end() |
|
122
|
|
|
->end() |
|
123
|
|
|
->end() |
|
124
|
|
|
->scalarNode('empty')->defaultNull()->end() |
|
125
|
|
|
->end() |
|
126
|
|
|
->validate() |
|
127
|
|
|
->always() |
|
128
|
|
|
->then(function ($v) { |
|
129
|
|
|
if (count($v) == 2) { |
|
130
|
|
|
unset($v['empty']); |
|
131
|
|
|
} |
|
132
|
|
|
if (count($v) > 1) { |
|
133
|
|
|
throw new \InvalidArgumentException( |
|
134
|
|
|
'You provided too much drivers, you have to choose only one.' |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $v; |
|
139
|
|
|
}) |
|
140
|
|
|
->end() |
|
141
|
|
|
; |
|
142
|
|
|
|
|
143
|
|
|
return $node; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|