Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 2a1a7a5

Browse files
committed
feature #15141 [DX] [Security] Renamed Token#getKey() to getSecret() (WouterJ)
This PR was squashed before being merged into the 2.8 branch (closes #15141). Discussion ---------- [DX] [Security] Renamed Token#getKey() to getSecret() There are 2 very vague parameter names in the authentication process: `$providerKey` and `$key`. Some tokens/providers have the first one, some tokens/providers the second one and some both. An overview: | Token | `providerKey` | `key` | --- | --- | --- | `AnonymousToken` | - | yes | `PreAuth...Token` | yes | - | `RememberMeToken` | yes | yes | `UsernamePasswordToken` | yes | - Both names are extremely general and their PHPdocs contains pure no-shit-sherlock-descriptions :squirrel: (like "The key."). This made me and @iltar think it's just an inconsistency and they have the same meaning. ...until we dived deeper into the code and came to the conclusion that `$key` has a Security task (while `$providerKey` doesn't really). If it takes people connected to Symfony internals 30+ minutes to find this out, it should be considered for an improvement imo. So here is our suggestion: **Rename `$key` to `$secret`**. This explains much better what the value of the string has to be (for instance, it's important that the string is not easily guessable and cannot be found out, according to the Spring docs). It also explains the usage better (it's used as a replacement for credentials and to hash the RememberMeToken). **Tl;dr**: `$key` and `$providerKey` are too general names, let's improve DX by renaming them. This PR tackles `$key` by renaming it to `$secret`. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - *My excuse for the completely unrelated branch name* Commits ------- 24e0eb6 [DX] [Security] Renamed Token#getKey() to getSecret()
2 parents 93bccca + cba12ee commit 2a1a7a5

17 files changed

+120
-78
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.8.0
5+
-----
6+
7+
* deprecated `getKey()` of the `AnonymousToken`, `RememberMeToken` and `AbstractRememberMeServices` classes
8+
in favor of `getSecret()`.
9+
410
2.7.0
511
-----
612

Core/Authentication/Provider/AnonymousAuthenticationProvider.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@
2222
*/
2323
class AnonymousAuthenticationProvider implements AuthenticationProviderInterface
2424
{
25-
private $key;
25+
/**
26+
* Used to determine if the token is created by the application
27+
* instead of a malicious client.
28+
*
29+
* @var string
30+
*/
31+
private $secret;
2632

2733
/**
2834
* Constructor.
2935
*
30-
* @param string $key The key shared with the authentication token
36+
* @param string $secret The secret shared with the AnonymousToken
3137
*/
32-
public function __construct($key)
38+
public function __construct($secret)
3339
{
34-
$this->key = $key;
40+
$this->secret = $secret;
3541
}
3642

3743
/**
@@ -43,7 +49,7 @@ public function authenticate(TokenInterface $token)
4349
return;
4450
}
4551

46-
if ($this->key !== $token->getKey()) {
52+
if ($this->secret !== $token->getSecret()) {
4753
throw new BadCredentialsException('The Token does not contain the expected key.');
4854
}
4955

Core/Authentication/Provider/RememberMeAuthenticationProvider.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
class RememberMeAuthenticationProvider implements AuthenticationProviderInterface
2020
{
2121
private $userChecker;
22-
private $key;
22+
private $secret;
2323
private $providerKey;
2424

2525
/**
2626
* Constructor.
2727
*
2828
* @param UserCheckerInterface $userChecker An UserCheckerInterface interface
29-
* @param string $key A key
30-
* @param string $providerKey A provider key
29+
* @param string $secret A secret
30+
* @param string $providerKey A provider secret
3131
*/
32-
public function __construct(UserCheckerInterface $userChecker, $key, $providerKey)
32+
public function __construct(UserCheckerInterface $userChecker, $secret, $providerKey)
3333
{
3434
$this->userChecker = $userChecker;
35-
$this->key = $key;
35+
$this->secret = $secret;
3636
$this->providerKey = $providerKey;
3737
}
3838

@@ -45,14 +45,14 @@ public function authenticate(TokenInterface $token)
4545
return;
4646
}
4747

48-
if ($this->key !== $token->getKey()) {
49-
throw new BadCredentialsException('The presented key does not match.');
48+
if ($this->secret !== $token->getSecret()) {
49+
throw new BadCredentialsException('The presented secret does not match.');
5050
}
5151

5252
$user = $token->getUser();
5353
$this->userChecker->checkPreAuth($user);
5454

55-
$authenticatedToken = new RememberMeToken($user, $this->providerKey, $this->key);
55+
$authenticatedToken = new RememberMeToken($user, $this->providerKey, $this->secret);
5656
$authenticatedToken->setAttributes($token->getAttributes());
5757

5858
return $authenticatedToken;

Core/Authentication/Token/AnonymousToken.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@
2020
*/
2121
class AnonymousToken extends AbstractToken
2222
{
23-
private $key;
23+
private $secret;
2424

2525
/**
2626
* Constructor.
2727
*
28-
* @param string $key The key shared with the authentication provider
29-
* @param string $user The user
30-
* @param RoleInterface[] $roles An array of roles
28+
* @param string $secret A secret used to make sure the token is created by the app and not by a malicious client
29+
* @param string $user The user
30+
* @param RoleInterface[] $roles An array of roles
3131
*/
32-
public function __construct($key, $user, array $roles = array())
32+
public function __construct($secret, $user, array $roles = array())
3333
{
3434
parent::__construct($roles);
3535

36-
$this->key = $key;
36+
$this->secret = $secret;
3737
$this->setUser($user);
3838
$this->setAuthenticated(true);
3939
}
@@ -47,29 +47,39 @@ public function getCredentials()
4747
}
4848

4949
/**
50-
* Returns the key.
51-
*
52-
* @return string The Key
50+
* @deprecated Since version 2.8, to be removed in 3.0. Use getSecret() instead.
5351
*/
5452
public function getKey()
5553
{
56-
return $this->key;
54+
@trigger_error(__method__.'() is deprecated since version 2.8 and will be removed in 3.0. Use getSecret() instead.', E_USER_DEPRECATED);
55+
56+
return $this->getSecret();
57+
}
58+
59+
/**
60+
* Returns the secret.
61+
*
62+
* @return string
63+
*/
64+
public function getSecret()
65+
{
66+
return $this->secret;
5767
}
5868

5969
/**
6070
* {@inheritdoc}
6171
*/
6272
public function serialize()
6373
{
64-
return serialize(array($this->key, parent::serialize()));
74+
return serialize(array($this->secret, parent::serialize()));
6575
}
6676

6777
/**
6878
* {@inheritdoc}
6979
*/
7080
public function unserialize($serialized)
7181
{
72-
list($this->key, $parentStr) = unserialize($serialized);
82+
list($this->secret, $parentStr) = unserialize($serialized);
7383
parent::unserialize($parentStr);
7484
}
7585
}

Core/Authentication/Token/RememberMeToken.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,32 @@
2020
*/
2121
class RememberMeToken extends AbstractToken
2222
{
23-
private $key;
23+
private $secret;
2424
private $providerKey;
2525

2626
/**
2727
* Constructor.
2828
*
2929
* @param UserInterface $user
3030
* @param string $providerKey
31-
* @param string $key
31+
* @param string $secret A secret used to make sure the token is created by the app and not by a malicious client
3232
*
3333
* @throws \InvalidArgumentException
3434
*/
35-
public function __construct(UserInterface $user, $providerKey, $key)
35+
public function __construct(UserInterface $user, $providerKey, $secret)
3636
{
3737
parent::__construct($user->getRoles());
3838

39-
if (empty($key)) {
40-
throw new \InvalidArgumentException('$key must not be empty.');
39+
if (empty($secret)) {
40+
throw new \InvalidArgumentException('$secret must not be empty.');
4141
}
4242

4343
if (empty($providerKey)) {
4444
throw new \InvalidArgumentException('$providerKey must not be empty.');
4545
}
4646

4747
$this->providerKey = $providerKey;
48-
$this->key = $key;
48+
$this->secret = $secret;
4949

5050
$this->setUser($user);
5151
parent::setAuthenticated(true);
@@ -64,23 +64,33 @@ public function setAuthenticated($authenticated)
6464
}
6565

6666
/**
67-
* Returns the provider key.
67+
* Returns the provider secret.
6868
*
69-
* @return string The provider key
69+
* @return string The provider secret
7070
*/
7171
public function getProviderKey()
7272
{
7373
return $this->providerKey;
7474
}
7575

7676
/**
77-
* Returns the key.
78-
*
79-
* @return string The Key
77+
* @deprecated Since version 2.8, to be removed in 3.0. Use getSecret() instead.
8078
*/
8179
public function getKey()
8280
{
83-
return $this->key;
81+
@trigger_error(__method__.'() is deprecated since version 2.8 and will be removed in 3.0. Use getSecret() instead.', E_USER_DEPRECATED);
82+
83+
return $this->getSecret();
84+
}
85+
86+
/**
87+
* Returns the secret.
88+
*
89+
* @return string
90+
*/
91+
public function getSecret()
92+
{
93+
return $this->secret;
8494
}
8595

8696
/**
@@ -97,7 +107,7 @@ public function getCredentials()
97107
public function serialize()
98108
{
99109
return serialize(array(
100-
$this->key,
110+
$this->secret,
101111
$this->providerKey,
102112
parent::serialize(),
103113
));
@@ -108,7 +118,7 @@ public function serialize()
108118
*/
109119
public function unserialize($serialized)
110120
{
111-
list($this->key, $this->providerKey, $parentStr) = unserialize($serialized);
121+
list($this->secret, $this->providerKey, $parentStr) = unserialize($serialized);
112122
parent::unserialize($parentStr);
113123
}
114124
}

Core/Tests/Authentication/Provider/AnonymousAuthenticationProviderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testAuthenticateWhenKeyIsNotValid()
3737
{
3838
$provider = $this->getProvider('foo');
3939

40-
$this->assertNull($provider->authenticate($this->getSupportedToken('bar')));
40+
$provider->authenticate($this->getSupportedToken('bar'));
4141
}
4242

4343
public function testAuthenticate()
@@ -50,9 +50,9 @@ public function testAuthenticate()
5050

5151
protected function getSupportedToken($key)
5252
{
53-
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken', array('getKey'), array(), '', false);
53+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken', array('getSecret'), array(), '', false);
5454
$token->expects($this->any())
55-
->method('getKey')
55+
->method('getSecret')
5656
->will($this->returnValue($key))
5757
;
5858

Core/Tests/Authentication/Provider/RememberMeAuthenticationProviderTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public function testAuthenticateWhenTokenIsNotSupported()
3636
/**
3737
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
3838
*/
39-
public function testAuthenticateWhenKeysDoNotMatch()
39+
public function testAuthenticateWhenSecretsDoNotMatch()
4040
{
41-
$provider = $this->getProvider(null, 'key1');
42-
$token = $this->getSupportedToken(null, 'key2');
41+
$provider = $this->getProvider(null, 'secret1');
42+
$token = $this->getSupportedToken(null, 'secret2');
4343

4444
$provider->authenticate($token);
4545
}
@@ -77,7 +77,7 @@ public function testAuthenticate()
7777
$this->assertEquals('', $authToken->getCredentials());
7878
}
7979

80-
protected function getSupportedToken($user = null, $key = 'test')
80+
protected function getSupportedToken($user = null, $secret = 'test')
8181
{
8282
if (null === $user) {
8383
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
@@ -87,7 +87,7 @@ protected function getSupportedToken($user = null, $key = 'test')
8787
->will($this->returnValue(array()));
8888
}
8989

90-
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', array('getProviderKey'), array($user, 'foo', $key));
90+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\RememberMeToken', array('getProviderKey'), array($user, 'foo', $secret));
9191
$token
9292
->expects($this->once())
9393
->method('getProviderKey')

Core/Tests/Authentication/Token/AnonymousTokenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testConstructor()
2828
public function testGetKey()
2929
{
3030
$token = new AnonymousToken('foo', 'bar');
31-
$this->assertEquals('foo', $token->getKey());
31+
$this->assertEquals('foo', $token->getSecret());
3232
}
3333

3434
public function testGetCredentials()

Core/Tests/Authentication/Token/RememberMeTokenTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testConstructor()
2222
$token = new RememberMeToken($user, 'fookey', 'foo');
2323

2424
$this->assertEquals('fookey', $token->getProviderKey());
25-
$this->assertEquals('foo', $token->getKey());
25+
$this->assertEquals('foo', $token->getSecret());
2626
$this->assertEquals(array(new Role('ROLE_FOO')), $token->getRoles());
2727
$this->assertSame($user, $token->getUser());
2828
$this->assertTrue($token->isAuthenticated());
@@ -31,7 +31,7 @@ public function testConstructor()
3131
/**
3232
* @expectedException \InvalidArgumentException
3333
*/
34-
public function testConstructorKeyCannotBeNull()
34+
public function testConstructorSecretCannotBeNull()
3535
{
3636
new RememberMeToken(
3737
$this->getUser(),
@@ -43,7 +43,7 @@ public function testConstructorKeyCannotBeNull()
4343
/**
4444
* @expectedException \InvalidArgumentException
4545
*/
46-
public function testConstructorKeyCannotBeEmptyString()
46+
public function testConstructorSecretCannotBeEmptyString()
4747
{
4848
new RememberMeToken(
4949
$this->getUser(),

0 commit comments

Comments
 (0)