GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( a79938...c2e7a4 )
by Dmitri
02:00
created

Configuration::exceptionsNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\DependencyInjection;
6
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9
use Symfony\Component\Config\Definition\ConfigurationInterface;
10
11
class Configuration implements ConfigurationInterface
12
{
13
    const SIGNER_SYMMETRIC = 'symmetric';
14
    const SIGNER_ASYMMETRIC = 'asymmetric';
15
16
    private const SYMMETRIC_ALGOS = ['HS256', 'HS384', 'HS512'];
17
    private const ASYMMETRIC_ALGOS = ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512'];
18
19
    public function getConfigTreeBuilder(): TreeBuilder
20
    {
21
        $treeBuilder = new TreeBuilder();
22
23
        /** @var ArrayNodeDefinition $rootNode */
24
        $rootNode = $treeBuilder->root('damax_api_auth');
25
        $rootNode
26
            ->addDefaultsIfNotSet()
27
            ->children()
28
                ->append($this->apiKeyNode('api_key'))
29
                ->append($this->jwtNode('jwt'))
30
                ->append($this->exceptionsNode('format_exceptions'))
31
            ->end()
32
        ;
33
34
        return $treeBuilder;
35
    }
36
37
    private function apiKeyNode(string $name): ArrayNodeDefinition
38
    {
39
        return (new ArrayNodeDefinition($name))
40
            ->canBeEnabled()
41
            ->beforeNormalization()
42
                ->ifTrue(function (array $config): bool {
43
                    return !isset($config['tokens']);
44
                })
45
                ->then(function (array $config): array {
46
                    $enabled = $config['enabled'];
47
48
                    unset($config['enabled']);
49
50
                    return ['enabled' => $enabled, 'tokens' => $config];
51
                })
52
            ->end()
53
            ->children()
54
                ->arrayNode('tokens')
55
                    ->useAttributeAsKey(true)
56
                    ->requiresAtLeastOneElement()
57
                    ->prototype('scalar')->isRequired()->end()
58
                ->end()
59
                ->append($this->extractorsNode('extractors', [
60
                    [
61
                        'type' => 'header',
62
                        'name' => 'Authorization',
63
                        'prefix' => 'Token',
64
                    ],
65
                ]))
66
            ->end()
67
        ;
68
    }
69
70
    private function jwtNode(string $name): ArrayNodeDefinition
71
    {
72
        return (new ArrayNodeDefinition($name))
73
            ->beforeNormalization()
74
                ->ifString()
75
                ->then(function (string $config): array {
76
                    return ['signer' => $config];
77
                })
78
            ->end()
79
            ->canBeEnabled()
1 ignored issue
show
Bug introduced by
The method canBeEnabled() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. It seems like you code against a sub-type of Symfony\Component\Config...\Builder\NodeDefinition such as Symfony\Component\Config...der\ArrayNodeDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            ->/** @scrutinizer ignore-call */ canBeEnabled()
Loading history...
80
            ->children()
81
                ->append($this->extractorsNode('extractors', [
82
                    [
83
                        'type' => 'header',
84
                        'name' => 'Authorization',
85
                        'prefix' => 'Bearer',
86
                    ],
87
                ]))
88
                ->scalarNode('identity_claim')
89
                    ->cannotBeEmpty()
90
                ->end()
91
                ->arrayNode('builder')
92
                    ->addDefaultsIfNotSet()
93
                    ->children()
94
                        ->scalarNode('issuer')
95
                            ->cannotBeEmpty()
96
                        ->end()
97
                        ->scalarNode('audience')
98
                            ->cannotBeEmpty()
99
                        ->end()
100
                        ->integerNode('ttl')
101
                            ->defaultValue(3600)
102
                        ->end()
103
                    ->end()
104
                ->end()
105
                ->arrayNode('parser')
106
                    ->children()
107
                        ->arrayNode('issuers')
108
                            ->requiresAtLeastOneElement()
109
                            ->prototype('scalar')
110
                                ->isRequired()
111
                            ->end()
112
                        ->end()
113
                        ->scalarNode('audience')
114
                            ->cannotBeEmpty()
115
                        ->end()
116
                    ->end()
117
                ->end()
118
                ->arrayNode('signer')
119
                    ->isRequired()
120
                    ->beforeNormalization()
121
                        ->ifString()
122
                        ->then(function (string $config): array {
123
                            return ['signing_key' => $config];
124
                        })
125
                    ->end()
126
                    ->beforeNormalization()
127
                        ->ifTrue(function (?array $config): bool {
128
                            $type = $config['type'] ?? self::SIGNER_SYMMETRIC;
129
130
                            return self::SIGNER_ASYMMETRIC === $type;
131
                        })
132
                        ->then(function (array $config): array {
133
                            if (isset($config['signing_key'])) {
134
                                $config['signing_key'] = 'file://' . $config['signing_key'];
135
                            }
136
137
                            if (isset($config['verification_key'])) {
138
                                $config['verification_key'] = 'file://' . $config['verification_key'];
139
                            }
140
141
                            if (!isset($config['algorithm'])) {
142
                                $config['algorithm'] = self::ASYMMETRIC_ALGOS[0];
143
                            }
144
145
                            return $config;
146
                        })
147
                    ->end()
148
                    ->validate()
149
                        ->ifTrue(function (array $config): bool {
150
                            return self::SIGNER_ASYMMETRIC === $config['type'] && empty($config['verification_key']);
151
                        })
152
                        ->thenInvalid('Verification key must be specified for "asymmetric" signer.')
153
                    ->end()
154
                    ->validate()
155
                        ->ifTrue(function (array $config): bool {
156
                            return self::SIGNER_SYMMETRIC === $config['type'] && !in_array($config['algorithm'], self::SYMMETRIC_ALGOS);
157
                        })
158
                        ->thenInvalid('HMAC algorithm must be specified for "symmetric" signer.')
159
                    ->end()
160
                    ->validate()
161
                        ->ifTrue(function (array $config): bool {
162
                            return self::SIGNER_ASYMMETRIC === $config['type'] && !in_array($config['algorithm'], self::ASYMMETRIC_ALGOS);
163
                        })
164
                        ->thenInvalid('RSA or ECDSA algorithm must be specified for "asymmetric" signer.')
165
                    ->end()
166
                    ->validate()
167
                        ->ifTrue(function (array $config): bool {
168
                            if (self::SIGNER_SYMMETRIC === $config['type']) {
169
                                return false;
170
                            }
171
172
                            return !is_readable($config['signing_key']) || !is_readable($config['verification_key']);
173
                        })
174
                        ->thenInvalid('Signing and/or verification key is not readable.')
175
                    ->end()
176
                    ->validate()
177
                        ->ifTrue(function (array $config): bool {
178
                            return self::SIGNER_SYMMETRIC === $config['type'] && !empty($config['verification_key']);
179
                        })
180
                        ->thenInvalid('Verification key must no be specified for "symmetric" signer.')
181
                    ->end()
182
                    ->validate()
183
                        ->ifTrue(function (array $config): bool {
184
                            return self::SIGNER_SYMMETRIC === $config['type'] && !empty($config['passphrase']);
185
                        })
186
                        ->thenInvalid('Passphrase must not be specified for "symmetric" signer.')
187
                    ->end()
188
                    ->children()
189
                        ->enumNode('type')
190
                            ->values(['symmetric', 'asymmetric'])
191
                            ->defaultValue('symmetric')
192
                        ->end()
193
                        ->enumNode('algorithm')
194
                            ->values(array_merge(self::SYMMETRIC_ALGOS, self::ASYMMETRIC_ALGOS))
195
                            ->defaultValue(self::SYMMETRIC_ALGOS[0])
196
                        ->end()
197
                        ->scalarNode('signing_key')
198
                            ->isRequired()
199
                        ->end()
200
                        ->scalarNode('verification_key')
201
                            ->cannotBeEmpty()
202
                        ->end()
203
                        ->scalarNode('passphrase')
204
                            ->cannotBeEmpty()
205
                            ->defaultValue('')
206
                        ->end()
207
                    ->end()
208
                ->end()
209
            ->end()
210
        ;
211
    }
212
213
    private function exceptionsNode(string $name): ArrayNodeDefinition
214
    {
215
        return (new ArrayNodeDefinition($name))
216
            ->canBeEnabled()
217
            ->beforeNormalization()
218
                ->ifString()
219
                ->then(function (string $config): array {
220
                    return ['enabled' => true, 'base_url' => $config];
221
                })
222
            ->end()
223
            ->children()
224
                ->scalarNode('base_url')
225
                    ->defaultValue('/')
226
                ->end()
227
            ->end()
228
        ;
229
    }
230
231
    private function extractorsNode(string $name, array $defaults): ArrayNodeDefinition
232
    {
233
        return (new ArrayNodeDefinition($name))
234
            ->prototype('array')
235
                ->children()
236
                    ->enumNode('type')
237
                        ->isRequired()
238
                        ->values(['header', 'query', 'cookie'])
239
                    ->end()
240
                    ->scalarNode('name')
241
                        ->isRequired()
242
                    ->end()
243
                    ->scalarNode('prefix')
244
                        ->cannotBeEmpty()
245
                    ->end()
246
                ->end()
247
            ->end()
248
            ->defaultValue($defaults)
249
        ;
250
    }
251
}
252