@@ -8,20 +8,20 @@ |
||
8 | 8 | |
9 | 9 | class OpenSSLCryptoTest extends SapphireTest |
10 | 10 | { |
11 | - /** |
|
12 | - * @requires extension openssl |
|
13 | - */ |
|
14 | - public function testIntegrity() |
|
15 | - { |
|
16 | - $key = random_bytes(8); |
|
17 | - $salt = random_bytes(16); |
|
11 | + /** |
|
12 | + * @requires extension openssl |
|
13 | + */ |
|
14 | + public function testIntegrity() |
|
15 | + { |
|
16 | + $key = random_bytes(8); |
|
17 | + $salt = random_bytes(16); |
|
18 | 18 | |
19 | - $handler = new OpenSSLCrypto($key, $salt); |
|
19 | + $handler = new OpenSSLCrypto($key, $salt); |
|
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
22 | - $data = random_bytes(1024 * 4); |
|
21 | + for ($i=0; $i<1000; ++$i) { |
|
22 | + $data = random_bytes(1024 * 4); |
|
23 | 23 | |
24 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | - } |
|
26 | - } |
|
24 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | + } |
|
26 | + } |
|
27 | 27 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | |
19 | 19 | $handler = new OpenSSLCrypto($key, $salt); |
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
21 | + for ($i = 0; $i < 1000; ++$i) { |
|
22 | 22 | $data = random_bytes(1024 * 4); |
23 | 23 | |
24 | 24 | $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
@@ -10,29 +10,29 @@ |
||
10 | 10 | |
11 | 11 | class DatabaseStoreTest extends AbstractTest |
12 | 12 | { |
13 | - protected function setUp() |
|
14 | - { |
|
15 | - parent::setUp(); |
|
16 | - |
|
17 | - if (!DB::get_conn() instanceof MySQLDatabase) { |
|
18 | - $this->markTestSkipped('Only MySQL databases are supported'); |
|
19 | - } |
|
20 | - } |
|
21 | - |
|
22 | - protected function getStore() |
|
23 | - { |
|
24 | - $store = Injector::inst()->get(DatabaseStore::class); |
|
25 | - $store->setKey(uniqid()); |
|
26 | - |
|
27 | - return $store; |
|
28 | - } |
|
29 | - |
|
30 | - public function testDataCodecIntegrity() |
|
31 | - { |
|
32 | - for ($i=0; $i<1000; ++$i) { |
|
33 | - $data = random_bytes(1024 * 4); |
|
34 | - |
|
35 | - $this->assertEquals($data, DataCodec::decode(DataCodec::encode($data))); |
|
36 | - } |
|
37 | - } |
|
13 | + protected function setUp() |
|
14 | + { |
|
15 | + parent::setUp(); |
|
16 | + |
|
17 | + if (!DB::get_conn() instanceof MySQLDatabase) { |
|
18 | + $this->markTestSkipped('Only MySQL databases are supported'); |
|
19 | + } |
|
20 | + } |
|
21 | + |
|
22 | + protected function getStore() |
|
23 | + { |
|
24 | + $store = Injector::inst()->get(DatabaseStore::class); |
|
25 | + $store->setKey(uniqid()); |
|
26 | + |
|
27 | + return $store; |
|
28 | + } |
|
29 | + |
|
30 | + public function testDataCodecIntegrity() |
|
31 | + { |
|
32 | + for ($i=0; $i<1000; ++$i) { |
|
33 | + $data = random_bytes(1024 * 4); |
|
34 | + |
|
35 | + $this->assertEquals($data, DataCodec::decode(DataCodec::encode($data))); |
|
36 | + } |
|
37 | + } |
|
38 | 38 | } |
@@ -29,7 +29,7 @@ |
||
29 | 29 | |
30 | 30 | public function testDataCodecIntegrity() |
31 | 31 | { |
32 | - for ($i=0; $i<1000; ++$i) { |
|
32 | + for ($i = 0; $i < 1000; ++$i) { |
|
33 | 33 | $data = random_bytes(1024 * 4); |
34 | 34 | |
35 | 35 | $this->assertEquals($data, DataCodec::decode(DataCodec::encode($data))); |
@@ -8,20 +8,20 @@ |
||
8 | 8 | |
9 | 9 | class McryptCryptoTest extends SapphireTest |
10 | 10 | { |
11 | - /** |
|
12 | - * @requires extension mcrypt |
|
13 | - */ |
|
14 | - public function testIntegrity() |
|
15 | - { |
|
16 | - $key = random_bytes(8); |
|
17 | - $salt = random_bytes(16); |
|
11 | + /** |
|
12 | + * @requires extension mcrypt |
|
13 | + */ |
|
14 | + public function testIntegrity() |
|
15 | + { |
|
16 | + $key = random_bytes(8); |
|
17 | + $salt = random_bytes(16); |
|
18 | 18 | |
19 | - $handler = new McryptCrypto($key, $salt); |
|
19 | + $handler = new McryptCrypto($key, $salt); |
|
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
22 | - $data = random_bytes(1024 * 4); |
|
21 | + for ($i=0; $i<1000; ++$i) { |
|
22 | + $data = random_bytes(1024 * 4); |
|
23 | 23 | |
24 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | - } |
|
26 | - } |
|
24 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
25 | + } |
|
26 | + } |
|
27 | 27 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | |
19 | 19 | $handler = new McryptCrypto($key, $salt); |
20 | 20 | |
21 | - for ($i=0; $i<1000; ++$i) { |
|
21 | + for ($i = 0; $i < 1000; ++$i) { |
|
22 | 22 | $data = random_bytes(1024 * 4); |
23 | 23 | |
24 | 24 | $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
@@ -13,38 +13,38 @@ |
||
13 | 13 | */ |
14 | 14 | class DataCodec |
15 | 15 | { |
16 | - /** |
|
17 | - * Encode binary data into ASCII string (a subset of UTF-8) |
|
18 | - * |
|
19 | - * @param string $data This is a binary blob |
|
20 | - * |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - public static function encode($data) { |
|
24 | - return json_encode([ |
|
25 | - self::class, |
|
26 | - base64_encode($data) |
|
27 | - ]); |
|
28 | - } |
|
16 | + /** |
|
17 | + * Encode binary data into ASCII string (a subset of UTF-8) |
|
18 | + * |
|
19 | + * @param string $data This is a binary blob |
|
20 | + * |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + public static function encode($data) { |
|
24 | + return json_encode([ |
|
25 | + self::class, |
|
26 | + base64_encode($data) |
|
27 | + ]); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Decode ASCII string into original binary data (a php string) |
|
32 | - * |
|
33 | - * @param string $text |
|
34 | - * |
|
35 | - * @param null|string |
|
36 | - */ |
|
37 | - public static function decode($text) { |
|
38 | - $struct = json_decode($text, true, 2); |
|
30 | + /** |
|
31 | + * Decode ASCII string into original binary data (a php string) |
|
32 | + * |
|
33 | + * @param string $text |
|
34 | + * |
|
35 | + * @param null|string |
|
36 | + */ |
|
37 | + public static function decode($text) { |
|
38 | + $struct = json_decode($text, true, 2); |
|
39 | 39 | |
40 | - if (!is_array($struct) || count($struct) !== 2) { |
|
41 | - return null; |
|
42 | - } |
|
40 | + if (!is_array($struct) || count($struct) !== 2) { |
|
41 | + return null; |
|
42 | + } |
|
43 | 43 | |
44 | - if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
45 | - return null; |
|
46 | - } |
|
44 | + if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
45 | + return null; |
|
46 | + } |
|
47 | 47 | |
48 | - return base64_decode($struct[1]); |
|
49 | - } |
|
48 | + return base64_decode($struct[1]); |
|
49 | + } |
|
50 | 50 | } |
@@ -13,94 +13,94 @@ |
||
13 | 13 | class DatabaseStore extends BaseStore |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Determine if the DB is ready to use. |
|
18 | - * |
|
19 | - * @return bool |
|
20 | - * @throws Exception |
|
21 | - */ |
|
22 | - protected function isDatabaseReady() |
|
23 | - { |
|
24 | - // Such as during setup of testsession prior to DB connection. |
|
25 | - if (!DB::is_active()) { |
|
26 | - return false; |
|
27 | - } |
|
28 | - |
|
29 | - // If we have a DB of the wrong type then complain |
|
30 | - if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
31 | - throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
32 | - } |
|
33 | - |
|
34 | - // Prevent freakout during dev/build |
|
35 | - return ClassInfo::hasTable('HybridSessionDataObject'); |
|
36 | - } |
|
37 | - |
|
38 | - public function open($save_path, $name) |
|
39 | - { |
|
40 | - // no-op |
|
41 | - } |
|
42 | - |
|
43 | - public function close() |
|
44 | - { |
|
45 | - // no-op |
|
46 | - } |
|
47 | - |
|
48 | - public function read($session_id) |
|
49 | - { |
|
50 | - if (!$this->isDatabaseReady()) { |
|
51 | - return null; |
|
52 | - } |
|
53 | - |
|
54 | - $query = sprintf( |
|
55 | - 'SELECT "Data" FROM "HybridSessionDataObject" |
|
16 | + /** |
|
17 | + * Determine if the DB is ready to use. |
|
18 | + * |
|
19 | + * @return bool |
|
20 | + * @throws Exception |
|
21 | + */ |
|
22 | + protected function isDatabaseReady() |
|
23 | + { |
|
24 | + // Such as during setup of testsession prior to DB connection. |
|
25 | + if (!DB::is_active()) { |
|
26 | + return false; |
|
27 | + } |
|
28 | + |
|
29 | + // If we have a DB of the wrong type then complain |
|
30 | + if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
31 | + throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
32 | + } |
|
33 | + |
|
34 | + // Prevent freakout during dev/build |
|
35 | + return ClassInfo::hasTable('HybridSessionDataObject'); |
|
36 | + } |
|
37 | + |
|
38 | + public function open($save_path, $name) |
|
39 | + { |
|
40 | + // no-op |
|
41 | + } |
|
42 | + |
|
43 | + public function close() |
|
44 | + { |
|
45 | + // no-op |
|
46 | + } |
|
47 | + |
|
48 | + public function read($session_id) |
|
49 | + { |
|
50 | + if (!$this->isDatabaseReady()) { |
|
51 | + return null; |
|
52 | + } |
|
53 | + |
|
54 | + $query = sprintf( |
|
55 | + 'SELECT "Data" FROM "HybridSessionDataObject" |
|
56 | 56 | WHERE "SessionID" = \'%s\' AND "Expiry" >= %s', |
57 | - Convert::raw2sql($session_id), |
|
58 | - $this->getNow() |
|
59 | - ); |
|
57 | + Convert::raw2sql($session_id), |
|
58 | + $this->getNow() |
|
59 | + ); |
|
60 | 60 | |
61 | - $result = DB::query($query); |
|
61 | + $result = DB::query($query); |
|
62 | 62 | |
63 | - if ($result && $result->numRecords()) { |
|
64 | - $data = $result->first(); |
|
65 | - $decoded = DataCodec::decode($data['Data']); |
|
66 | - return is_null($decoded) ? $data['Data'] : $decoded; |
|
67 | - } |
|
68 | - } |
|
63 | + if ($result && $result->numRecords()) { |
|
64 | + $data = $result->first(); |
|
65 | + $decoded = DataCodec::decode($data['Data']); |
|
66 | + return is_null($decoded) ? $data['Data'] : $decoded; |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - public function write($session_id, $session_data) |
|
71 | - { |
|
72 | - if (!$this->isDatabaseReady()) { |
|
73 | - return false; |
|
74 | - } |
|
70 | + public function write($session_id, $session_data) |
|
71 | + { |
|
72 | + if (!$this->isDatabaseReady()) { |
|
73 | + return false; |
|
74 | + } |
|
75 | 75 | |
76 | - $expiry = $this->getNow() + $this->getLifetime(); |
|
76 | + $expiry = $this->getNow() + $this->getLifetime(); |
|
77 | 77 | |
78 | - DB::query($str = sprintf( |
|
79 | - 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
78 | + DB::query($str = sprintf( |
|
79 | + 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
80 | 80 | VALUES (\'%1$s\', %2$u, \'%3$s\') |
81 | 81 | ON DUPLICATE KEY UPDATE "Expiry" = %2$u, "Data" = \'%3$s\'', |
82 | - Convert::raw2sql($session_id), |
|
83 | - $expiry, |
|
84 | - Convert::raw2sql(DataCodec::encode($session_data)) |
|
85 | - )); |
|
86 | - |
|
87 | - return true; |
|
88 | - } |
|
89 | - |
|
90 | - public function destroy($session_id) |
|
91 | - { |
|
92 | - // NOP |
|
93 | - } |
|
94 | - |
|
95 | - public function gc($maxlifetime) |
|
96 | - { |
|
97 | - if (!$this->isDatabaseReady()) { |
|
98 | - return; |
|
99 | - } |
|
100 | - |
|
101 | - DB::query(sprintf( |
|
102 | - 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
103 | - $this->getNow() |
|
104 | - )); |
|
105 | - } |
|
82 | + Convert::raw2sql($session_id), |
|
83 | + $expiry, |
|
84 | + Convert::raw2sql(DataCodec::encode($session_data)) |
|
85 | + )); |
|
86 | + |
|
87 | + return true; |
|
88 | + } |
|
89 | + |
|
90 | + public function destroy($session_id) |
|
91 | + { |
|
92 | + // NOP |
|
93 | + } |
|
94 | + |
|
95 | + public function gc($maxlifetime) |
|
96 | + { |
|
97 | + if (!$this->isDatabaseReady()) { |
|
98 | + return; |
|
99 | + } |
|
100 | + |
|
101 | + DB::query(sprintf( |
|
102 | + 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
103 | + $this->getNow() |
|
104 | + )); |
|
105 | + } |
|
106 | 106 | } |