-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate the cookie value authentication time
The cookie lifetime can be extended by the user. But the explicit lifetime of the cookie was not yet validated by the cookie service. Meaning you could extend the SSO lifetime by keeping your cookie alive. As of this change, the authentication lifetime is checked against the current timestamp togegher with the cookie expiration time. See: https://www.pivotaltracker.com/story/show/183402734 -> the SSO cookie's timestamp is within the configured SSO lifetime
- Loading branch information
Showing
11 changed files
with
314 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Surfnet/StepupGateway/GatewayBundle/Sso2fa/DateTime/ExpirationHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* Copyright 2023 SURFnet bv | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\StepupGateway\GatewayBundle\Sso2fa\DateTime; | ||
|
||
use DateTime as CoreDateTime; | ||
use Surfnet\StepupBundle\DateTime\DateTime; | ||
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception\InvalidAuthenticationTimeException; | ||
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\CookieValueInterface; | ||
use TypeError; | ||
|
||
class ExpirationHelper implements ExpirationHelperInterface | ||
{ | ||
/** | ||
* @var CoreDateTime | ||
*/ | ||
private $now; | ||
|
||
/* | ||
* The SSO on 2FA cookie lifetime in seconds | ||
* | ||
* See: config/legacy/parameters.yaml sso_cookie_lifetime | ||
*/ | ||
private $cookieLifetime; | ||
|
||
public function __construct(int $cookieLifetime, CoreDateTime $now = null) | ||
{ | ||
$this->cookieLifetime = $cookieLifetime; | ||
if (!$now) { | ||
$now = DateTime::now(); | ||
} | ||
$this->now = $now; | ||
} | ||
|
||
public function isExpired(CookieValueInterface $cookieValue): bool | ||
{ | ||
try { | ||
$authenticationTimestamp = $cookieValue->authenticationTime(); | ||
} catch (TypeError $error) { | ||
throw new InvalidAuthenticationTimeException( | ||
'The authentication time contained a non-int value', | ||
0, | ||
$error | ||
); | ||
} | ||
if ($authenticationTimestamp < 0) { | ||
throw new InvalidAuthenticationTimeException( | ||
'The authentication time is from before the Unix timestamp epoch' | ||
); | ||
} | ||
if ($authenticationTimestamp > $this->now->getTimestamp()) { | ||
throw new InvalidAuthenticationTimeException( | ||
'The authentication time is from the future, which indicates the clock settings ' . | ||
'are incorrect, or the time in the cookie value was tampered with.' | ||
); | ||
} | ||
|
||
$expirationTimestamp = $authenticationTimestamp + $this->cookieLifetime; | ||
$currentTimestamp = $this->now->getTimestamp(); | ||
// Is the current time greater than the expiration time? | ||
return $currentTimestamp > $expirationTimestamp; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Surfnet/StepupGateway/GatewayBundle/Sso2fa/DateTime/ExpirationHelperInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* Copyright 2023 SURFnet bv | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\StepupGateway\GatewayBundle\Sso2fa\DateTime; | ||
|
||
use Surfnet\StepupGateway\GatewayBundle\Sso2fa\ValueObject\CookieValueInterface; | ||
|
||
/** | ||
* Used to verify if the authentication time from the CookieValue | ||
* surpasses the current timestamp. Which is determined by adding | ||
* the cookie lifetime to the authentication time. And checking that | ||
* against the current timestamp. | ||
* | ||
* The current timestamp can be set on this helper class in order | ||
* to make testing more predictable. However, if this is not set | ||
* explicitly it will use 'now' as the current timestamp. | ||
*/ | ||
interface ExpirationHelperInterface | ||
{ | ||
public function isExpired(CookieValueInterface $cookieValue): bool; | ||
} |
26 changes: 26 additions & 0 deletions
26
...rfnet/StepupGateway/GatewayBundle/Sso2fa/Exception/InvalidAuthenticationTimeException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* Copyright 2023 SURFnet bv | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Surfnet\StepupGateway\GatewayBundle\Sso2fa\Exception; | ||
|
||
use Surfnet\StepupGateway\GatewayBundle\Exception\InvalidArgumentException; | ||
|
||
class InvalidAuthenticationTimeException extends InvalidArgumentException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.