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 ( 69ad8e...5f5918 )
by Dmitri
02:42
created

Configuration::apiKeyNode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 73
rs 8.589
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    const STORAGE_FIXED = 'fixed';
17
    const STORAGE_REDIS = 'redis';
18
    const STORAGE_DOCTRINE = 'doctrine';
19
20
    private const SYMMETRIC_ALGOS = ['HS256', 'HS384', 'HS512'];
21
    private const ASYMMETRIC_ALGOS = ['RS256', 'RS384', 'RS512', 'ES256', 'ES384', 'ES512'];
22
23
    public function getConfigTreeBuilder(): TreeBuilder
24
    {
25
        $treeBuilder = new TreeBuilder();
26
27
        /** @var ArrayNodeDefinition $rootNode */
28
        $rootNode = $treeBuilder->root('damax_api_auth');
29
        $rootNode
30
            ->addDefaultsIfNotSet()
31
            ->children()
32
                ->append($this->apiKeyNode('api_key'))
33
                ->append($this->jwtNode('jwt'))
34
                ->append($this->exceptionsNode('format_exceptions'))
35
            ->end()
36
        ;
37
38
        return $treeBuilder;
39
    }
40
41
    private function apiKeyNode(string $name): ArrayNodeDefinition
42
    {
43
        return (new ArrayNodeDefinition($name))
44
            ->canBeEnabled()
45
            ->children()
46
                ->append($this->extractorsNode('extractors', [
47
                    [
48
                        'type' => 'header',
49
                        'name' => 'Authorization',
50
                        'prefix' => 'Token',
51
                    ],
52
                ]))
53
54
                ->enumNode('generator')
55
                    ->values(['fixed', 'random'])
56
                    ->defaultValue('random')
57
                ->end()
58
59
                ->arrayNode('storage')
1 ignored issue
show
Bug introduced by
The method arrayNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

59
                ->/** @scrutinizer ignore-call */ arrayNode('storage')
Loading history...
60
                    ->beforeNormalization()
61
                        ->ifTrue(function (array $config): bool {
62
                            return !isset($config[0]);
63
                        })
64
                        ->then(function (array $config): array {
65
                            return [
66
                                ['type' => self::STORAGE_FIXED, 'tokens' => $config],
67
                            ];
68
                        })
69
                    ->end()
70
                    ->arrayPrototype()
71
                        ->children()
72
                            ->enumNode('type')
73
                                ->isRequired()
74
                                ->values([self::STORAGE_FIXED, self::STORAGE_REDIS, self::STORAGE_DOCTRINE])
75
                            ->end()
76
                            ->arrayNode('tokens')
77
                                ->useAttributeAsKey(true)
78
                                ->requiresAtLeastOneElement()
79
                                ->scalarPrototype()
80
                                    ->isRequired()
81
                                ->end()
82
                            ->end()
83
                            ->booleanNode('writable')
84
                                ->defaultFalse()
85
                            ->end()
86
                            ->scalarNode('redis_client_id')
87
                                ->cannotBeEmpty()
88
                                ->defaultValue('snc_redis.default')
89
                            ->end()
90
                            ->scalarNode('doctrine_connection_id')
91
                                ->cannotBeEmpty()
92
                                ->defaultValue('database_connection')
93
                            ->end()
94
                            ->scalarNode('table_name')
95
                                ->cannotBeEmpty()
96
                            ->end()
97
                            ->arrayNode('fields')
98
                                ->children()
99
                                    ->scalarNode('key')
100
                                        ->cannotBeEmpty()
101
                                    ->end()
102
                                    ->scalarNode('username')
103
                                        ->cannotBeEmpty()
104
                                    ->end()
105
                                    ->scalarNode('expires')
106
                                        ->cannotBeEmpty()
107
                                    ->end()
108
                                ->end()
109
                            ->end()
110
                        ->end()
111
                    ->end()
112
                ->end()
113
            ->end()
114
        ;
115
    }
116
117
    private function jwtNode(string $name): ArrayNodeDefinition
