@@ -12,137 +12,137 @@ |
||
| 12 | 12 | class DatabaseStore extends BaseStore |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Determine if the DB is ready to use. |
|
| 17 | - * |
|
| 18 | - * @return bool |
|
| 19 | - * @throws Exception |
|
| 20 | - */ |
|
| 21 | - protected function isDatabaseReady() |
|
| 22 | - { |
|
| 23 | - // Such as during setup of testsession prior to DB connection. |
|
| 24 | - if (!DB::is_active()) { |
|
| 25 | - return false; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - // If we have a DB of the wrong type then complain |
|
| 29 | - if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
| 30 | - throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - // Prevent freakout during dev/build |
|
| 34 | - return ClassInfo::hasTable('HybridSessionDataObject'); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function open($save_path, $name) |
|
| 38 | - { |
|
| 39 | - // no-op |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - public function close() |
|
| 43 | - { |
|
| 44 | - // no-op |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function read($session_id) |
|
| 48 | - { |
|
| 49 | - if (!$this->isDatabaseReady()) { |
|
| 50 | - return null; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - $query = sprintf( |
|
| 54 | - 'SELECT "Data" FROM "HybridSessionDataObject" |
|
| 15 | + /** |
|
| 16 | + * Determine if the DB is ready to use. |
|
| 17 | + * |
|
| 18 | + * @return bool |
|
| 19 | + * @throws Exception |
|
| 20 | + */ |
|
| 21 | + protected function isDatabaseReady() |
|
| 22 | + { |
|
| 23 | + // Such as during setup of testsession prior to DB connection. |
|
| 24 | + if (!DB::is_active()) { |
|
| 25 | + return false; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + // If we have a DB of the wrong type then complain |
|
| 29 | + if (!(DB::get_conn() instanceof MySQLDatabase)) { |
|
| 30 | + throw new Exception('HybridSessions\Store\DatabaseStore currently only works with MySQL databases'); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + // Prevent freakout during dev/build |
|
| 34 | + return ClassInfo::hasTable('HybridSessionDataObject'); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function open($save_path, $name) |
|
| 38 | + { |
|
| 39 | + // no-op |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + public function close() |
|
| 43 | + { |
|
| 44 | + // no-op |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function read($session_id) |
|
| 48 | + { |
|
| 49 | + if (!$this->isDatabaseReady()) { |
|
| 50 | + return null; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + $query = sprintf( |
|
| 54 | + 'SELECT "Data" FROM "HybridSessionDataObject" |
|
| 55 | 55 | WHERE "SessionID" = \'%s\' AND "Expiry" >= %s', |
| 56 | - Convert::raw2sql($session_id), |
|
| 57 | - $this->getNow() |
|
| 58 | - ); |
|
| 56 | + Convert::raw2sql($session_id), |
|
| 57 | + $this->getNow() |
|
| 58 | + ); |
|
| 59 | 59 | |
| 60 | - $result = DB::query($query); |
|
| 60 | + $result = DB::query($query); |
|
| 61 | 61 | |
| 62 | - if ($result && $result->numRecords()) { |
|
| 63 | - $data = $result->first(); |
|
| 64 | - $decoded = static::binaryDataJsonDecode($data['Data']); |
|
| 65 | - return is_null($decoded) ? $data['Data'] : $decoded; |
|
| 66 | - } |
|
| 67 | - } |
|
| 62 | + if ($result && $result->numRecords()) { |
|
| 63 | + $data = $result->first(); |
|
| 64 | + $decoded = static::binaryDataJsonDecode($data['Data']); |
|
| 65 | + return is_null($decoded) ? $data['Data'] : $decoded; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - public function write($session_id, $session_data) |
|
| 70 | - { |
|
| 71 | - if (!$this->isDatabaseReady()) { |
|
| 72 | - return false; |
|
| 73 | - } |
|
| 69 | + public function write($session_id, $session_data) |
|
| 70 | + { |
|
| 71 | + if (!$this->isDatabaseReady()) { |
|
| 72 | + return false; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $expiry = $this->getNow() + $this->getLifetime(); |
|
| 75 | + $expiry = $this->getNow() + $this->getLifetime(); |
|
| 76 | 76 | |
| 77 | - DB::query($str = sprintf( |
|
| 78 | - 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
| 77 | + DB::query($str = sprintf( |
|
| 78 | + 'INSERT INTO "HybridSessionDataObject" ("SessionID", "Expiry", "Data") |
|
| 79 | 79 | VALUES (\'%1$s\', %2$u, \'%3$s\') |
| 80 | 80 | ON DUPLICATE KEY UPDATE "Expiry" = %2$u, "Data" = \'%3$s\'', |
| 81 | - Convert::raw2sql($session_id), |
|
| 82 | - $expiry, |
|
| 83 | - Convert::raw2sql(static::binaryDataJsonEncode($session_data)) |
|
| 84 | - )); |
|
| 85 | - |
|
| 86 | - return true; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function destroy($session_id) |
|
| 90 | - { |
|
| 91 | - // NOP |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - public function gc($maxlifetime) |
|
| 95 | - { |
|
| 96 | - if (!$this->isDatabaseReady()) { |
|
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - DB::query(sprintf( |
|
| 101 | - 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
| 102 | - $this->getNow() |
|
| 103 | - )); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Encode binary data into ASCII string (a subset of UTF-8) |
|
| 108 | - * |
|
| 109 | - * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
| 110 | - * binary data as text |
|
| 111 | - * |
|
| 112 | - * @param string $data This is a binary blob |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public static function binaryDataJsonEncode($data) |
|
| 117 | - { |
|
| 118 | - return json_encode([ |
|
| 119 | - self::class, |
|
| 120 | - base64_encode($data) |
|
| 121 | - ]); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Decode ASCII string into original binary data (a php string) |
|
| 126 | - * |
|
| 127 | - * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
| 128 | - * binary data as text |
|
| 129 | - * |
|
| 130 | - * @param string $text |
|
| 131 | - * |
|
| 132 | - * @param null|string |
|
| 133 | - */ |
|
| 134 | - public static function binaryDataJsonDecode($text) |
|
| 135 | - { |
|
| 136 | - $struct = json_decode($text, true, 2); |
|
| 137 | - |
|
| 138 | - if (!is_array($struct) || count($struct) !== 2) { |
|
| 139 | - return null; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
| 143 | - return null; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return base64_decode($struct[1]); |
|
| 147 | - } |
|
| 81 | + Convert::raw2sql($session_id), |
|
| 82 | + $expiry, |
|
| 83 | + Convert::raw2sql(static::binaryDataJsonEncode($session_data)) |
|
| 84 | + )); |
|
| 85 | + |
|
| 86 | + return true; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function destroy($session_id) |
|
| 90 | + { |
|
| 91 | + // NOP |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + public function gc($maxlifetime) |
|
| 95 | + { |
|
| 96 | + if (!$this->isDatabaseReady()) { |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + DB::query(sprintf( |
|
| 101 | + 'DELETE FROM "HybridSessionDataObject" WHERE "Expiry" < %u', |
|
| 102 | + $this->getNow() |
|
| 103 | + )); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Encode binary data into ASCII string (a subset of UTF-8) |
|
| 108 | + * |
|
| 109 | + * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
| 110 | + * binary data as text |
|
| 111 | + * |
|
| 112 | + * @param string $data This is a binary blob |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public static function binaryDataJsonEncode($data) |
|
| 117 | + { |
|
| 118 | + return json_encode([ |
|
| 119 | + self::class, |
|
| 120 | + base64_encode($data) |
|
| 121 | + ]); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Decode ASCII string into original binary data (a php string) |
|
| 126 | + * |
|
| 127 | + * Silverstripe <= 4.4 does not have a binary db field implementation, so we have to store |
|
| 128 | + * binary data as text |
|
| 129 | + * |
|
| 130 | + * @param string $text |
|
| 131 | + * |
|
| 132 | + * @param null|string |
|
| 133 | + */ |
|
| 134 | + public static function binaryDataJsonDecode($text) |
|
| 135 | + { |
|
| 136 | + $struct = json_decode($text, true, 2); |
|
| 137 | + |
|
| 138 | + if (!is_array($struct) || count($struct) !== 2) { |
|
| 139 | + return null; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if (!isset($struct[0]) || !isset($struct[1]) || $struct[0] !== self::class) { |
|
| 143 | + return null; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return base64_decode($struct[1]); |
|
| 147 | + } |
|
| 148 | 148 | } |
@@ -10,17 +10,17 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class OpenSSLCryptoTest extends SapphireTest |
| 12 | 12 | { |
| 13 | - public function testIntegrity() |
|
| 14 | - { |
|
| 15 | - $key = random_bytes(8); |
|
| 16 | - $salt = random_bytes(16); |
|
| 13 | + public function testIntegrity() |
|
| 14 | + { |
|
| 15 | + $key = random_bytes(8); |
|
| 16 | + $salt = random_bytes(16); |
|
| 17 | 17 | |
| 18 | - $handler = new OpenSSLCrypto($key, $salt); |
|
| 18 | + $handler = new OpenSSLCrypto($key, $salt); |
|
| 19 | 19 | |
| 20 | - for ($i = 0; $i < 1000; ++$i) { |
|
| 21 | - $data = random_bytes(1024 * 4); |
|
| 20 | + for ($i = 0; $i < 1000; ++$i) { |
|
| 21 | + $data = random_bytes(1024 * 4); |
|
| 22 | 22 | |
| 23 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 24 | - } |
|
| 25 | - } |
|
| 23 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -9,29 +9,29 @@ |
||
| 9 | 9 | |
| 10 | 10 | class DatabaseStoreTest extends AbstractTest |
| 11 | 11 | { |
| 12 | - protected function setUp() |
|
| 13 | - { |
|
| 14 | - parent::setUp(); |
|
| 15 | - |
|
| 16 | - if (!DB::get_conn() instanceof MySQLDatabase) { |
|
| 17 | - $this->markTestSkipped('Only MySQL databases are supported'); |
|
| 18 | - } |
|
| 19 | - } |
|
| 20 | - |
|
| 21 | - protected function getStore() |
|
| 22 | - { |
|
| 23 | - $store = Injector::inst()->get(DatabaseStore::class); |
|
| 24 | - $store->setKey(uniqid()); |
|
| 25 | - |
|
| 26 | - return $store; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function testDataCodecIntegrity() |
|
| 30 | - { |
|
| 31 | - for ($i = 0; $i < 1000; ++$i) { |
|
| 32 | - $data = random_bytes(1024 * 4); |
|
| 33 | - |
|
| 34 | - $this->assertEquals($data, DatabaseStore::binaryDataJsonDecode(DatabaseStore::binaryDataJsonEncode($data))); |
|
| 35 | - } |
|
| 36 | - } |
|
| 12 | + protected function setUp() |
|
| 13 | + { |
|
| 14 | + parent::setUp(); |
|
| 15 | + |
|
| 16 | + if (!DB::get_conn() instanceof MySQLDatabase) { |
|
| 17 | + $this->markTestSkipped('Only MySQL databases are supported'); |
|
| 18 | + } |
|
| 19 | + } |
|
| 20 | + |
|
| 21 | + protected function getStore() |
|
| 22 | + { |
|
| 23 | + $store = Injector::inst()->get(DatabaseStore::class); |
|
| 24 | + $store->setKey(uniqid()); |
|
| 25 | + |
|
| 26 | + return $store; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function testDataCodecIntegrity() |
|
| 30 | + { |
|
| 31 | + for ($i = 0; $i < 1000; ++$i) { |
|
| 32 | + $data = random_bytes(1024 * 4); |
|
| 33 | + |
|
| 34 | + $this->assertEquals($data, DatabaseStore::binaryDataJsonDecode(DatabaseStore::binaryDataJsonEncode($data))); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | } |
@@ -11,28 +11,28 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class McryptCryptoTest extends SapphireTest |
| 13 | 13 | { |
| 14 | - public function testIntegrity() |
|
| 15 | - { |
|
| 16 | - $this->markTestSkipped( |
|
| 17 | - 'McryptCrypto is losing zero bytes at the end of messages: ' . |
|
| 18 | - 'https://github.com/silverstripe/silverstripe-hybridsessions/issues/53' |
|
| 19 | - ); |
|
| 20 | - |
|
| 21 | - $error_reporting = ini_set('error_reporting', (int) ini_get('error_reporting') & ~E_DEPRECATED); |
|
| 22 | - |
|
| 23 | - try { |
|
| 24 | - $key = random_bytes(8); |
|
| 25 | - $salt = random_bytes(16); |
|
| 26 | - |
|
| 27 | - $handler = new McryptCrypto($key, $salt); |
|
| 28 | - |
|
| 29 | - for ($i = 0; $i < 1000; ++$i) { |
|
| 30 | - $data = random_bytes(1024 * 4); |
|
| 31 | - |
|
| 32 | - $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 33 | - } |
|
| 34 | - } finally { |
|
| 35 | - ini_set('error_reporting', $error_reporting); |
|
| 36 | - } |
|
| 37 | - } |
|
| 14 | + public function testIntegrity() |
|
| 15 | + { |
|
| 16 | + $this->markTestSkipped( |
|
| 17 | + 'McryptCrypto is losing zero bytes at the end of messages: ' . |
|
| 18 | + 'https://github.com/silverstripe/silverstripe-hybridsessions/issues/53' |
|
| 19 | + ); |
|
| 20 | + |
|
| 21 | + $error_reporting = ini_set('error_reporting', (int) ini_get('error_reporting') & ~E_DEPRECATED); |
|
| 22 | + |
|
| 23 | + try { |
|
| 24 | + $key = random_bytes(8); |
|
| 25 | + $salt = random_bytes(16); |
|
| 26 | + |
|
| 27 | + $handler = new McryptCrypto($key, $salt); |
|
| 28 | + |
|
| 29 | + for ($i = 0; $i < 1000; ++$i) { |
|
| 30 | + $data = random_bytes(1024 * 4); |
|
| 31 | + |
|
| 32 | + $this->assertEquals($data, $handler->decrypt($handler->encrypt($data))); |
|
| 33 | + } |
|
| 34 | + } finally { |
|
| 35 | + ini_set('error_reporting', $error_reporting); |
|
| 36 | + } |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | public function testIntegrity() |
| 15 | 15 | { |
| 16 | 16 | $this->markTestSkipped( |
| 17 | - 'McryptCrypto is losing zero bytes at the end of messages: ' . |
|
| 17 | + 'McryptCrypto is losing zero bytes at the end of messages: '. |
|
| 18 | 18 | 'https://github.com/silverstripe/silverstripe-hybridsessions/issues/53' |
| 19 | 19 | ); |
| 20 | 20 | |