diff options
Diffstat (limited to 'routes/test-challenges.php')
| -rw-r--r-- | routes/test-challenges.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/routes/test-challenges.php b/routes/test-challenges.php new file mode 100644 index 0000000..32aad7e --- /dev/null +++ b/routes/test-challenges.php @@ -0,0 +1,67 @@ +<?php + +function presentChallenges( + 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 + return; + } + if (count($challengeTypes) == 0) { + throw new RuntimeException('no more challenges available!! auth fail'); + } + + $challenges = []; + foreach ($challengeTypes as $type) { + $c = $type::create($context); + $challenges[$c->serial] = $c; + } + + $resp = new Psso\XMLResponse; + $resp->doc->addAttribute('title', L('login.title')); + if (isset($message)) { + $resp->doc->addChild('challenge-message', L($message)); + } + foreach ($challenges as $challenge) { + $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)); +} + +function GET() { + $context = new Psso\Context; + presentChallenges($context); +} + +function POST(array $config) { + // we are receiving results of a previous challenge... load it in + $challenges = unserialize(file_get_contents('challenges-data')); + $answeredChallenge = $challenges[$_POST['challenge']]; + // match up the given input responses to their original Inputs + $inputData = []; + foreach ($_POST as $name => $value) { + if ($name == 'challenge') continue; + $serial = explode('__', $name, 2)[1]; + $input = $answeredChallenge->findInput($serial); + $inputData[$input->id] = $value; + } + + $providerClass = 'Psso\\AuthProvider\\' . $config['auth']['provider']; + $provider = new $providerClass($config); + $result = $answeredChallenge->validate($provider, $inputData); + + $context = $answeredChallenge->context; + $context->addResult($result); + + if ($result->successful) { + if (isset($result->user) && !isset($context->user)) { + $context->user = $result->user; + } + } + presentChallenges($context, $result->message); +} |
