2024-12-30 10:41:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Core\Objects\DatabaseEntity;
|
|
|
|
|
|
|
|
use Core\Driver\SQL\SQL;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\DefaultValue;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
|
|
|
|
use Core\Objects\DatabaseEntity\Attribute\Unique;
|
|
|
|
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
|
|
|
|
|
|
|
|
class SsoRequest extends DatabaseEntity {
|
|
|
|
|
|
|
|
const SSO_REQUEST_DURABILITY = 15; // in minutes
|
|
|
|
|
2024-12-31 14:25:36 +01:00
|
|
|
// auto-delete sso requests after 30 days after creation
|
|
|
|
protected static array $entityLogConfig = [
|
|
|
|
"update" => false,
|
|
|
|
"delete" => true,
|
|
|
|
"insert" => true,
|
|
|
|
"lifetime" => 30
|
|
|
|
];
|
|
|
|
|
2024-12-30 10:41:13 +01:00
|
|
|
#[MaxLength(128)]
|
|
|
|
#[Unique]
|
|
|
|
private string $identifier;
|
|
|
|
|
|
|
|
private SsoProvider $ssoProvider;
|
|
|
|
|
2024-12-31 14:25:36 +01:00
|
|
|
private ?Session $session;
|
|
|
|
|
2024-12-30 10:41:13 +01:00
|
|
|
private \DateTime $validUntil;
|
|
|
|
|
|
|
|
#[DefaultValue(false)]
|
|
|
|
private bool $used;
|
|
|
|
|
|
|
|
private ?string $redirectUrl;
|
|
|
|
|
|
|
|
public static function create(SQL $sql, SsoProvider $ssoProvider, ?string $redirectUrl): ?SsoRequest {
|
|
|
|
$request = new SsoRequest();
|
|
|
|
$request->identifier = uuidv4();
|
|
|
|
$request->ssoProvider = $ssoProvider;
|
|
|
|
$request->used = false;
|
2024-12-31 14:25:36 +01:00
|
|
|
$request->session = null;
|
2024-12-30 10:41:13 +01:00
|
|
|
$request->validUntil = (new \DateTime())->modify(sprintf('+%d minutes', self::SSO_REQUEST_DURABILITY));
|
|
|
|
$request->redirectUrl = $redirectUrl;
|
|
|
|
if ($request->save($sql)) {
|
|
|
|
return $request;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIdentifier() : string {
|
|
|
|
return $this->identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRedirectUrl() : ?string {
|
|
|
|
return $this->redirectUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function wasUsed() : bool {
|
|
|
|
return $this->used;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isValid() : bool {
|
|
|
|
return !isInPast($this->validUntil);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getProvider() : SsoProvider {
|
|
|
|
return $this->ssoProvider;
|
|
|
|
}
|
|
|
|
|
2024-12-31 14:25:36 +01:00
|
|
|
public function invalidate(SQL $sql, ?Session $session = null) : bool {
|
2024-12-30 10:41:13 +01:00
|
|
|
$this->used = true;
|
2024-12-31 14:25:36 +01:00
|
|
|
if ($session) {
|
|
|
|
$this->session = $session;
|
|
|
|
return $this->save($sql, ["used", "session"]);
|
|
|
|
} else {
|
|
|
|
return $this->save($sql, ["used"]);
|
|
|
|
}
|
2024-12-30 10:41:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|