-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add token expire clean up * improve expired sso identities removal
- Loading branch information
Showing
9 changed files
with
201 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Listeners | ||
|
||
## Clean Up expired Identities | ||
There two ways to clean up expired identities. | ||
|
||
> **Note:** This feature is not enabled by default! | ||
### Important Notes | ||
This Clean-Up task also removes the related user within some exceptions: | ||
- The user will **not be deleted** if the given user has other SSO identities | ||
- The user will **not be deleted** if a password has been defined (not empty rule) | ||
|
||
### I. Expired Date | ||
Use the `clean_up_expired_tokens` configuration flag to enable the clean-up. | ||
In this example, all identities, older than `expiresAt` will be removed. | ||
|
||
|
||
```yaml | ||
members: | ||
oauth: | ||
clean_up_expired_tokens: true | ||
``` | ||
SQL Explanation: | ||
```mysql | ||
SELECT entites WHERE expiresAt IS NOT NULL AND expiresAt < NOW(); | ||
``` | ||
|
||
### II. Expired By TTL | ||
Use the `clean_up_expired_tokens` configuration flag to enable the clean-up. Use the `expired_tokens_ttl` configuration flag to define a ttl in seconds. | ||
In this example, all identities, older than 24h will be removed. | ||
|
||
```yaml | ||
members: | ||
oauth: | ||
clean_up_expired_tokens: true | ||
expired_tokens_ttl: 86400 | ||
``` | ||
SQL Explanation: | ||
```mysql | ||
SELECT entites WHERE o_creationDate < (UNIX_TIMESTAMP() - $TTL); | ||
``` |
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
107 changes: 107 additions & 0 deletions
107
src/MembersBundle/EventListener/Maintenance/SsoCleanUpExpiredTokenListener.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,107 @@ | ||
<?php | ||
|
||
namespace MembersBundle\EventListener\Maintenance; | ||
|
||
use MembersBundle\Adapter\User\UserInterface; | ||
use MembersBundle\Manager\SsoIdentityManagerInterface; | ||
use Pimcore\Maintenance\TaskInterface; | ||
use Pimcore\Model\DataObject\Concrete; | ||
|
||
class SsoCleanUpExpiredTokenListener implements TaskInterface | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
protected $oauthEnabled; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $cleanUpExpiredTokens; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $expiredTokensTtl; | ||
|
||
/** | ||
* @var SsoIdentityManagerInterface | ||
*/ | ||
protected $ssoIdentityManager; | ||
|
||
/** | ||
* @param bool $oauthEnabled | ||
* @param bool $cleanUpExpiredTokens | ||
* @param int $expiredTokensTtl | ||
* @param SsoIdentityManagerInterface $ssoIdentityManager | ||
*/ | ||
public function __construct( | ||
bool $oauthEnabled, | ||
bool $cleanUpExpiredTokens, | ||
int $expiredTokensTtl, | ||
SsoIdentityManagerInterface $ssoIdentityManager | ||
) { | ||
$this->oauthEnabled = $oauthEnabled; | ||
$this->cleanUpExpiredTokens = $cleanUpExpiredTokens; | ||
$this->expiredTokensTtl = $expiredTokensTtl; | ||
$this->ssoIdentityManager = $ssoIdentityManager; | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function execute() | ||
{ | ||
if ($this->oauthEnabled === false) { | ||
return; | ||
} | ||
|
||
if ($this->cleanUpExpiredTokens === false) { | ||
return; | ||
} | ||
|
||
$identities = $this->ssoIdentityManager->findExpiredSsoIdentities($this->expiredTokensTtl); | ||
|
||
foreach ($identities as $ssoIdentity) { | ||
|
||
if (!$ssoIdentity instanceof Concrete) { | ||
continue; | ||
} | ||
|
||
$this->handleIdentityRemoval($ssoIdentity); | ||
} | ||
} | ||
|
||
/** | ||
* @param Concrete $ssoIdentity | ||
* | ||
* @throws \Exception | ||
*/ | ||
protected function handleIdentityRemoval(Concrete $ssoIdentity) | ||
{ | ||
$user = $this->ssoIdentityManager->getUserBySsoIdentity($ssoIdentity->getProvider(), $ssoIdentity->getIdentifier()); | ||
|
||
$ssoIdentity->delete(); | ||
|
||
if (!$user instanceof UserInterface) { | ||
return; | ||
} | ||
|
||
// don't touch a user with a stored password | ||
if (!empty($user->getPassword())) { | ||
return; | ||
} | ||
|
||
// don't touch a user if he has other identities | ||
$userSsoIdentities = $this->ssoIdentityManager->getSsoIdentities($user); | ||
if (is_array($userSsoIdentities) && count($userSsoIdentities) > 0) { | ||
return; | ||
} | ||
|
||
if (!$user instanceof Concrete) { | ||
return; | ||
} | ||
|
||
$user->delete(); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/MembersBundle/Resources/config/services/oauth/mainentance.yml
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,12 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
|
||
MembersBundle\EventListener\Maintenance\SsoCleanUpExpiredTokenListener: | ||
arguments: | ||
$oauthEnabled: '%members.oauth.enabled%' | ||
$cleanUpExpiredTokens: '%members.oauth.clean_up_expired_tokens%' | ||
$expiredTokensTtl: '%members.oauth.expired_tokens_ttl%' | ||
tags: | ||
- {name: pimcore.maintenance.task, type: members_sso_clean_up_expired_tokens } |
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