SignatureStringReader::setAlgorithm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Model\XmlDSig;
13
14
use LightSaml\Error\LightSamlSecurityException;
15
use LightSaml\Model\Context\DeserializationContext;
16
use LightSaml\Model\Context\SerializationContext;
17
use RobRichards\XMLSecLibs\XMLSecurityKey;
18
19
class SignatureStringReader extends AbstractSignatureReader
20
{
21
    /** @var string */
22
    protected $signature;
23
24
    /** @var string */
25
    protected $algorithm;
26
27
    /** @var string */
28
    protected $data;
29
30
    /**
31
     * @param string|null $signature
32
     * @param string|null $algorithm
33
     * @param string|null $data
34
     */
35 11
    public function __construct($signature = null, $algorithm = null, $data = null)
36
    {
37 11
        $this->signature = $signature;
38 11
        $this->algorithm = $algorithm;
39 11
        $this->data = $data;
40 11
    }
41
42
    /**
43
     * @param string $algorithm
44
     */
45
    public function setAlgorithm($algorithm)
46
    {
47
        $this->algorithm = (string) $algorithm;
48
    }
49
50
    /**
51
     * @return string
52
     */
53 2
    public function getAlgorithm()
54
    {
55 2
        return $this->algorithm;
56
    }
57
58
    /**
59
     * @param string $data
60
     */
61
    public function setData($data)
62
    {
63
        $this->data = (string) $data;
64
    }
65
66
    /**
67
     * @return string
68
     */
69 1
    public function getData()
70
    {
71 1
        return $this->data;
72
    }
73
74
    /**
75
     * @param string $signature
76
     */
77
    public function setSignature($signature)
78
    {
79
        $this->signature = (string) $signature;
80
    }
81
82
    /**
83
     * @return string
84
     */
85 3
    public function getSignature()
86
    {
87 3
        return $this->signature;
88
    }
89
90
    /**
91
     * @param XMLSecurityKey $key
92
     *
93
     * @return bool True if validated, False if validation was not performed
94
     *
95
     * @throws LightSamlSecurityException If validation fails
96
     */
97 2
    public function validate(XMLSecurityKey $key)
98
    {
99 2
        if (null == $this->getSignature()) {
100 1
            return false;
101
        }
102
103 1
        $key = $this->castKeyIfNecessary($key);
104
105 1
        $signature = base64_decode($this->getSignature());
106
107 1
        if (false == $key->verifySignature($this->getData(), $signature)) {
108
            throw new LightSamlSecurityException('Unable to validate signature on query string');
109
        }
110
111 1
        return true;
112
    }
113
114
    /**
115
     * @param \DOMNode             $parent
116
     * @param SerializationContext $context
117
     *
118
     * @throws \LogicException
119
     */
120 1
    public function serialize(\DOMNode $parent, SerializationContext $context)
121
    {
122 1
        throw new \LogicException('SignatureStringReader can not be serialized');
123
    }
124
125
    /**
126
     * @param \DOMNode               $node
127
     * @param DeserializationContext $context
128
     */
129 1
    public function deserialize(\DOMNode $node, DeserializationContext $context)
130
    {
131 1
        throw new \LogicException('SignatureStringReader can not be deserialized');
132
    }
133
}
134