Passed
Push — develop ( 046a3b...2fdc71 )
by Fabian
02:29
created

AbstractAuthentication   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 55
ccs 12
cts 12
cp 1
rs 10
c 2
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptions() 0 3 1
A generateCnonce() 0 14 5
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * Sasl library.
5
 *
6
 * Copyright (c) 2002-2003 Richard Heyes,
7
 *               2014-2021 Fabian Grutschus
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 *
14
 * o Redistributions of source code must retain the above copyright
15
 *   notice, this list of conditions and the following disclaimer.
16
 * o Redistributions in binary form must reproduce the above copyright
17
 *   notice, this list of conditions and the following disclaimer in the
18
 *   documentation and/or other materials provided with the distribution.|
19
 * o The names of the authors may not be used to endorse or promote
20
 *   products derived from this software without specific prior written
21
 *   permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 *
35
 * @author Richard Heyes <[email protected]>
36
 */
37
38
namespace Fabiang\Sasl\Authentication;
39
40
use Fabiang\Sasl\Options;
41
42
/**
43
 * Common functionality to SASL mechanisms
44
 *
45
 * @author Richard Heyes <[email protected]>
46
 */
47
abstract class AbstractAuthentication
48
{
49
50
    /**
51
     * Use random devices.
52
     *
53
     * @var bool
54
     */
55
    public static $useDevRandom = true;
56
57
    /**
58
     * Options object.
59
     *
60
     * @var Options
61
     */
62
    protected $options;
63
64
    /**
65
     *
66
     * @param Options $options
67
     */
68 20
    public function __construct(Options $options)
69
    {
70 20
        $this->options = $options;
71
    }
72
73
    /**
74
     * Get options object.
75
     *
76
     * @return Options
77
     */
78 20
    public function getOptions()
79
    {
80 20
        return $this->options;
81
    }
82
83
    /**
84
     * Creates the client nonce for the response
85
     *
86
     * @return string The cnonce value
87
     */
88 10
    protected function generateCnonce()
89
    {
90 10
        foreach (array('/dev/urandom', '/dev/random') as $file) {
91 10
            if (true === static::$useDevRandom && is_readable($file)) {
92 8
                return base64_encode(file_get_contents($file, false, null, 0, 32));
93
            }
94
        }
95
96 2
        $cnonce = '';
97 2
        for ($i = 0; $i < 32; $i++) {
98 2
            $cnonce .= chr(mt_rand(0, 255));
99
        }
100
101 2
        return base64_encode($cnonce);
102
    }
103
}
104