Completed
Push — feature/issue-7 ( 9d75a7...1ff64b )
by Mikaël
03:36
created

Password   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 24
dl 0
loc 101
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A convertPassword() 0 7 2
A setTypeValue() 0 5 1
A __construct() 0 9 2
A getTypeValue() 0 3 1
A digestPassword() 0 9 1
1
<?php
2
3
namespace WsdlToPhp\WsSecurity;
4
5
/**
6
 * Class that represents the Password element.
7
 *
8
 * @author WsdlToPhp Team <[email protected]>
9
 */
10
class Password extends Element
11
{
12
    /**
13
     * Element name.
14
     *
15
     * @var string
16
     */
17
    const NAME = 'Password';
18
    /**
19
     * Element attribute type name.
20
     *
21
     * @var string
22
     */
23
    const ATTRIBUTE_TYPE = 'Type';
24
    /**
25
     * Passwor must be sent using digest.
26
     *
27
     * @var string
28
     */
29
    const TYPE_PASSWORD_DIGEST = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest';
30
    /**
31
     * Passwor must be sent in text.
32
     *
33
     * @var string
34
     */
35
    const TYPE_PASSWORD_TEXT = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
36
    /**
37
     * TypeValue of password.
38
     *
39
     * @var string
40
     */
41
    protected $typeValue;
42
43
    /**
44
     * Constructor for Password element.
45
     *
46
     * @param string $password       the password
47
     * @param string $typeValue      the typeValue
48
     * @param string $timestampValue the timestamp to use
49
     * @param string $namespace      the namespace
50
     */
51 54
    public function __construct($password, $typeValue = self::TYPE_PASSWORD_TEXT, $timestampValue = 0, $namespace = self::NS_WSSE)
52
    {
53 27
        $this
54 54
            ->setTypeValue($typeValue)
55 54
            ->setTimestampValue($timestampValue ? $timestampValue : time())
0 ignored issues
show
Bug introduced by
It seems like $timestampValue ? $timestampValue : time() can also be of type string; however, parameter $timestampValue of WsdlToPhp\WsSecurity\Element::setTimestampValue() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

55
            ->setTimestampValue(/** @scrutinizer ignore-type */ $timestampValue ? $timestampValue : time())
Loading history...
56 54
            ->setNonceValue(mt_rand())
57
        ;
58 54
        parent::__construct(self::NAME, $namespace, $this->convertPassword($password), [
59 54
            self::ATTRIBUTE_TYPE => $typeValue,
60 27
        ]);
61 54
    }
62
63
    /**
64
     * Returns the converted form of the password accroding to the password typeValue.
65
     *
66
     * @param string $password
67
     */
68 54
    public function convertPassword($password)
69
    {
70 54
        if (self::TYPE_PASSWORD_DIGEST === $this->getTypeValue()) {
71 6
            $password = $this->digestPassword($password);
72 3
        }
73
74 54
        return $password;
75
    }
76
77
    /**
78
     * When generating the password digest, we define values (nonce and timestamp) that can be used in other place.
79
     *
80
     * @param string $password
81
     */
82 6
    public function digestPassword($password)
83
    {
84 6
        $packedNonce = pack('H*', $this->getNonceValue());
85 6
        $packedTimestamp = pack('a*', $this->getTimestampValue(true));
86 6
        $packedPassword = pack('a*', $password);
87 6
        $hash = sha1($packedNonce . $packedTimestamp . $packedPassword);
88 6
        $packedHash = pack('H*', $hash);
89
90 6
        return base64_encode($packedHash);
91
    }
92
93
    /**
94
     * @return string
95
     */
96 54
    public function getTypeValue()
97
    {
98 54
        return $this->typeValue;
99
    }
100
101
    /**
102
     * @param string $typeValue
103
     *
104
     * @return Password
105
     */
106 54
    public function setTypeValue($typeValue)
107
    {
108 54
        $this->typeValue = $typeValue;
109
110 54
        return $this;
111
    }
112
}
113