1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\ServerBundle\Component\Scope; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Scope\ScopeRepository; |
17
|
|
|
use OAuth2Framework\ServerBundle\Component\Component; |
18
|
|
|
use OAuth2Framework\ServerBundle\Component\Scope\Compiler\ScopeMetadataCompilerPass; |
19
|
|
|
use OAuth2Framework\ServerBundle\Component\Scope\Compiler\ScopePolicyCompilerPass; |
20
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
21
|
|
|
use Symfony\Component\Config\FileLocator; |
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
23
|
|
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
24
|
|
|
|
25
|
|
|
class ScopeSource implements Component |
26
|
|
|
{ |
27
|
|
|
public function name(): string |
28
|
|
|
{ |
29
|
|
|
return 'scope'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function load(array $configs, ContainerBuilder $container) |
33
|
|
|
{ |
34
|
|
|
if (!\interface_exists(ScopeRepository::class) || !$configs['scope']['enabled']) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$container->setAlias(ScopeRepository::class, $configs['scope']['repository']); |
39
|
|
|
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config/scope')); |
40
|
|
|
$loader->load('scope.php'); |
41
|
|
|
|
42
|
|
|
if (!$configs['scope']['policy']['enabled']) { |
43
|
|
|
$container->setParameter('oauth2_server.scope.policy.by_default', 'none'); |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$container->setParameter('oauth2_server.scope.policy.by_default', $configs['scope']['policy']['by_default']); |
49
|
|
|
$loader->load('policy.php'); |
50
|
|
|
|
51
|
|
|
if ($configs['scope']['policy']['default']['enabled']) { |
52
|
|
|
$container->setParameter('oauth2_server.scope.policy.default.scope', $configs['scope']['policy']['default']['scope']); |
53
|
|
|
$loader->load('policy_default.php'); |
54
|
|
|
} |
55
|
|
|
if ($configs['scope']['policy']['error']['enabled']) { |
56
|
|
|
$loader->load('policy_error.php'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getNodeDefinition(ArrayNodeDefinition $node, ArrayNodeDefinition $rootNode) |
61
|
|
|
{ |
62
|
|
|
if (!\interface_exists(ScopeRepository::class)) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
$node->children() |
66
|
|
|
->arrayNode($this->name()) |
67
|
|
|
->canBeEnabled() |
68
|
|
|
->children() |
69
|
|
|
->scalarNode('repository') |
70
|
|
|
->info('Scope repository.') |
71
|
|
|
->defaultNull() |
72
|
|
|
->end() |
73
|
|
|
->arrayNode('policy') |
|
|
|
|
74
|
|
|
->canBeEnabled() |
75
|
|
|
->children() |
76
|
|
|
->scalarNode('by_default') |
77
|
|
|
->info('Default scope policy.') |
78
|
|
|
->defaultValue('none') |
79
|
|
|
->end() |
80
|
|
|
->arrayNode('error') |
81
|
|
|
->canBeEnabled() |
82
|
|
|
->info('When the error policy is used, requests without a scope are not allowed') |
83
|
|
|
->end() |
84
|
|
|
->arrayNode('default') |
85
|
|
|
->canBeEnabled() |
86
|
|
|
->children() |
87
|
|
|
->scalarNode('scope') |
88
|
|
|
->info('Scope added by default.') |
89
|
|
|
->isRequired() |
90
|
|
|
->end() |
91
|
|
|
->end() |
92
|
|
|
->end() |
93
|
|
|
->end() |
94
|
|
|
->end() |
95
|
|
|
->end() |
96
|
|
|
->end() |
97
|
|
|
->end(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function build(ContainerBuilder $container) |
101
|
|
|
{ |
102
|
|
|
$container->addCompilerPass(new ScopePolicyCompilerPass()); |
103
|
|
|
$container->addCompilerPass(new ScopeMetadataCompilerPass()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function prepend(ContainerBuilder $container, array $config): array |
107
|
|
|
{ |
108
|
|
|
//Nothing to do |
109
|
|
|
return []; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|