Skip to content

Commit 74dedff

Browse files
committed
Merge branch '2.2' into 2.3
* 2.2: Fix some annotates [FrameworkBundle] made sure that the debug event dispatcher is used everywhere [HttpKernel] remove unneeded strtoupper updated the composer install command to reflect changes in Composer Conflicts: src/Symfony/Component/Console/Application.php src/Symfony/Component/Console/Command/Command.php src/Symfony/Component/Console/Input/InputDefinition.php src/Symfony/Component/CssSelector/Node/CombinedSelectorNode.php src/Symfony/Component/Form/Form.php src/Symfony/Component/HttpKernel/Debug/ErrorHandler.php src/Symfony/Component/HttpKernel/DependencyInjection/RegisterListenersPass.php src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterListenersPassTest.php src/Symfony/Component/Locale/Locale.php src/Symfony/Component/Locale/README.md src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php
1 parent 4a831dc commit 74dedff

File tree

7 files changed

+40
-154
lines changed

7 files changed

+40
-154
lines changed

Authentication/RememberMe/TokenProviderInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ interface TokenProviderInterface
2323
/**
2424
* Loads the active token for the given series.
2525
*
26-
* @throws TokenNotFoundException if the token is not found
27-
*
2826
* @param string $series
2927
*
3028
* @return PersistentTokenInterface
29+
*
30+
* @throws TokenNotFoundException if the token is not found
3131
*/
3232
public function loadTokenBySeries($series);
3333

@@ -44,6 +44,7 @@ public function deleteTokenBySeries($series);
4444
* @param string $series
4545
* @param string $tokenValue
4646
* @param \DateTime $lastUsed
47+
* @throws TokenNotFoundException if the token is not found
4748
*/
4849
public function updateToken($series, $tokenValue, \DateTime $lastUsed);
4950

