Skip to content

Commit f8c2bc0

Browse files
authored
Nested tokens (#85)
Fix #65 Nested Token Support
1 parent 7055ece commit f8c2bc0

27 files changed

+2293
-21
lines changed

src/Bundle/JoseFramework/DependencyInjection/Source/AbstractSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function addConfiguration(NodeDefinition $node)
6060
->useAttributeAsKey('name')
6161
->treatNullLike([])
6262
->treatFalseLike([])
63-
->prototype('variable')->end()
63+
->variablePrototype()->end()
6464
->end()
6565
->end();
6666
}

src/Bundle/JoseFramework/DependencyInjection/Source/Checker/ClaimChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getNodeDefinition(NodeDefinition $node)
8181
->useAttributeAsKey('name')
8282
->treatNullLike([])
8383
->treatFalseLike([])
84-
->prototype('variable')
84+
->variablePrototype()
8585
->end()
8686
->end()
8787
->end()

src/Bundle/JoseFramework/DependencyInjection/Source/Checker/HeaderChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getNodeDefinition(NodeDefinition $node)
8181
->useAttributeAsKey('name')
8282
->treatNullLike([])
8383
->treatFalseLike([])
84-
->prototype('variable')
84+
->variablePrototype()
8585
->end()
8686
->end()
8787
->end()

src/Bundle/JoseFramework/DependencyInjection/Source/Encryption/AbstractEncryptionSource.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ public function getNodeDefinition(NodeDefinition $node)
5252
->info('A list of supported compression methods.')
5353
->useAttributeAsKey('name')
5454
->defaultValue(['DEF'])
55-
->requiresAtLeastOneElement()
5655
->scalarPrototype()->end()
5756
->end()
5857
->arrayNode('tags')
5958
->info('A list of tags to be associated to the service.')
6059
->useAttributeAsKey('name')
6160
->treatNullLike([])
6261
->treatFalseLike([])
63-
->prototype('variable')->end()
62+
->variablePrototype()->end()
6463
->end()
6564
->end()
6665
->end()

