Passed
Branch refactoring (9be877)
by Fabian
16:03
created

CramMD5::createResponse()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Sasl library.
7
 *
8
 * Copyright (c) 2002-2003 Richard Heyes,
9
 *               2014-2025 Fabian Grutschus
10
 * All rights reserved.
11
 *
12
 * Redistribution and use in source and binary forms, with or without
13
 * modification, are permitted provided that the following conditions
14
 * are met:
15
 *
16
 * o Redistributions of source code must retain the above copyright
17
 *   notice, this list of conditions and the following disclaimer.
18
 * o Redistributions in binary form must reproduce the above copyright
19
 *   notice, this list of conditions and the following disclaimer in the
20
 *   documentation and/or other materials provided with the distribution.|
21
 * o The names of the authors may not be used to endorse or promote
22
 *   products derived from this software without specific prior written
23
 *   permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @author Richard Heyes <[email protected]>
38
 */
39
40
namespace Fabiang\SASL\Authentication;
41
42
use Fabiang\SASL\Authentication\AbstractAuthentication;
43
44
/**
45
 * Implmentation of CRAM-MD5 SASL mechanism
46
 *
47
 * @author Richard Heyes <[email protected]>
48
 */
49
class CramMD5 extends AbstractAuthentication implements ChallengeAuthenticationInterface
50
{
51
    /**
52
     * Implements the CRAM-MD5 SASL mechanism
53
     * This DOES NOT base64 encode the return value,
54
     * you will need to do that yourself.
55
     *
56
     * @param string|null $challenge The challenge supplied by the server.
57
     *                          this should be already base64_decoded.
58
     *
59
     * @return string|false The string to pass back to the server, of the form
60
     *                "<user> <digest>". This is NOT base64_encoded.
61
     */
62
    public function createResponse(?string $challenge = null): string|false
63
    {
64
        $authcid = $this->options->getAuthcid();
65
        $secret  = $this->options->getSecret();
66
67
        if ($authcid === null || $secret === null || $challenge === null
68
            || $authcid === '' || $secret === '' || $challenge === '') {
69
            return false;
70
        }
71
72
        return $authcid . ' ' . hash_hmac('md5', $challenge, $secret);
73
    }
74
}
75