-
Notifications
You must be signed in to change notification settings - Fork 59
/
Provider.php
198 lines (169 loc) · 6.57 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace Escape\WSSEAuthenticationBundle\Security\Core\Authentication\Provider;
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\Exception\NonceExpiredException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken as Token;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Common\Cache\Cache;
class Provider implements AuthenticationProviderInterface
{
private $userChecker;
private $userProvider;
private $providerKey;
private $encoder;
private $nonceCache;
private $lifetime;
private $dateFormat;
/**
* Constructor.
*
* @param UserCheckerInterface $userChecker A UserChecketerInterface instance
* @param UserProviderInterface $userProvider An UserProviderInterface instance
* @param string $providerKey The provider key
* @param PasswordEncoderInterface $encoder A PasswordEncoderInterface instance
* @param Cache $nonceCache The nonce cache
* @param int $lifetime The lifetime
* @param string $dateFormat The date format
*/
public function __construct(
UserCheckerInterface $userChecker,
UserProviderInterface $userProvider,
$providerKey,
PasswordEncoderInterface $encoder,
Cache $nonceCache,
$lifetime=300,
$dateFormat='/^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/'
)
{
if(empty($providerKey))
{
throw new \InvalidArgumentException('$providerKey must not be empty.');
}
$this->userChecker = $userChecker;
$this->userProvider = $userProvider;
$this->providerKey = $providerKey;
$this->encoder = $encoder;
$this->nonceCache = $nonceCache;
$this->lifetime = $lifetime;
$this->dateFormat = $dateFormat;
}
public function authenticate(TokenInterface $token)
{
if(!$this->supports($token))
{
return;
}
$user = $this->userProvider->loadUserByUsername($token->getUsername());
if ($user) {
$this->userChecker->checkPreAuth($user);
if ($this->validateDigest(
$token->getCredentials(),
$token->getAttribute('nonce'),
$token->getAttribute('created'),
$this->getSecret($user),
$this->getSalt($user)
)) {
$this->userChecker->checkPostAuth($user);
$authenticatedToken = new Token(
$user,
$token->getCredentials(),
$this->providerKey,
$user->getRoles()
);
return $authenticatedToken;
}
}
throw new AuthenticationException('WSSE authentication failed.');
}
protected function getSecret(UserInterface $user)
{
return $user->getPassword();
}
protected function getSalt(UserInterface $user)
{
return $user->getSalt();
}
protected function validateDigest($digest, $nonce, $created, $secret, $salt)
{
//check whether timestamp is formatted correctly
if(!$this->isFormattedCorrectly($created))
{
throw new BadCredentialsException('Incorrectly formatted "created" in token.');
}
//check whether timestamp is not in the future
if($this->isTokenFromFuture($created))
{
throw new BadCredentialsException('Future token detected.');
}
//expire timestamp after specified lifetime
if(strtotime($this->getCurrentTime()) - strtotime($created) > $this->lifetime)
{
throw new CredentialsExpiredException('Token has expired.');
}
//validate that nonce is unique within specified lifetime
//if it is not, this could be a replay attack
if($this->nonceCache->contains($nonce))
{
throw new NonceExpiredException('Previously used nonce detected.');
}
$this->nonceCache->save($nonce, strtotime($this->getCurrentTime()), $this->lifetime);
//validate secret
$expected = $this->encoder->encodePassword(
sprintf(
'%s%s%s',
base64_decode($nonce),
$created,
$secret
),
$salt
);
if (class_exists('Symfony\Component\Security\Core\Util\StringUtils')) {
return \Symfony\Component\Security\Core\Util\StringUtils::equals($expected, $digest);
} else {
return hash_equals($expected, $digest);
}
}
protected function getCurrentTime()
{
return gmdate(DATE_ISO8601);
}
protected function isTokenFromFuture($created)
{
return strtotime($created) > strtotime($this->getCurrentTime());
}
protected function isFormattedCorrectly($created)
{
return preg_match($this->getDateFormat(), $created);
}
public function getUserProvider()
{
return $this->userProvider;
}
public function getEncoder()
{
return $this->encoder;
}
public function getNonceCache()
{
return $this->nonceCache;
}
public function getLifetime()
{
return $this->lifetime;
}
public function getDateFormat()
{
return $this->dateFormat;
}
public function supports(TokenInterface $token)
{
return $token instanceof Token && $token->hasAttribute('nonce') && $token->hasAttribute('created') && $this->providerKey === $token->getProviderKey();
}
}