Passed
Push — master ( b28c40...f61e74 )
by Petr
08:06
created

CertsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 101
rs 10
wmc 4
1
<?php
2
3
namespace MethodsTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_address_handler\Handler;
8
use kalanis\kw_address_handler\Sources;
9
use kalanis\kw_auth\AuthException;
10
use kalanis\kw_auth\Data\FileCertUser;
11
use kalanis\kw_auth\Methods;
12
use kalanis\kw_locks\LockException;
13
14
15
class CertsTest extends CommonTestClass
16
{
17
    /**
18
     * from PHP.net, updated for my use
19
     */
20
    public function testDummyCert(): void
21
    {
22
        //data you want to sign
23
        $data = 'my data qwertzuiopasdfghjklyxcvbnm1234567890';
24
25
        //create new private and public key
26
        $privateKey = openssl_pkey_new([
27
            "private_key_bits" => 1024,
28
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
29
        ]);
30
        $privateData = openssl_pkey_get_details($privateKey);
31
        $publicKey = openssl_pkey_get_public($privateData['key']);
32
        $publicData = openssl_pkey_get_details($publicKey); // now we must pass string as PublicKey, not resource
33
34
        // create signature
35
        openssl_sign($data, $signature, $privateKey, "sha256WithRSAEncryption");
36
37
        // pass as string
38
        $sig = rawurlencode(base64_encode($signature));
39
40
        // verify signature
41
        $ok = openssl_verify($data, base64_decode(rawurldecode($sig)), $publicData['key'], OPENSSL_ALGO_SHA256);
42
        $this->assertEquals(1, $ok);
43
    }
44
45
    /**
46
     * @throws AuthException
47
     * @throws LockException
48
     * Cannot use provider for this stuff
49
     */
50
    public function testUrlCert(): void
51
    {
52
        //create signature
53
        $privateKey = openssl_pkey_new([
54
            "private_key_bits" => 1024,  # not need too long for testing purposes
55
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
56
        ]);
57
        $privateData = openssl_pkey_get_details($privateKey);
58
        $publicKey = openssl_pkey_get_public($privateData['key']);
59
        $publicData = openssl_pkey_get_details($publicKey);
60
61
        $mockUser = new FileCertUser();
62
        $mockUser->setData(123, 'testing', 456, 789, 3, 'Testing', '/dunno/');
63
        $mockUser->addCertInfo($publicData['key'], 'qwertziop');
64
65
        // now query itself
66
        $urlSource = new Sources\Sources();
67
        $urlSource->setAddress($this->signLink($privateKey, '/dummy/?user=testing&pass=asdf123ghjk456&timestamp=123456', 'qwertziop'));
68
        $method = new Methods\UrlCerts(new \MockAuthCert($mockUser, ''), null, new Handler($urlSource) );
69
        $method->process(new \MockCredentials([Methods\UrlHash::INPUT_NAME => 'testing', Methods\UrlHash::INPUT_STAMP => time(), ]));
70
        $this->assertTrue($method->isAuthorized());
71
        $method->remove();
72
    }
73
74
    protected function signLink($privateKey, string $link, string $salt): string
75
    {
76
        $data = $link . '&salt=' . $salt;
77
        openssl_sign($data, $signature, $privateKey, "sha256WithRSAEncryption");
78
        return $link . '&digest=' . rawurlencode(base64_encode($signature));
79
    }
80
81
    /**
82
     * @throws AuthException
83
     * @throws LockException
84
     * Cannot use provider for this stuff
85
     */
86
    public function testHttpCert(): void
87
    {
88
        //create signature
89
        $privateKey = openssl_pkey_new([
90
            "private_key_bits" => 1024,  # not need too long for testing purposes
91
            "private_key_type" => OPENSSL_KEYTYPE_RSA,
92
        ]);
93
        $privateData = openssl_pkey_get_details($privateKey);
94
        $publicKey = openssl_pkey_get_public($privateData['key']);
95
        $publicData = openssl_pkey_get_details($publicKey);
96
97
        $mockUser = new FileCertUser();
98
        $mockUser->setData(123, 'testing', 456, 789, 3, 'Testing', '/dunno/');
99
        $mockUser->addCertInfo($publicData['key'], 'qwertziop');
100
101
102
        // address
103
        $urlSource = new Sources\Sources();
104
        $urlSource->setAddress('/dummy/?timestamp=123456');
105
106
        // signed
107
        $data = $urlSource->getAddress() . '&salt=qwertziop';
108
        openssl_sign($data, $signature, $privateKey, "sha256WithRSAEncryption");
109
110
        // now query itself
111
        $method = new Methods\HttpCerts(new \MockAuthCert($mockUser, ''), null, new Handler($urlSource), new \MockCredentials(
112
            [Methods\HttpCerts::INPUT_NAME => 'testing', Methods\HttpCerts::INPUT_PASS => rawurlencode(base64_encode($signature)), ]
113
        ) );
114
        $method->process(new \MockCredentials());
115
        $this->assertTrue($method->isAuthorized());
116
    }
117
}
118