Failed Conditions
Pull Request — 4.0 (#4528)
by Kentaro
06:07 queued 35s
created

PasswordEncoder::needsRehash()   A

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
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Security\Core\Encoder;
15
16
use Eccube\Common\EccubeConfig;
17
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
18
19
class PasswordEncoder implements PasswordEncoderInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    public $auth_magic;
25
26
    /**
27
     * @var string
28
     */
29
    public $auth_type;
30
31
    /**
32
     * @var string
33
     */
34
    public $password_hash_algos;
35
36 811
    public function __construct(EccubeConfig $eccubeConfig)
37
    {
38 811
        $this->auth_magic = $eccubeConfig->get('eccube_auth_magic');
39 811
        $this->auth_type = $eccubeConfig->get('eccube_auth_type');
40 811
        $this->password_hash_algos = $eccubeConfig->get('eccube_password_hash_algos');
41
    }
42
43
    /**
44
     * Set Auth Magic.
45
     *
46
     * @param $authMagic
47
     */
48
    public function setAuthMagic($authMagic)
49
    {
50
        $this->auth_magic = $authMagic;
51
    }
52
53
    /**
54
     * Checks a raw password against an encoded password.
55
     *
56
     * @param string $encoded An encoded password
57
     * @param string $raw A raw password
58
     * @param string $salt The salt
59
     *
60
     * @return bool true if the password is valid, false otherwise
61
     */
62 2
    public function isPasswordValid($encoded, $raw, $salt)
63
    {
64 2
        if ($encoded == '') {
65
            return false;
66
        }
67
68 2
        if ($this->auth_type == 'PLAIN') {
69
            if ($raw === $encoded) {
70
                return true;
71
            }
72
        } else {
73
            // 旧バージョン(2.11未満)からの移行を考慮
74 2
            if (empty($salt)) {
75
                $hash = sha1($raw.':'.$this->auth_magic);
76
            } else {
77 2
                $hash = $this->encodePassword($raw, $salt);
78
            }
79
80 2
            if ($hash === $encoded) {
81 2
                return true;
82
            }
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * Encodes the raw password.
90
     *
91
     * @param string $raw The password to encode
92
     * @param string $salt The salt
93
     *
94
     * @return string The encoded password
95
     */
96 524
    public function encodePassword($raw, $salt)
97
    {
98 524
        if ($salt == '') {
99
            $salt = $this->auth_magic;
100
        }
101 524
        if ($this->auth_type == 'PLAIN') {
102
            $res = $raw;
103
        } else {
104 524
            $res = hash_hmac($this->password_hash_algos, $raw.':'.$this->auth_magic, $salt);
105
        }
106
107 524
        return $res;
108
    }
109
110
    /**
111
     * saltを生成する.
112
     *
113
     * @param int $length
114
     *
115
     * @return string
116
     */
117 301
    public function createSalt($length = 5)
118
    {
119 301
        return bin2hex(openssl_random_pseudo_bytes($length));
120
    }
121
122
    public function needsRehash(string $encoded): bool
123
    {
124
         return true;
125
    }
126
}
127