Policy13::getCertificateWSTrustBinding()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 92
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 57
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 92
rs 8.9381

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 SimpleSAML\Module\adfs\Trust;
6
7
use SimpleSAML\WSSecurity\Constants as C;
8
use SimpleSAML\WSSecurity\XML\sp_200702\AlgorithmSuite;
9
use SimpleSAML\WSSecurity\XML\sp_200702\Basic256;
10
use SimpleSAML\WSSecurity\XML\sp_200702\EndorsingSupportingTokens;
11
use SimpleSAML\WSSecurity\XML\sp_200702\Header;
12
use SimpleSAML\WSSecurity\XML\sp_200702\HttpsToken;
13
use SimpleSAML\WSSecurity\XML\sp_200702\IncludeTimestamp;
14
use SimpleSAML\WSSecurity\XML\sp_200702\IncludeToken;
15
use SimpleSAML\WSSecurity\XML\sp_200702\IssuedToken;
16
use SimpleSAML\WSSecurity\XML\sp_200702\KeyValueToken;
17
use SimpleSAML\WSSecurity\XML\sp_200702\Layout;
18
use SimpleSAML\WSSecurity\XML\sp_200702\MustSupportIssuedTokens;
19
use SimpleSAML\WSSecurity\XML\sp_200702\MustSupportRefThumbprint;
20
use SimpleSAML\WSSecurity\XML\sp_200702\RequestSecurityTokenTemplate;
21
use SimpleSAML\WSSecurity\XML\sp_200702\RequireClientEntropy;
22
use SimpleSAML\WSSecurity\XML\sp_200702\RequireInternalReference;
23
use SimpleSAML\WSSecurity\XML\sp_200702\RequireServerEntropy;
24
use SimpleSAML\WSSecurity\XML\sp_200702\RequireThumbprintReference;
25
use SimpleSAML\WSSecurity\XML\sp_200702\SignedEncryptedSupportingTokens;
26
use SimpleSAML\WSSecurity\XML\sp_200702\SignedParts;
27
use SimpleSAML\WSSecurity\XML\sp_200702\Strict;
28
use SimpleSAML\WSSecurity\XML\sp_200702\TransportBinding;
29
use SimpleSAML\WSSecurity\XML\sp_200702\TransportToken;
30
use SimpleSAML\WSSecurity\XML\sp_200702\Trust13;
31
use SimpleSAML\WSSecurity\XML\sp_200702\UsernameToken;
32
use SimpleSAML\WSSecurity\XML\sp_200702\Wss11;
33
use SimpleSAML\WSSecurity\XML\sp_200702\WssUsernameToken10;
34
use SimpleSAML\WSSecurity\XML\sp_200702\WssX509V3Token10;
35
use SimpleSAML\WSSecurity\XML\sp_200702\X509Token;
36
use SimpleSAML\WSSecurity\XML\wsaw\UsingAddressing;
37
use SimpleSAML\WSSecurity\XML\wsp\All;
38
use SimpleSAML\WSSecurity\XML\wsp\ExactlyOne;
39
use SimpleSAML\WSSecurity\XML\wsp\Policy;
40
use SimpleSAML\WSSecurity\XML\wst_200512\CanonicalizationAlgorithm;
41
use SimpleSAML\WSSecurity\XML\wst_200512\EncryptionAlgorithm;
42
use SimpleSAML\WSSecurity\XML\wst_200512\EncryptWith;
43
use SimpleSAML\WSSecurity\XML\wst_200512\KeySize;
44
use SimpleSAML\WSSecurity\XML\wst_200512\KeyType;
45
use SimpleSAML\WSSecurity\XML\wst_200512\KeyTypeEnum;
46
use SimpleSAML\WSSecurity\XML\wst_200512\KeyWrapAlgorithm;
47
use SimpleSAML\WSSecurity\XML\wst_200512\SignatureAlgorithm;
48
use SimpleSAML\XML\Attribute as XMLAttribute;
49
50
/**
51
 * @package simplesamlphp/simplesamlphp-module-adfs
52
 */
