AuthForm::tokenAndDigest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 9.7998
cc 1
nc 1
nop 5
crap 1
1
<?php
2
3
namespace kalanis\kw_auth_forms;
4
5
6
use ArrayAccess;
7
use kalanis\kw_forms\Form;
8
use kalanis\kw_rules\Rules\MatchAll;
9
use kalanis\kw_rules\Rules\ProcessCallback;
10
11
12
/**
13
 * Class AuthForm
14
 * @package kalanis\kw_auth_forms
15
 * How it works:
16
 * Insert this one into the form and call it:
17
 *
18
 *    AuthForm::tokenAndDigest('digest2', new Methods\ImplodeHash($currentUserCertLib), $this, ['first', 'next', 'last'], $cookies)
19
 *
20
 * It adds hidden input which will be checked for token value or digest code
21
 * Then when the form will be processed this hidden input allow/deny processing further because it checks for code in added input
22
 */
23
class AuthForm
24
{
25
    /**
26
     * @param string $inputAlias
27
     * @param Rules\ARule $digest
28
     * @param Form $boundForm
29
     * @param string[] $whichInputs
30
     * @param ArrayAccess<string|int, string|int|float|bool|null> $cookies
31
     */
32 3
    public static function digest(string $inputAlias, Rules\ARule $digest, Form $boundForm, array $whichInputs, ArrayAccess $cookies): void
33
    {
34
        // init input
35 3
        $csrf = new Inputs\AuthCsrf();
36 3
        $csrf->setHidden($inputAlias, $cookies);
37
38
        // check content for digested value
39 3
        $digest->setBoundForm($boundForm);
40 3
        $digest->setAgainstValue($whichInputs);
41 3
        $digest->setErrorText('Digest fails');
42
43
        // add rule to input
44 3
        $csrf->removeRules();
45 3
        $csrf->addRules([$digest]);
46 3
        $boundForm->addControlDefaultKey($csrf);
47 3
    }
48
49
    /**
50
     * @param string $inputAlias
51
     * @param Rules\ARule $digest
52
     * @param Form $boundForm
53
     * @param string[] $whichInputs
54
     * @param ArrayAccess<string|int, string|int|float|bool|null> $cookies
55
     */
56 3
    public static function tokenAndDigest(string $inputAlias, Rules\ARule $digest, Form $boundForm, array $whichInputs, ArrayAccess $cookies): void
57
    {
58
        // init input
59 3
        $csrf = new Inputs\AuthCsrf();
60 3
        $csrf->setHidden($inputAlias, $cookies);
61
62
        // check for classical CSRF token
63 3
        $check = new ProcessCallback();
64 3
        $check->setAgainstValue([$csrf, 'checkToken']);
65 3
        $check->setErrorText('Token fails');
66
67
        // check content for digested value
68 3
        $digest->setBoundForm($boundForm);
69 3
        $digest->setAgainstValue($whichInputs);
70 3
        $digest->setErrorText('Digest fails');
71
72
        // match any rule
73 3
        $match = new MatchAll();
74 3
        $match->setErrorText('Nothing match');
75 3
        $match->setAgainstValue([$check, $digest]);
76
77
        // add rules to input
78 3
        $csrf->removeRules();
79 3
        $csrf->addRules([$match]);
80 3
        $boundForm->addControlDefaultKey($csrf);
81 3
    }
82
}
83