Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/SimpleSAML/Auth/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Simple
/** @var \SimpleSAML\Configuration */
protected Configuration $app_config;

/** @var \SimpleSAML\Session */
protected Session $session;
/** @var ?\SimpleSAML\Session */
protected ?Session $session;


/**
Expand All @@ -44,7 +44,7 @@ public function __construct(
$this->app_config = $config->getOptionalConfigItem('application', []);

if ($session === null) {
$session = Session::getSessionFromRequest();
$session = Session::getExistingSessionFromRequest();
}
$this->session = $session;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getAuthSource(): Source
*/
public function isAuthenticated(): bool
{
return $this->session->isValid($this->authSource);
return $this->session !== null && $this->session->isValid($this->authSource);
}


Expand All @@ -96,7 +96,7 @@ public function isAuthenticated(): bool
*/
public function requireAuth(array $params = []): void
{
if ($this->session->isValid($this->authSource)) {
if ($this->isAuthenticated()) {
// Already authenticated
return;
}
Expand Down Expand Up @@ -202,13 +202,13 @@ public function logout(string|array|null $params = null): void
Assert::true(isset($params['ReturnStateParam'], $params['ReturnStateStage']));
}

if ($this->session->isValid($this->authSource)) {
$state = $this->session->getAuthData($this->authSource, 'LogoutState');
if ($this->isAuthenticated()) {
$state = $this->getSession()->getAuthData($this->authSource, 'LogoutState');
if ($state !== null) {
$params = array_merge($state, $params);
}

$this->session->doLogout($this->authSource);
$this->getSession()->doLogout($this->authSource);

$params['LogoutCompletedHandler'] = [get_class($this), 'logoutCompleted'];

Expand Down Expand Up @@ -259,13 +259,7 @@ public static function logoutCompleted(array $state): void
*/
public function getAttributes(): array
{
if (!$this->isAuthenticated()) {
// Not authenticated
return [];
}

// Authenticated
return $this->session->getAuthData($this->authSource, 'Attributes');
return $this->getAuthData('Attributes') ?? [];
}


Expand All @@ -282,7 +276,7 @@ public function getAuthData(string $name): mixed
return null;
}

return $this->session->getAuthData($this->authSource, $name);
return $this->getSession()->getAuthData($this->authSource, $name);
}


Expand All @@ -297,7 +291,7 @@ public function getAuthDataArray(): ?array
return null;
}

return $this->session->getAuthState($this->authSource);
return $this->getSession()->getAuthState($this->authSource);
}


Expand Down Expand Up @@ -347,6 +341,21 @@ public function getLogoutURL(?string $returnTo = null): string
}


/**
* Get the current session, create it if it doesn't exist yet.
*
* @return \SimpleSAML\Session
*/
protected function getSession(): Session
{
if ($this->session === null) {
$this->session = Session::getSessionFromRequest();
}

return $this->session;
}


/**
* Process a URL and modify it according to the application/baseURL configuration option, if present.
*
Expand Down
23 changes: 20 additions & 3 deletions src/SimpleSAML/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ public function __unserialize(array $serialized): void


/**
* Retrieves the current session. Creates a new session if there's not one.
* Retrieves the current session. Returns null if there's not one.
*
* @return \SimpleSAML\Session The current session.
* @return ?\SimpleSAML\Session The current session.
* @throws \Exception When session couldn't be initialized and the session fallback is disabled by configuration.
*/
public static function getSessionFromRequest(): Session
public static function getExistingSessionFromRequest(): ?Session
{
// check if we already have initialized the session
/** @psalm-suppress RedundantCondition */
Expand Down Expand Up @@ -288,6 +288,23 @@ public static function getSessionFromRequest(): Session
return self::load($session);
}

return null;
}


/**
* Retrieves the current session. Creates a new session if there's not one.
*
* @return \SimpleSAML\Session The current session.
* @throws \Exception When session couldn't be initialized and the session fallback is disabled by configuration.
*/
public static function getSessionFromRequest(): Session
{
$session = static::getExistingSessionFromRequest();
if ($session !== null) {
return $session;
}

/*
* We didn't have a session loaded when we started, but we have it now. At this point, getSession() failed but
* it must have triggered the creation of a session at some point during the process (e.g. while logging an
Expand Down
Loading