53
class Policy13
54
{
55
    /**
56
     * Constructor.
57
     *
58
     * @param \SimpleSAML\Configuration $config The general configuration
59
     * @param \SimpleSAML\Configuration $metadata The metadata configuration
60
     */
61
    public function __construct()
62
    {
63
    }
64
65
66
    /**
67
     * This method builds the wsp:Policy elements
68
     *
69
     * @param \SimpleSAML\WSSecurity\XML\wsp\Policy[]
70
     */
71
    public function getPolicies(): array
72
    {
73
        return [
74
            $this->getCertificateWSTrustBinding(),
75
            $this->getUserNameWSTrustBindingPolicy(),
76
            $this->getIssuedTokenWSTrustBinding(),
77
            $this->getIssuedTokenWSTrustBinding1(),
78
        ];
79
    }
80
81
82
    /**
83
     * This method builds the CertificateWSTrustBinding policy.
84
     *
85
     * @param \SimpleSAML\WSSecurity\XML\wsp\Policy
86
     */
87
    private function getCertificateWSTrustBinding(): Policy
88
    {
89
        $transportBinding = new TransportBinding(
90
            elements: [new Policy(
91
                children: [
92
                    new TransportToken(
93
                        elements: [new Policy(
94
                            children: [new HttpsToken()],
95
                        )],
96
                    ),
97
                    new AlgorithmSuite(
98
                        elements: [new Policy(
99
                            children: [new Basic256()],
100
                        )],
101
                    ),
102
                    new Layout(
103
                        elements: [new Policy(
104
                            children: [new Strict()],
105
                        )],
106
                    ),
107
                    new IncludeTimestamp(),
108
                ],
109
            )],
110
        );
111
112
        $endorsingSupportingTokens = new EndorsingSupportingTokens(
113
            elements: [new Policy(
114
                children: [
115
                    new X509Token(
116
                        elts: [new Policy(
117
                            children: [
118
                                new RequireThumbprintReference(),
119
                                new WssX509V3Token10(),
120
                            ],
121
                        )],
122
                        namespacedAttributes: [
123
                            new XMLAttribute(
124
                                C::NS_SEC_POLICY_12,
125
                                'sp',
126
                                'IncludeToken',
127
                                IncludeToken::AlwaysToRecipient->value,
128
                            ),
129
                        ],
130
                    ),
131
                    new KeyValueToken(
132
                        namespacedAttributes: [
133
                            new XMLAttribute(C::NS_SEC_POLICY_12, 'sp', 'IncludeToken', IncludeToken::Never->value),
134
                            new XMLAttribute(C::NS_POLICY, 'wsp', 'Optional', 'true'),
135
                        ],
136
                    ),
137
                    new SignedParts(
138
                        header: [
139
                            new Header(
140
                                namespace: C::NS_ADDR_200508,
141
                                name: 'To',
142
                            ),
143
                        ],
144
                    ),
145
                ],
146
            )],
147
        );
148
149
        $wss11 = new Wss11(
150
            elements: [new Policy(
151
                children: [
152
                    new MustSupportRefThumbprint(),
153
                ],
154
            )],
155
        );
156
157
        $trust10 = new Trust13(
158
            elements: [new Policy(
159
                children: [
160
                    new MustSupportIssuedTokens(),
161
                    new RequireClientEntropy(),
162
                    new RequireServerEntropy(),
163
                ],
164
            )],
165
        );
166
167
        $usingAddressing = new UsingAddressing();
168
169
        return new Policy(
170
            Id: new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', 'CertificateWSTrustBinding_IWSTrust13Async_policy'),
171
            operatorContent: [new ExactlyOne(
172
                operatorContent: [new All(
173
                    children: [
174
                        $transportBinding,
175
                        $endorsingSupportingTokens,
176
                        $wss11,
177
                        $trust10,
178
                        $usingAddressing,
179
                    ],
180
                )],
181
            )],
182
        );
183
    }
184
185
186
    /**
187
     * This method builds the UserNameWSTrustBinding policy.
188
     *
189
     * @param \SimpleSAML\WSSecurity\XML\wsp\Policy
190
     */
191
    private function getUserNameWSTrustBindingPolicy(): Policy
192
    {
193
        $transportBinding = new TransportBinding(
194
            elements: [new Policy(
195
                children: [
196
                    new TransportToken(
197
                        elements: [new Policy(
198
                            children: [new HttpsToken()],
199
                        )],
200
                    ),
201
                    new AlgorithmSuite(
202
                        elements: [new Policy(
203
                            children: [new Basic256()],
204
                        )],
205
                    ),
206
                    new Layout(
207
                        elements: [new Policy(
208
                            children: [new Strict()],
209
                        )],
210
                    ),
211
                    new IncludeTimestamp(),
212
                ],
213
            )],
214
        );
215
216
        $signedEncryptedSupportingTokens = new SignedEncryptedSupportingTokens(
217
            elements: [new Policy(
218
                children: [new UsernameToken(
219
                    elts: [new Policy(
220
                        children: [new WssUsernameToken10()],
221
                    )],
222
                    namespacedAttributes: [
223
                        new XMLAttribute(
224
                            C::NS_SEC_POLICY_12,
225
                            'sp',
226
                            'IncludeToken',
227
                            IncludeToken::AlwaysToRecipient->value,
228
                        ),
229
                    ],
230
                )],
231
            )],
232
        );
233
234
        $endorsingSupportingTokens = new EndorsingSupportingTokens(
235
            elements: [new Policy(
236
                children: [
237
                    new KeyValueToken(
238
                        namespacedAttributes: [
239
                            new XMLAttribute(C::NS_SEC_POLICY_12, 'sp', 'IncludeToken', IncludeToken::Never->value),
240
                            new XMLAttribute(C::NS_POLICY, 'wsp', 'Optional', 'true'),
241
                        ],
242
                    ),
243
                    new SignedParts(
244
                        header: [
245
                            new Header(
246
                                namespace: C::NS_ADDR_200508,
247
                                name: 'To',
248
                            ),
249
                        ],
250
                    ),
251
                ],
252
            )],
253
        );
254
255
        $wss11 = new Wss11(
256
            elements: [new Policy()],
257
        );
258
259
        $trust13 = new Trust13(
260
            elements: [new Policy(
261
                children: [
262
                    new MustSupportIssuedTokens(),
263
                    new RequireClientEntropy(),
264
                    new RequireServerEntropy(),
265
                ],
266
            )],
267
        );
268
269
        $usingAddressing = new UsingAddressing();
270
271
        return new Policy(
272
            Id: new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', 'UserNameWSTrustBinding_IWSTrustFeb2005Async_policy'),
273
            operatorContent: [new ExactlyOne(
274
                operatorContent: [new All(
275
                    children: [
276
                        $transportBinding,
277
                        $signedEncryptedSupportingTokens,
278
                        $endorsingSupportingTokens,
279
                        $wss11,
280
                        $trust13,
281
                        $usingAddressing,
282
                    ],
283
                )],
284
            )],
285
        );
286
    }
287
288
289
    /**
290
     * This method builds the IssuedTokenWSTrustBinding policy.
291
     *
292
     * @param \SimpleSAML\WSSecurity\XML\wsp\Policy
293
     */
294
    private function getIssuedTokenWSTrustBinding(): Policy
295
    {
296
        $transportBinding = new TransportBinding(
297
            elements: [new Policy(
298
                children: [
299
                    new TransportToken(
300
                        elements: [new Policy(
301
                            children: [new HttpsToken()],
302
                        )],
303
                    ),
304
                    new AlgorithmSuite(
305
                        elements: [new Policy(
306
                            children: [new Basic256()],
307
                        )],
308
                    ),
309
                    new Layout(
310
                        elements: [new Policy(
311
                            children: [new Strict()],
312
                        )],
313
                    ),
314
                    new IncludeTimestamp(),
315
                ],
316
            )],
317
        );
318
319
        $endorsingSupportingTokens = new EndorsingSupportingTokens(
320
            elements: [new Policy(
321
                children: [
322
                    new IssuedToken(
323
                        requestSecurityTokenTemplate: new RequestSecurityTokenTemplate(
324
                            elts: [
325
                                new KeyType([KeyTypeEnum::PublicKey]),
326
                                new KeyWrapAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P),
327
                                new EncryptWith(C::KEY_TRANSPORT_OAEP_MGF1P),
328
                                new SignatureAlgorithm(C::SIG_RSA_SHA1),
329
                                new CanonicalizationAlgorithm(C::C14N_EXCLUSIVE_WITHOUT_COMMENTS),
330
                                new EncryptionAlgorithm(C::BLOCK_ENC_AES256),
331
                            ],
332
                        ),
333
                        elts: [
334
                            new Policy(
335
                                children: [
336
                                    new RequireInternalReference(),
337
                                ],
338
                            ),
339
                        ],
340
                        namespacedAttributes: [
341
                            new XMLAttribute(
342
                                C::NS_SEC_POLICY_12,
343
                                'sp',
344
                                'IncludeToken',
345
                                IncludeToken::AlwaysToRecipient->value,
346
                            ),
347
                        ],
348
                    ),
349
                    new KeyValueToken(
350
                        namespacedAttributes: [
351
                            new XMLAttribute(C::NS_SEC_POLICY_12, 'sp', 'IncludeToken', IncludeToken::Never->value),
352
                            new XMLAttribute(C::NS_POLICY, 'wsp', 'Optional', 'true'),
353
                        ],
354
                    ),
355
                    new SignedParts(
356
                        header: [
357
                            new Header(
358
                                namespace: C::NS_ADDR_200508,
359
                                name: 'To',
360
                            ),
361
                        ],
362
                    ),
363
                ],
364
            )],
365
        );
366
367
        $wss11 = new Wss11(
368
            elements: [new Policy(
369
            )],
370
        );
371
372
        $trust13 = new Trust13(
373
            elements: [new Policy(
374
                children: [
375
                    new MustSupportIssuedTokens(),
376
                    new RequireClientEntropy(),
377
                    new RequireServerEntropy(),
378
                ],
379
            )],
380
        );
381
382
        $usingAddressing = new UsingAddressing();
383
384
        return new Policy(
385
            Id: new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', 'IssuedTokenWSTrustBinding_IWSTrust13Async_policy'),
386
            operatorContent: [new ExactlyOne(
387
                operatorContent: [new All(
388
                    children: [
389
                        $transportBinding,
390
                        $endorsingSupportingTokens,
391
                        $wss11,
392
                        $trust13,
393
                        $usingAddressing,
394
                    ],
395
                )],
396
            )],
397
        );
398
    }
