| 29 | | class EntraIdSession extends AbstractSession |
| 30 | | { |
| 31 | | |
| 32 | | final public const string ENTRA_ID_SESSION_KEY = 'pmf-entra-id-session-key'; |
| 33 | | |
| 34 | | final public const string ENTRA_ID_OAUTH_VERIFIER = 'pmf-entra-id-oauth-verifier'; |
| 35 | | |
| 36 | | final public const string ENTRA_ID_JWT = 'pmf-entra-id-jwt'; |
| 37 | | |
| 38 | | private string $currentSessionKey; |
| 39 | | |
| 40 | | public function __construct( |
| 41 | | private readonly Configuration $configuration, |
| 42 | | private readonly Session $session, |
| 43 | | ) { |
| 44 | | parent::__construct($session); |
| 45 | | |
| 46 | | $this->createCurrentSessionKey(); |
| 47 | | } |
| 48 | | |
| 49 | | |
| 50 | | |
| 51 | | |
| 52 | | public function createCurrentSessionKey(): void |
| 53 | | { |
| 54 | | $this->currentSessionKey = Uuid::v4()->toRfc4122(); |
| 55 | | } |
| 56 | | |
| 57 | | |
| 58 | | |
| 59 | | |
| 60 | | public function getCurrentSessionKey(): ?string |
| 61 | | { |
| 62 | | $sessionKey = $this->currentSessionKey ?? $this->session->get(self::ENTRA_ID_SESSION_KEY); |
| 63 | | |
| 64 | | return $sessionKey === null ? null : (string) $sessionKey; |
| 65 | | } |
| 66 | | |
| 67 | | |
| 68 | | |
| 69 | | |
| 70 | | |
| 71 | | |
| 72 | | public function setCurrentSessionKey(): EntraIdSession |
| 73 | | { |
| 74 | | |
| 75 | | if (!isset($this->currentSessionKey)) { |
| 76 | | $this->createCurrentSessionKey(); |
| 77 | | } |
| 78 | | |
| 79 | | $this->session->set(self::ENTRA_ID_SESSION_KEY, $this->currentSessionKey); |
| 80 | | |
| 81 | | return $this; |
| 82 | | } |
| 83 | | |
| 84 | | |
| 85 | | |
| 86 | | |
| 87 | | |
| 88 | | |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | | public function setCookie(string $name, int|string|null $sessionId, int $timeout = 3600, bool $strict = true): void |
| 93 | | { |
| 94 | | $request = Request::createFromGlobals(); |
| 95 | | |
| 96 | | $cookieDomain = parse_url($this->configuration->getDefaultUrl(), PHP_URL_HOST); |
| 97 | | |
| 98 | | setcookie($name, (string) ($sessionId ?? ''), [ |
| 99 | | 'expires' => (int) $request->server->get('REQUEST_TIME') + $timeout, |
| 100 | | 'path' => dirname((string) $request->server->get('SCRIPT_NAME')), |
| 101 | | 'domain' => is_string($cookieDomain) ? $cookieDomain : '', |
| 102 | | 'secure' => $request->isSecure(), |
| 103 | | 'httponly' => true, |
| 104 | | |
| 105 | | 'samesite' => $strict ? 'strict' : 'lax', |
| 106 | | ]); |
| 107 | | } |
| 108 | | |
| 109 | | |
| 110 | | |
| 111 | | |
| 112 | | |
| 113 | | |
| 114 | | public function getCookie(string $name): string |
| 115 | | { |
| 116 | | $request = Request::createFromGlobals(); |
| 117 | | return $request->cookies->get($name, ''); |
| 118 | | } |
| 119 | | } |