diff options
| author | winter Sparkles | 2026-08-09 02:26:33 +0100 |
|---|---|---|
| committer | winter Sparkles | 2026-08-09 02:26:33 +0100 |
| commit | 3354d45762bbe0db33e3d26d8997e6f6624f24d7 (patch) | |
| tree | 4bef4bf59488f443ed54ff6e30002268836ac259 /routes | |
| parent | 4bb659e1f7bb5850db5695a536428a9b71e3ffe5 (diff) | |
implement sessions and also log in/out
session data is stored in sqlite, not using php sessions
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/index.php | 12 | ||||
| -rw-r--r-- | routes/login.php (renamed from routes/test-challenges.php) | 44 | ||||
| -rw-r--r-- | routes/logout.php | 24 |
3 files changed, 66 insertions, 14 deletions
diff --git a/routes/index.php b/routes/index.php index 685ab92..f884be5 100644 --- a/routes/index.php +++ b/routes/index.php @@ -1,8 +1,20 @@ <?php function GET() { + $session = Psso\Session::get(); + + // all of this markup is temporary and for testing only :3 $resp = new Psso\XMLResponse; $content = $resp->doc->addChild('content'); $content->addChild('p', 'Welcome to Pleasant SSO!'); + $identity = $session->getIdentity(); + $content->addChild('pre', 'Your identity: ' . print_r($identity, true)); + if ($identity === null) { + $link = $content->addChild('a', L('login.title')); + $link->addAttribute('href', '/login'); + } else { + $link = $content->addChild('a', L('logout.title')); + $link->addAttribute('href', '/logout'); + } $resp->send(); } diff --git a/routes/test-challenges.php b/routes/login.php index 32aad7e..2f73271 100644 --- a/routes/test-challenges.php +++ b/routes/login.php @@ -1,24 +1,30 @@ <?php function presentChallenges( - Psso\Context $context, ?string $message = null + Psso\Session $session, Psso\Context $context, ?string $message = null ) { $challengeTypes = Psso\AuthFlow::nextStep($context); if ($challengeTypes === true) { // auth finished! all good - header('Location: /login-success'); //temporary crap for testing + $session->setChallenges(null); + $session->setIdentity( + new Psso\Identity($context->user, $context->groups) + ); + header('Location: /'); //temporary crap for testing return; } if (count($challengeTypes) == 0) { throw new RuntimeException('no more challenges available!! auth fail'); } + // create challenges as indicated by the auth flow $challenges = []; foreach ($challengeTypes as $type) { $c = $type::create($context); $challenges[$c->serial] = $c; } - + + // send challenges to user $resp = new Psso\XMLResponse; $resp->doc->addAttribute('title', L('login.title')); if (isset($message)) { @@ -28,20 +34,29 @@ function presentChallenges( $challenge->addAsHtml($resp->doc); } $resp->send(); - // instead of just saving this to a file, it needs to be associated with the - // user's session somehow - file_put_contents('challenges-data', serialize($challenges)); + + // and store the challenges (actual instances!) for next request + $session->setChallenges($challenges); } function GET() { + $session = Psso\Session::get(); + if ($session->getIdentity() !== null) { + // already logged in + header('Location: /'); // change this to return continue page + } + $context = new Psso\Context; - presentChallenges($context); + presentChallenges($session, $context); } function POST(array $config) { + $session = Psso\Session::get(); + // we are receiving results of a previous challenge... load it in - $challenges = unserialize(file_get_contents('challenges-data')); + $challenges = $session->getChallenges(); $answeredChallenge = $challenges[$_POST['challenge']]; + // match up the given input responses to their original Inputs $inputData = []; foreach ($_POST as $name => $value) { @@ -51,17 +66,18 @@ function POST(array $config) { $inputData[$input->id] = $value; } + // check provided inputs against the challenge, are they correct? $providerClass = 'Psso\\AuthProvider\\' . $config['auth']['provider']; $provider = new $providerClass($config); $result = $answeredChallenge->validate($provider, $inputData); + // append the new result to the context so the auth flow can see it $context = $answeredChallenge->context; $context->addResult($result); - - if ($result->successful) { - if (isset($result->user) && !isset($context->user)) { - $context->user = $result->user; - } + + // also set the user in context if we're able to + if ($result->successful && isset($result->user) && !isset($context->user)) { + $context->user = $result->user; } - presentChallenges($context, $result->message); + presentChallenges($session, $context, $result->message); } diff --git a/routes/logout.php b/routes/logout.php new file mode 100644 index 0000000..21b2179 --- /dev/null +++ b/routes/logout.php @@ -0,0 +1,24 @@ +<?php + +function GET() { + $session = Psso\Session::get(); + if ($session->getIdentity() === null) { + // not actually logged in anyway + header('Location: /'); + return; + } + + $resp = new Psso\XMLResponse; + $resp->doc->addAttribute('title', L('logout.title')); + $form = $resp->doc->addChild('form'); + $form->addChild('p', L('logout.warning')); + $form->addAttribute('method', 'post'); + $form->addChild('button', L('logout.confirm')); + $resp->send(); +} + +function POST() { + $session = Psso\Session::get(); + $session->setIdentity(null); + header('Location: /'); +} |