399
400
401
    /**
402
     * This method builds the IssuedTokenWSTrustBinding1 policy.
403
     *
404
     * @param \SimpleSAML\WSSecurity\XML\wsp\Policy
405
     */
406
    private function getIssuedTokenWSTrustBinding1(): Policy
407
    {
408
        $transportBinding = new TransportBinding(
409
            elements: [new Policy(
410
                children: [
411
                    new TransportToken(
412
                        elements: [new Policy(
413
                            children: [new HttpsToken()],
414
                        )],
415
                    ),
416
                    new AlgorithmSuite(
417
                        elements: [new Policy(
418
                            children: [new Basic256()],
419
                        )],
420
                    ),
421
                    new Layout(
422
                        elements: [new Policy(
423
                            children: [new Strict()],
424
                        )],
425
                    ),
426
                    new IncludeTimestamp(),
427
                ],
428
            )],
429
        );
430
431
        $endorsingSupportingTokens = new EndorsingSupportingTokens(
432
            elements: [new Policy(
433
                children: [
434
                    new IssuedToken(
435
                        requestSecurityTokenTemplate: new RequestSecurityTokenTemplate(
436
                            elts: [
437
                                new KeyType([KeyTypeEnum::SymmetricKey]),
438
                                new KeySize('256'),
439
                                new KeyWrapAlgorithm(C::KEY_TRANSPORT_OAEP_MGF1P),
440
                                new EncryptWith(C::BLOCK_ENC_AES256),
441
                                new SignatureAlgorithm(C::SIG_HMAC_SHA1),
442
                                new CanonicalizationAlgorithm(C::C14N_EXCLUSIVE_WITHOUT_COMMENTS),
443
                                new EncryptionAlgorithm(C::BLOCK_ENC_AES256),
444
                            ],
445
                        ),
446
                        elts: [
447
                            new Policy(
448
                                children: [
449
                                    new RequireInternalReference(),
450
                                ],
451
                            ),
452
                        ],
453
                        namespacedAttributes: [
454
                            new XMLAttribute(
455
                                C::NS_SEC_POLICY_12,
456
                                'sp',
457
                                'IncludeToken',
458
                                IncludeToken::AlwaysToRecipient->value,
459
                            ),
460
                        ],
461
                    ),
462
                    new KeyValueToken(
463
                        namespacedAttributes: [
464
                            new XMLAttribute(C::NS_SEC_POLICY_12, 'sp', 'IncludeToken', IncludeToken::Never->value),
465
                            new XMLAttribute(C::NS_POLICY, 'wsp', 'Optional', 'true'),
466
                        ],
467
                    ),
468
                    new SignedParts(
469
                        header: [
470
                            new Header(
471
                                namespace: C::NS_ADDR_200508,
472
                                name: 'To',
473
                            ),
474
                        ],
475
                    ),
476
                ],
477
            )],
478
        );
479
480
        $wss11 = new Wss11(
481
            elements: [new Policy(
482
            )],
483
        );
484
485
        $trust13 = new Trust13(
486
            elements: [new Policy(
487
                children: [
488
                    new MustSupportIssuedTokens(),
489
                    new RequireClientEntropy(),
490
                    new RequireServerEntropy(),
491
                ],
492
            )],
493
        );
494
495
        $usingAddressing = new UsingAddressing();
496
497
        return new Policy(
498
            Id: new XMLAttribute(C::NS_SEC_UTIL, 'wsu', 'Id', 'IssuedTokenWSTrustBinding_IWSTrust13Async1_policy'),
499
            operatorContent: [new ExactlyOne(
500
                operatorContent: [new All(
501
                    children: [
502
                        $transportBinding,
503
                        $endorsingSupportingTokens,
504
                        $wss11,
505
                        $trust13,
506
                        $usingAddressing,
507
                    ],
508
                )],
509
            )],
510
        );
511
    }
512
}
513