aboutsummaryrefslogtreecommitdiff
path: root/routes/test-challenges.php
blob: 32aad7e66a946d075b5a625ede9a47ecd52ec9db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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);
}