Completed
Push — master ( 4e360d...80bc96 )
by Daan van
07:27
created

EncryptedAssertion   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 5.45 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 14
c 3
b 1
f 0
lcom 1
cbo 6
dl 6
loc 110
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
C setAssertion() 0 33 7
A getAssertion() 0 8 1
A toXML() 6 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SAML2;
4
5
use RobRichards\XMLSecLibs\XMLSecEnc;
6
use RobRichards\XMLSecLibs\XMLSecurityKey;
7
8
/**
9
 * Class handling encrypted assertions.
10
 *
11
 * @package SimpleSAMLphp
12
 */
13
class EncryptedAssertion
14
{
15
    /**
16
     * The current encrypted assertion.
17
     *
18
     * @var \DOMElement
19
     */
20
    private $encryptedData;
21
22
    /**
23
     * Constructor for SAML 2 encrypted assertions.
24
     *
25
     * @param \DOMElement|null $xml The encrypted assertion XML element.
26
     * @throws \Exception
27
     */
28
    public function __construct(\DOMElement $xml = null)
29
    {
30
        if ($xml === null) {
31
            return;
32
        }
33
34
        $data = Utils::xpQuery($xml, './xenc:EncryptedData');
35
        if (count($data) === 0) {
36
            throw new \Exception('Missing encrypted data in <saml:EncryptedAssertion>.');
37
        } elseif (count($data) > 1) {
38
            throw new \Exception('More than one encrypted data element in <saml:EncryptedAssertion>.');
39
        }
40
        $this->encryptedData = $data[0];
41
    }
42
43
    /**
44
     * Set the assertion.
45
     *
46
     * @param \SAML2\Assertion $assertion The assertion.
47
     * @param XMLSecurityKey  $key       The key we should use to encrypt the assertion.
48
     * @throws \Exception
49
     */
50
    public function setAssertion(Assertion $assertion, XMLSecurityKey $key)
51
    {
52
        $xml = $assertion->toXML();
53
54
        Utils::getContainer()->debugMessage($xml, 'encrypt');
0 ignored issues
show
Documentation introduced by
$xml is of type object<DOMElement>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
56
        $enc = new XMLSecEnc();
57
        $enc->setNode($xml);
58
        $enc->type = XMLSecEnc::Element;
59
60
        switch ($key->type) {
61
            case XMLSecurityKey::TRIPLEDES_CBC:
62
            case XMLSecurityKey::AES128_CBC:
63
            case XMLSecurityKey::AES192_CBC:
64
            case XMLSecurityKey::AES256_CBC:
65
                $symmetricKey = $key;
66
                break;
67
68
            case XMLSecurityKey::RSA_1_5:
69
            case XMLSecurityKey::RSA_OAEP_MGF1P:
70
                $symmetricKey = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
71
                $symmetricKey->generateSessionKey();
72
73
                $enc->encryptKey($key, $symmetricKey);
74
75
                break;
76
77
            default:
78
                throw new \Exception('Unknown key type for encryption: ' . $key->type);
79
        }
80
81
        $this->encryptedData = $enc->encryptNode($symmetricKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like $enc->encryptNode($symmetricKey) of type object<RobRichards\XMLSecLibs\DOMElement> is incompatible with the declared type object<DOMElement> of property $encryptedData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
    }
83
84
    /**
85
     * Retrieve the assertion.
86
     *
87
     * @param  XMLSecurityKey  $inputKey  The key we should use to decrypt the assertion.
88
     * @param  array           $blacklist Blacklisted decryption algorithms.
89
     * @return \SAML2\Assertion The decrypted assertion.
90
     */
91
    public function getAssertion(XMLSecurityKey $inputKey, array $blacklist = array())
92
    {
93
        $assertionXML = Utils::decryptElement($this->encryptedData, $inputKey, $blacklist);
94
95
        Utils::getContainer()->debugMessage($assertionXML, 'decrypt');
0 ignored issues
show
Documentation introduced by
$assertionXML is of type object<DOMElement>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
97
        return new Assertion($assertionXML);
98
    }
99
100
    /**
101
     * Convert this encrypted assertion to an XML element.
102
     *
103
     * @param  \DOMNode|null $parentElement The DOM node the assertion should be created in.
104
     * @return \DOMElement   This encrypted assertion.
105
     */
106
    public function toXML(\DOMNode $parentElement = null)
107
    {
108 View Code Duplication
        if ($parentElement === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $document = DOMDocumentFactory::create();
110
            $parentElement = $document;
111
        } else {
112
            $document = $parentElement->ownerDocument;
113
        }
114
115
        $root = $document->createElementNS(Constants::NS_SAML, 'saml:' . 'EncryptedAssertion');
116
        $parentElement->appendChild($root);
117
118
        $root->appendChild($document->importNode($this->encryptedData, true));
119
120
        return $root;
121
    }
122
}
123