118
    {
119
        return (new ArrayNodeDefinition($name))
120
            ->beforeNormalization()
121
                ->ifString()
122
                ->then(function (string $config): array {
123
                    return ['signer' => $config];
124
                })
125
            ->end()
126
            ->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

126
            ->/** @scrutinizer ignore-call */ canBeEnabled()
Loading history...
127
            ->children()
128
                ->append($this->extractorsNode('extractors', [
129
                    [
130
                        'type' => 'header',
131
                        'name' => 'Authorization',
132
                        'prefix' => 'Bearer',
133
                    ],
134
                ]))
135
                ->scalarNode('identity_claim')
136
                    ->cannotBeEmpty()
137
                ->end()
138
                ->arrayNode('builder')
139
                    ->addDefaultsIfNotSet()
140
                    ->children()
141
                        ->scalarNode('issuer')
142
                            ->cannotBeEmpty()
143
                        ->end()
144
                        ->scalarNode('audience')
145
                            ->cannotBeEmpty()
146
                        ->end()
147
                        ->integerNode('ttl')
148
                            ->defaultValue(3600)
149
                        ->end()
150
                    ->end()
151
                ->end()
152
                ->arrayNode('parser')
153
                    ->children()
154
                        ->arrayNode('issuers')
155
                            ->requiresAtLeastOneElement()
156
                            ->scalarPrototype()
157
                                ->isRequired()
158
                            ->end()
159
                        ->end()
160
                        ->scalarNode('audience')
161
                            ->cannotBeEmpty()
162
                        ->end()
163
                    ->end()
164
                ->end()
165
                ->arrayNode('signer')
166
                    ->isRequired()
167
                    ->beforeNormalization()
168
                        ->ifString()
169
                        ->then(function (string $config): array {
170
                            return ['signing_key' => $config];
171
                        })
172
                    ->end()
173
                    ->beforeNormalization()
174
                        ->ifTrue(function (?array $config): bool {
175
                            $type = $config['type'] ?? self::SIGNER_SYMMETRIC;
176
177
                            return self::SIGNER_ASYMMETRIC === $type;
178
                        })
179
                        ->then(function (array $config): array {
180
                            if (isset($config['signing_key'])) {
181
                                $config['signing_key'] = 'file://' . $config['signing_key'];
182
                            }
183
184
                            if (isset($config['verification_key'])) {
185
                                $config['verification_key'] = 'file://' . $config['verification_key'];
186
                            }
187
188
                            if (!isset($config['algorithm'])) {
189
                                $config['algorithm'] = self::ASYMMETRIC_ALGOS[0];
190
                            }
191
192
                            return $config;
193
                        })
194
                    ->end()
195
                    ->validate()
196
                        ->ifTrue(function (array $config): bool {
197
                            return self::SIGNER_ASYMMETRIC === $config['type'] && empty($config['verification_key']);
198
                        })
199
                        ->thenInvalid('Verification key must be specified for "asymmetric" signer.')
200
                    ->end()
201
                    ->validate()
202
                        ->ifTrue(function (array $config): bool {
203
                            return self::SIGNER_SYMMETRIC === $config['type'] && !in_array($config['algorithm'], self::SYMMETRIC_ALGOS);
204
                        })
205
                        ->thenInvalid('HMAC algorithm must be specified for "symmetric" signer.')
206
                    ->end()
207
                    ->validate()
208
                        ->ifTrue(function (array $config): bool {
209
                            return self::SIGNER_ASYMMETRIC === $config['type'] && !in_array($config['algorithm'], self::ASYMMETRIC_ALGOS);
210
                        })
211
                        ->thenInvalid('RSA or ECDSA algorithm must be specified for "asymmetric" signer.')
212
                    ->end()
213
                    ->validate()
214
                        ->ifTrue(function (array $config): bool {
215
                            if (self::SIGNER_SYMMETRIC === $config['type']) {
216
                                return false;
217
                            }
218
219
                            return !is_readable($config['signing_key']) || !is_readable($config['verification_key']);
220
                        })
221
                        ->thenInvalid('Signing and/or verification key is not readable.')
222
                    ->end()
223
                    ->validate()
224
                        ->ifTrue(function (array $config): bool {
225
                            return self::SIGNER_SYMMETRIC === $config['type'] && !empty($config['verification_key']);
226
                        })
227
                        ->thenInvalid('Verification key must no be specified for "symmetric" signer.')
228
                    ->end()
229
                    ->validate()
230
                        ->ifTrue(function (array $config): bool {
231
                            return self::SIGNER_SYMMETRIC === $config['type'] && !empty($config['passphrase']);
232
                        })
233
                        ->thenInvalid('Passphrase must not be specified for "symmetric" signer.')
234
                    ->end()
235
                    ->children()
236
                        ->enumNode('type')
237
                            ->values(['symmetric', 'asymmetric'])
238
                            ->defaultValue('symmetric')
239
                        ->end()
240
                        ->enumNode('algorithm')
241
                            ->values(array_merge(self::SYMMETRIC_ALGOS, self::ASYMMETRIC_ALGOS))
242
                            ->defaultValue(self::SYMMETRIC_ALGOS[0])
243
                        ->end()
244
                        ->scalarNode('signing_key')
245
                            ->isRequired()
246
                        ->end()
247
                        ->scalarNode('verification_key')
248
                            ->cannotBeEmpty()
249
                        ->end()
250
                        ->scalarNode('passphrase')
251
                            ->cannotBeEmpty()
252
                            ->defaultValue('')
253
                        ->end()
254
                    ->end()
255
                ->end()
256
            ->end()
257
        ;
258
    }
259
260
    private function exceptionsNode(string $name): ArrayNodeDefinition
261
    {
262
        return (new ArrayNodeDefinition($name))
263
            ->canBeEnabled()
264
            ->beforeNormalization()
265
                ->ifString()
266
                ->then(function (string $config): array {
267
                    return ['enabled' => true, 'base_url' => $config];
268
                })
269
            ->end()
270
            ->children()
271
                ->scalarNode('base_url')
272
                    ->defaultValue('/')
273
                ->end()
274
            ->end()
275
        ;
276
    }
277
278
    private function extractorsNode(string $name, array $defaults): ArrayNodeDefinition
279
    {
280
        return (new ArrayNodeDefinition($name))
281
            ->arrayPrototype()
282
                ->children()
283
                    ->enumNode('type')
284
                        ->isRequired()
285
                        ->values(['header', 'query', 'cookie'])
286
                    ->end()
287
                    ->scalarNode('name')
1 ignored issue
show
Bug introduced by
The method scalarNode() does not exist on Symfony\Component\Config...der\NodeParentInterface. It seems like you code against a sub-type of Symfony\Component\Config...der\NodeParentInterface such as Symfony\Component\Config...ion\Builder\NodeBuilder. ( Ignorable by Annotation )

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

287
                    ->/** @scrutinizer ignore-call */ scalarNode('name')
Loading history...
288
                        ->isRequired()
289
                    ->end()
290
                    ->scalarNode('prefix')
291
                        ->cannotBeEmpty()
292
                    ->end()
293
                ->end()
294
            ->end()
295
            ->defaultValue($defaults)
296
        ;
297
    }
298
}
299