src/Bundle/JoseFramework/DependencyInjection/Source/Encryption/JWELoader.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getNodeDefinition(NodeDefinition $node)
6565
->arrayNode($this->name())
6666
->requiresAtLeastOneElement()
6767
->useAttributeAsKey('name')
68-
->prototype('array')
68+
->arrayPrototype()
6969
->children()
7070
->booleanNode('is_public')
7171
->info('If true, the service will be public, else private.')
@@ -75,39 +75,39 @@ public function getNodeDefinition(NodeDefinition $node)
7575
->info('A list of key encryption algorithm aliases.')
7676
->useAttributeAsKey('name')
7777
->isRequired()
78-
->prototype('scalar')->end()
78+
->scalarPrototype()->end()
7979
->end()
8080
->arrayNode('content_encryption_algorithms')
8181
->info('A list of key encryption algorithm aliases.')
8282
->useAttributeAsKey('name')
8383
->isRequired()
84-
->prototype('scalar')->end()
84+
->scalarPrototype()->end()
8585
->end()
8686
->arrayNode('compression_methods')
8787
->info('A list of compression method aliases.')
8888
->useAttributeAsKey('name')
89-
->isRequired()
90-
->prototype('scalar')->end()
89+
->defaultValue(['DEF'])
90+
->scalarPrototype()->end()
9191
->end()
9292
->arrayNode('serializers')
9393
->info('A list of signature serializer aliases.')
9494
->useAttributeAsKey('name')
9595
->requiresAtLeastOneElement()
96-
->prototype('scalar')->end()
96+
->scalarPrototype()->end()
9797
->end()
9898
->arrayNode('header_checkers')
9999
->info('A list of header checker aliases.')
100100
->useAttributeAsKey('name')
101101
->treatNullLike([])
102102
->treatFalseLike([])
103-
->prototype('scalar')->end()
103+
->scalarPrototype()->end()
104104
->end()
105105
->arrayNode('tags')
106106
->info('A list of tags to be associated to the service.')
107107
->useAttributeAsKey('name')
108108
->treatNullLike([])
109109
->treatFalseLike([])
110-
->prototype('variable')->end()
110+
->variablePrototype()->end()
111111
->end()
112112
->end()
113113
->end()
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 Jose\Bundle\JoseFramework\DependencyInjection\Source\Encryption;
15+
16+
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
17+
use Jose\Component\Checker\HeaderCheckerManagerFactory;
18+
use Jose\Component\Encryption\JWEDecrypterFactory;
19+
use Jose\Component\Signature\JWSVerifierFactory;
20+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
21+
use Symfony\Component\Config\FileLocator;
22+
use Symfony\Component\DependencyInjection\ContainerBuilder;
23+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
24+
25+
class NestedToken implements Source
26+
{
27+
/**
28+
* @var Source[]
29+
*/
30+
private $sources;
31+
32+
/**
33+
* EncryptionSource constructor.
34+
*/
35+
public function __construct()
36+
{
37+
$this->sources = [
38+
new NestedTokenLoader(),
39+
new NestedTokenBuilder(),
40+
];
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function name(): string
47+
{
48+
return 'nested_token';
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function load(array $configs, ContainerBuilder $container)
55+
{
56+
if (!$this->isEnabled()) {
57+
return;
58+
}
59+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
60+
$loader->load('nested_token.yml');
61+
62+
if (array_key_exists('nested_token', $configs)) {
63+
foreach ($this->sources as $source) {
64+
$source->load($configs['nested_token'], $container);
65+
}
66+
}
67+
}
68+
69+
public function getNodeDefinition(NodeDefinition $node)
70+
{
71+
if (!$this->isEnabled()) {
72+
return;
73+
}
74+
$childNode = $node->children()
75+
->arrayNode($this->name())
76+
->treatNullLike([])
77+
->treatFalseLike([]);
78+
79+
foreach ($this->sources as $source) {
80+
$source->getNodeDefinition($childNode);
81+
}
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function prepend(ContainerBuilder $container, array $config): array
88+
{
89+
if (!$this->isEnabled()) {
90+
return [];
91+
}
92+
$result = [];
93+
foreach ($this->sources as $source) {
94+
$prepend = $source->prepend($container, $config);
95+
if (!empty($prepend)) {
96+
$result[$source->name()] = $prepend;
97+
}
98+
}
99+
100+
return $result;
101+
}
102+
103+
/**
104+
* @return bool
105+
*/
106+
private function isEnabled(): bool
107+
{
108+
return class_exists(JWEDecrypterFactory::class)
109+
&& class_exists(JWSVerifierFactory::class)
110+
&& class_exists(HeaderCheckerManagerFactory::class);
111+
}
112+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 Jose\Bundle\JoseFramework\DependencyInjection\Source\Encryption;
15+
16+
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
17+
use Jose\Component\Encryption\NestedTokenBuilderFactory;
18+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
19+
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Definition;
21+
use Symfony\Component\DependencyInjection\Reference;
22+
23+
class NestedTokenBuilder implements Source
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function name(): string
29+
{
30+
return 'builders';
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function load(array $configs, ContainerBuilder $container)
37+
{
38+
foreach ($configs[$this->name()] as $name => $itemConfig) {
39+
$service_id = sprintf('jose.nested_token_builder.%s', $name);
40+
$definition = new Definition(self::class);
41+
$definition
42+
->setFactory([new Reference(NestedTokenBuilderFactory::class), 'create'])
43+
->setArguments([
44+
$itemConfig['jwe_serializers'],
45+
$itemConfig['key_encryption_algorithms'],
46+
$itemConfig['content_encryption_algorithms'],
47+
$itemConfig['compression_methods'],
48+
$itemConfig['jws_serializers'],
49+
$itemConfig['signature_algorithms'],
50+
])
51+
->addTag('jose.nested_token_builder')
52+
->setPublic($itemConfig['is_public']);
53+
foreach ($itemConfig['tags'] as $id => $attributes) {
54+
$definition->addTag($id, $attributes);
55+
}
56+
$container->setDefinition($service_id, $definition);
57+
}
58+
}
59+
60+
public function getNodeDefinition(NodeDefinition $node)
61+
{
62+
$node->children()
63+
->arrayNode($this->name())
64+
->treatNullLike([])
65+
->treatFalseLike([])
66+
->useAttributeAsKey('name')
67+
->arrayPrototype()
68+
->children()
69+
->booleanNode('is_public')
70+
->info('If true, the service will be public, else private.')
71+
->defaultTrue()
72+
->end()
73+
->arrayNode('signature_algorithms')
74+
->info('A list of signature algorithm aliases.')
75+
->useAttributeAsKey('name')
76+
->isRequired()
77+
->scalarPrototype()->end()
78+
->end()
79+
->arrayNode('key_encryption_algorithms')
80+
->info('A list of key encryption algorithm aliases.')
81+
->useAttributeAsKey('name')
82+
->isRequired()
83+
->scalarPrototype()->end()
84+
->end()
85+
->arrayNode('content_encryption_algorithms')
86+
->info('A list of key encryption algorithm aliases.')
87+
->useAttributeAsKey('name')
88+
->isRequired()
89+
->scalarPrototype()->end()
90+
->end()
91+
->arrayNode('compression_methods')
92+
->info('A list of compression method aliases.')
93+
->useAttributeAsKey('name')
94+
->defaultValue(['DEF'])
95+
->scalarPrototype()->end()
96+
->end()
97+
->arrayNode('jws_serializers')
98+
->info('A list of JWS serializer aliases.')
99+
->useAttributeAsKey('name')
100+
->treatNullLike([])
101+
->treatFalseLike([])
102+
->isRequired()
103+
->requiresAtLeastOneElement()
104+
->scalarPrototype()->end()
105+
->end()
106+
->arrayNode('jwe_serializers')
107+
->info('A list of JWE serializer aliases.')
108+
->useAttributeAsKey('name')
109+
->treatNullLike([])
110+
->treatFalseLike([])
111+
->isRequired()
112+
->requiresAtLeastOneElement()
113+
->scalarPrototype()->end()
114+
->end()
115+
->arrayNode('tags')
116+
->info('A list of tags to be associated to the service.')
117+
->useAttributeAsKey('name')
118+
->treatNullLike([])
119+
->treatFalseLike([])
120+
->variablePrototype()->end()
121+
->end()
122+
->end()
123+
->end()
124+
->end();
125+
}
126+
127+
/**
128+
* {@inheritdoc}
129+
*/
130+
public function prepend(ContainerBuilder $container, array $config): array
131+
{
132+
return [];
133+
}
134+
}

0 commit comments

Comments
 (0)