-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Feature: Clockskew #84
base: master
Are you sure you want to change the base?
Changes from 5 commits
2a4f4e2
d544bfd
8467696
756d38f
323f810
0b1c36e
0611766
e6cead3
7be6b9e
a7c39f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ class Provider implements AuthenticationProviderInterface | |
private $nonceCache; | ||
private $lifetime; | ||
private $dateFormat; | ||
private $clockSkew; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -36,15 +37,17 @@ class Provider implements AuthenticationProviderInterface | |
* @param Cache $nonceCache The nonce cache | ||
* @param int $lifetime The lifetime | ||
* @param string $dateFormat The date format | ||
*/ | ||
* @param int $clockSkew The margin in seconds to mitigate clock skew | ||
*/ | ||
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)?)?)?)?$/' | ||
$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)?)?)?)?$/', | ||
$clockSkew = 0 | ||
) | ||
{ | ||
if(empty($providerKey)) | ||
|
@@ -59,6 +62,7 @@ public function __construct( | |
$this->nonceCache = $nonceCache; | ||
$this->lifetime = $lifetime; | ||
$this->dateFormat = $dateFormat; | ||
$this->clockSkew = $clockSkew; | ||
} | ||
|
||
public function authenticate(TokenInterface $token) | ||
|
@@ -119,7 +123,7 @@ protected function validateDigest($digest, $nonce, $created, $secret, $salt) | |
} | ||
|
||
//expire timestamp after specified lifetime | ||
if(strtotime($this->getCurrentTime()) - strtotime($created) > $this->lifetime) | ||
if(strtotime($this->getCurrentTime()) - strtotime($created) > ($this->lifetime - $this->clockSkew)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant the lifetime here |
||
{ | ||
throw new CredentialsExpiredException('Token has expired.'); | ||
} | ||
|
@@ -158,7 +162,7 @@ protected function getCurrentTime() | |
|
||
protected function isTokenFromFuture($created) | ||
{ | ||
return strtotime($created) > strtotime($this->getCurrentTime()); | ||
return strtotime($created) - $this->clockSkew > strtotime($this->getCurrentTime()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we take clock skew (offset) into consideration upon determination whether the token is from the future (by subtracting the skew from the created time), shouldn't we also take the same clock skew (offset) into consideration when determining whether the token is valid (by adding the skew to the created time)? Perhaps extracting https://github.com/hostage-nl/EscapeWSSEAuthenticationBundle/blob/0b1c36e8151049aba7beeaa27045dd19ca30d65d/Security/Core/Authentication/Provider/Provider.php#L125-L129 into a isTokenExpired-method, where clock skew is used as follows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, i initially implemented something like this, but the opposite. Your solution would limit the lifetime with correctly synced clocks. I considered the clock skew to be a margin of acceptable error, the allowed time difference between the client clock and the systems clock. So the clock skew would have to be added to the current time, so that a client clock which would be in de past, would have an extended lifetime of clock skew seconds. Then i thought, is this really a nice to have feature? For a client clock being set to 29 secs in to the future and a clock skew setting of 30 secs, this would mean extending the lifetime by 59 seconds, which didn't feel like something that would be desirable. I came to the conclusion it would be better to determine the clock skew automatically by determining the difference between the "Created" timestamp of the requesting WSSE header and the servers current time. I was confused about if it would be possible to create something like this, and because the project i was working on was fixed by my patch, i moved on and forgot about it. So for now i would suggest not using the clock skew for expiration time and maybe file a feature request for dynamic determination of the clock skew. |
||
} | ||
|
||
protected function isFormattedCorrectly($created) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ public function testCreate() | |
$profile = 'someprofile'; | ||
$lifetime = 300; | ||
$date_format = '/^([\+-]?\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)?)?)?)?$/'; | ||
$clock_skew = 60; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest using the default (0) here in the existing tests, but adding some specific tests for the clock skew feature. |
||
|
||
$algorithm = 'sha1'; | ||
$encodeHashAsBase64 = true; | ||
|
@@ -68,7 +69,8 @@ public function testCreate() | |
'profile' => $profile, | ||
'encoder' => $encoder, | ||
'lifetime' => $lifetime, | ||
'date_format' => $date_format | ||
'date_format' => $date_format, | ||
'clock_skew' => $clock_skew | ||
), | ||
'user_provider', | ||
'entry_point' | ||
|
@@ -108,7 +110,8 @@ public function testCreate() | |
'index_3' => new Reference($encoderId), | ||
'index_4' => new Reference($nonceCacheId), | ||
'index_5' => $lifetime, | ||
'index_6' => $date_format | ||
'index_6' => $date_format, | ||
'index_7' => $clock_skew | ||
), | ||
$definition->getArguments() | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you make sure to set a default value of 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, good idea, keeping the behaviour the same as without these changes.