Authentication/Token/AbstractToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function getUser()
9191
public function setUser($user)
9292
{
9393
if (!($user instanceof UserInterface || (is_object($user) && method_exists($user, '__toString')) || is_string($user))) {
94-
throw new \InvalidArgumentException('$user must be an instanceof of UserInterface, an object implementing a __toString method, or a primitive string.');
94+
throw new \InvalidArgumentException('$user must be an instanceof UserInterface, an object implementing a __toString method, or a primitive string.');
9595
}
9696

9797
if (null === $this->user) {
@@ -190,7 +190,7 @@ public function hasAttribute($name)
190190
}
191191

192192
/**
193-
* Returns a attribute value.
193+
* Returns an attribute value.
194194
*
195195
* @param string $name The attribute name
196196
*

Encoder/BCryptPasswordEncoder.php

Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -12,137 +12,72 @@
1212
namespace Symfony\Component\Security\Core\Encoder;
1313

1414
use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;
15-
use Symfony\Component\Security\Core\Util\SecureRandomInterface;
1615

1716
/**
1817
* @author Elnur Abdurrakhimov <[email protected]>
1918
* @author Terje Bråten <[email protected]>
2019
*/
2120
class BCryptPasswordEncoder extends BasePasswordEncoder
2221
{
23-
/**
24-
* @var SecureRandomInterface
25-
*/
26-
private $secureRandom;
27-
2822
/**
2923
* @var string
3024
*/
3125
private $cost;
3226

33-
private static $prefix = null;
34-
3527
/**
3628
* Constructor.
3729
*
38-
* @param SecureRandomInterface $secureRandom A SecureRandomInterface instance
39-
* @param integer $cost The algorithmic cost that should be used
30+
* @param integer $cost The algorithmic cost that should be used
4031
*
4132
* @throws \InvalidArgumentException if cost is out of range
4233
*/
43-
public function __construct(SecureRandomInterface $secureRandom, $cost)
34+
public function __construct($cost)
4435
{
45-
$this->secureRandom = $secureRandom;
36+
if (!function_exists('password_hash')) {
37+
throw new \RuntimeException('To use the BCrypt encoder, you need to upgrade to PHP 5.5 or install the "ircmaxell/password-compat" via Composer.');
38+
}
4639

4740
$cost = (int) $cost;
4841
if ($cost < 4 || $cost > 31) {
4942
throw new \InvalidArgumentException('Cost must be in the range of 4-31.');
5043
}
51-
$this->cost = sprintf('%02d', $cost);
52-
53-
if (!self::$prefix) {
54-
self::$prefix = '$'.(version_compare(phpversion(), '5.3.7', '>=') ? '2y' : '2a').'$';
55-
}
56-
}
57-
58-
/**
59-
* {@inheritdoc}
60-
*/
61-
public function encodePassword($raw, $salt)
62-
{
63-
if (function_exists('password_hash')) {
64-
return password_hash($raw, PASSWORD_BCRYPT, array('cost' => $this->cost));
65-
}
66-
67-
$salt = self::$prefix.$this->cost.'$'.$this->encodeSalt($this->getRawSalt());
68-
$encoded = crypt($raw, $salt);
69-
if (!is_string($encoded) || strlen($encoded) <= 13) {
70-
return false;
71-
}
72-
73-
return $encoded;
74-
}
75-
76-
/**
77-
* {@inheritdoc}
78-
*/
79-
public function isPasswordValid($encoded, $raw, $salt)
80-
{
81-
if (function_exists('password_verify')) {
82-
return password_verify($raw, $encoded);
83-
}
8444

85-
$crypted = crypt($raw, $encoded);
86-
if (strlen($crypted) <= 13) {
87-
return false;
88-
}
89-
90-
return $this->comparePasswords($encoded, $crypted);
45+
$this->cost = sprintf('%02d', $cost);
9146
}
9247

9348
/**
94-
* Encodes the salt to be used by Bcrypt.
49+
* Encodes the raw password.
9550
*
96-
* The blowfish/bcrypt algorithm used by PHP crypt expects a different
97-
* set and order of characters than the usual base64_encode function.
98-
* Regular b64: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
99-
* Bcrypt b64: ./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
100-
* We care because the last character in our encoded string will
101-
* only represent 2 bits. While two known implementations of
102-
* bcrypt will happily accept and correct a salt string which
103-
* has the 4 unused bits set to non-zero, we do not want to take
104-
* chances and we also do not want to waste an additional byte
105-
* of entropy.
51+
* It doesn't work with PHP versions lower than 5.3.7, since
52+
* the password compat library uses CRYPT_BLOWFISH hash type with
53+
* the "$2y$" salt prefix (which is not available in the early PHP versions).
54+
* @see https://github.com/ircmaxell/password_compat/issues/10#issuecomment-11203833
10655
*
107-
* @param bytes $random a string of 16 random bytes
56+
* It is almost best to **not** pass a salt and let PHP generate one for you.
10857
*
109-
* @return string Properly encoded salt to use with php crypt function
58+
* @param string $raw The password to encode
59+
* @param string $salt The salt
11060
*
111-
* @throws \InvalidArgumentException if string of random bytes is too short
61+
* @return string The encoded password
62+
*
63+
* @link http://lxr.php.net/xref/PHP_5_5/ext/standard/password.c#111
11264
*/
113-
protected function encodeSalt($random)
65+
public function encodePassword($raw, $salt)
11466
{
115-
$len = strlen($random);
116-
if ($len < 16) {
117-
throw new \InvalidArgumentException('The bcrypt salt needs 16 random bytes.');
118-
}
119-
if ($len > 16) {
120-
$random = substr($random, 0, 16);
121-
}
67+
$options = array('cost' => $this->cost);
12268

123-
$base64raw = str_replace('+', '.', base64_encode($random));
124-
$salt128bit = substr($base64raw, 0, 21);
125-
$lastchar = substr($base64raw, 21, 1);
126-
$lastchar = strtr($lastchar, 'AQgw', '.Oeu');
127-
$salt128bit .= $lastchar;
69+
if ($salt) {
70+
$options['salt'] = $salt;
71+
}
12872

129-
return $salt128bit;
73+
return password_hash($raw, PASSWORD_BCRYPT, $options);
13074
}
13175

13276
/**
133-
* @return bytes 16 random bytes to be used in the salt
77+
* {@inheritdoc}
13478
*/
135-
protected function getRawSalt()
79+
public function isPasswordValid($encoded, $raw, $salt)
13680
{
137-
$rawSalt = false;
138-
$numBytes = 16;
139-
if (function_exists('mcrypt_create_iv')) {
140-
$rawSalt = mcrypt_create_iv($numBytes, MCRYPT_DEV_URANDOM);
141-
}
142-
if (!$rawSalt) {
143-
$rawSalt = $this->secureRandom->nextBytes($numBytes);
144-
}
145-
146-
return $rawSalt;
81+
return password_verify($raw, $encoded);
14782
}
14883
}

Encoder/Pbkdf2PasswordEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function hashPbkdf2($algorithm, $password, $salt, $iterations, $length =
8282
$digest = '';
8383

8484
for ($i = 1; $i <= $blocks; $i++) {
85-
$ib = $block = hash_hmac($algorithm, $salt . pack('N', $i), $password, true);
85+
$ib = $block = hash_hmac($algorithm, $salt.pack('N', $i), $password, true);
8686

8787
// Iterations
8888
for ($j = 1; $j < $iterations; $j++) {

User/ChainUserProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function __construct(array $providers)
3131
$this->providers = $providers;
3232
}
3333

34+
/**
35+
* @return array
36+
*/
37+
public function getProviders()
38+
{
39+
return $this->providers;
40+
}
41+
3442
/**
3543
* {@inheritDoc}
3644
*/

Validator/Constraint/UserPassword.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

Validator/Constraint/UserPasswordValidator.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)