aboutsummaryrefslogtreecommitdiff
path: root/routes/login.php
diff options
context:
space:
mode:
Diffstat (limited to 'routes/login.php')
-rw-r--r--routes/login.php83
1 files changed, 83 insertions, 0 deletions
diff --git a/routes/login.php b/routes/login.php
new file mode 100644
index 0000000..2f73271
--- /dev/null
+++ b/routes/login.php
@@ -0,0 +1,83 @@
+<?php
+
+function presentChallenges(
+ Psso\Session $session, Psso\Context $context, ?string $message = null
+) {
+ $challengeTypes = Psso\AuthFlow::nextStep($context);
+ if ($challengeTypes === true) {
+ // auth finished! all good
+ $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)) {
+ $resp->doc->addChild('challenge-message', L($message));
+ }
+ foreach ($challenges as $challenge) {
+ $challenge->addAsHtml($resp->doc);
+ }
+ $resp->send();
+
+ // 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($session, $context);
+}
+
+function POST(array $config) {
+ $session = Psso\Session::get();
+
+ // we are receiving results of a previous challenge... load it in
+ $challenges = $session->getChallenges();
+ $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;
+ }
+
+ // 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);
+
+ // 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($session, $context, $result->message);
+}