Completed
Pull Request — master (#33)
by
unknown
01:33
created
src/Crypto/CryptoHandler.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -5,27 +5,27 @@
 block discarded – undo
5 5
 interface CryptoHandler
6 6
 {
7 7
 
8
-    /**
9
-     * @param string $data
10
-     *
11
-     * @return string
12
-     */
13
-    public function encrypt($data);
8
+	/**
9
+	 * @param string $data
10
+	 *
11
+	 * @return string
12
+	 */
13
+	public function encrypt($data);
14 14
 
15
-    /**
16
-     * @param string $data
17
-     *
18
-     * @return string
19
-     */
20
-    public function decrypt($data);
15
+	/**
16
+	 * @param string $data
17
+	 *
18
+	 * @return string
19
+	 */
20
+	public function decrypt($data);
21 21
 
22
-    /**
23
-     * @return string
24
-     */
25
-    public function getKey();
22
+	/**
23
+	 * @return string
24
+	 */
25
+	public function getKey();
26 26
 
27
-    /**
28
-     * @return string
29
-     */
30
-    public function getSalt();
27
+	/**
28
+	 * @return string
29
+	 */
30
+	public function getSalt();
31 31
 }
Please login to merge, or discard this patch.
src/Crypto/OpenSSLCrypto.php 1 patch
Indentation   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -8,90 +8,90 @@
 block discarded – undo
8 8
  */
9 9
 class OpenSSLCrypto implements CryptoHandler
10 10
 {
11
-    protected $key;
11
+	protected $key;
12 12
 
13
-    protected $salt;
13
+	protected $salt;
14 14
 
15
-    protected $saltedKey;
15
+	protected $saltedKey;
16 16
 
17
-    /**
18
-     * @return string
19
-     */
20
-    public function getKey()
21
-    {
22
-        return $this->key;
23
-    }
17
+	/**
18
+	 * @return string
19
+	 */
20
+	public function getKey()
21
+	{
22
+		return $this->key;
23
+	}
24 24
 
25
-    /**
26
-     * @return string
27
-     */
28
-    public function getSalt()
29
-    {
30
-        return $this->salt;
31
-    }
25
+	/**
26
+	 * @return string
27
+	 */
28
+	public function getSalt()
29
+	{
30
+		return $this->salt;
31
+	}
32 32
 
33
-    /**
34
-     * @param string $key a per-site secret string which is used as the base encryption key.
35
-     * @param string $salt a per-session random string which is used as a salt to generate a per-session key
36
-     *
37
-     * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session,
38
-     * and even modify & re-sign it.
39
-     *
40
-     * The salt is a random per-session string that is used with the base encryption key to create a per-session key.
41
-     * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key.
42
-     *
43
-     * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't
44
-     * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes
45
-     * a great salt, so no need to generate & handle another one.
46
-     */
47
-    public function __construct($key, $salt)
48
-    {
49
-        $this->key = $key;
50
-        $this->salt = $salt;
51
-        $this->saltedKey = hash_pbkdf2('sha256', $this->key, $this->salt, 1000, 0, true);
52
-    }
33
+	/**
34
+	 * @param string $key a per-site secret string which is used as the base encryption key.
35
+	 * @param string $salt a per-session random string which is used as a salt to generate a per-session key
36
+	 *
37
+	 * The base encryption key needs to stay secret. If an attacker ever gets it, they can read their session,
38
+	 * and even modify & re-sign it.
39
+	 *
40
+	 * The salt is a random per-session string that is used with the base encryption key to create a per-session key.
41
+	 * This (amongst other things) makes sure an attacker can't use a known-plaintext attack to guess the key.
42
+	 *
43
+	 * Normally we could create a salt on encryption, send it to the client as part of the session (it doesn't
44
+	 * need to remain secret), then use the returned salt to decrypt. But we already have the Session ID which makes
45
+	 * a great salt, so no need to generate & handle another one.
46
+	 */
47
+	public function __construct($key, $salt)
48
+	{
49
+		$this->key = $key;
50
+		$this->salt = $salt;
51
+		$this->saltedKey = hash_pbkdf2('sha256', $this->key, $this->salt, 1000, 0, true);
52
+	}
53 53
 
54
-    /**
55
-     * Encrypt and then sign some cleartext
56
-     *
57
-     * @param string $cleartext - The cleartext to encrypt and sign
58
-     * @return string - The encrypted-and-signed message as base64 ASCII.
59
-     */
60
-    public function encrypt($cleartext)
61
-    {
62
-        $cipher = "AES-256-CBC";
63
-        $ivlen = openssl_cipher_iv_length($cipher);
64
-        $iv = openssl_random_pseudo_bytes($ivlen);
65
-        $ciphertext_raw = openssl_encrypt($cleartext, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv);
66
-        $hmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true);
67
-        $ciphertext = base64_encode($iv.$hmac.$ciphertext_raw);
54
+	/**
55
+	 * Encrypt and then sign some cleartext
56
+	 *
57
+	 * @param string $cleartext - The cleartext to encrypt and sign
58
+	 * @return string - The encrypted-and-signed message as base64 ASCII.
59
+	 */
60
+	public function encrypt($cleartext)
61
+	{
62
+		$cipher = "AES-256-CBC";
63
+		$ivlen = openssl_cipher_iv_length($cipher);
64
+		$iv = openssl_random_pseudo_bytes($ivlen);
65
+		$ciphertext_raw = openssl_encrypt($cleartext, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv);
66
+		$hmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true);
67
+		$ciphertext = base64_encode($iv.$hmac.$ciphertext_raw);
68 68
 
69
-        return base64_encode($iv.$hmac.$ciphertext_raw);
70
-    }
69
+		return base64_encode($iv.$hmac.$ciphertext_raw);
70
+	}
71 71
 
72
-    /**
73
-     * Check the signature on an encrypted-and-signed message, and if valid
74
-     * decrypt the content
75
-     *
76
-     * @param string $data - The encrypted-and-signed message as base64 ASCII
77
-     *
78
-     * @return bool|string - The decrypted cleartext or false if signature failed
79
-     */
80
-    public function decrypt($data)
81
-    {
82
-        $c = base64_decode($data);
83
-        $cipher = "AES-256-CBC";
84
-        $ivlen = openssl_cipher_iv_length($cipher);
85
-        $iv = substr($c, 0, $ivlen);
86
-        $hmac = substr($c, $ivlen, $sha2len = 32);
87
-        $ciphertext_raw = substr($c, $ivlen+$sha2len);
88
-        $cleartext = openssl_decrypt($ciphertext_raw, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv);
89
-        $calcmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true);
72
+	/**
73
+	 * Check the signature on an encrypted-and-signed message, and if valid
74
+	 * decrypt the content
75
+	 *
76
+	 * @param string $data - The encrypted-and-signed message as base64 ASCII
77
+	 *
78
+	 * @return bool|string - The decrypted cleartext or false if signature failed
79
+	 */
80
+	public function decrypt($data)
81
+	{
82
+		$c = base64_decode($data);
83
+		$cipher = "AES-256-CBC";
84
+		$ivlen = openssl_cipher_iv_length($cipher);
85
+		$iv = substr($c, 0, $ivlen);
86
+		$hmac = substr($c, $ivlen, $sha2len = 32);
87
+		$ciphertext_raw = substr($c, $ivlen+$sha2len);
88
+		$cleartext = openssl_decrypt($ciphertext_raw, $cipher, $this->saltedKey, $options = OPENSSL_RAW_DATA, $iv);
89
+		$calcmac = hash_hmac('sha256', $ciphertext_raw, $this->saltedKey, $as_binary = true);
90 90
 
91
-        if (hash_equals($hmac, $calcmac)) {
92
-            return $cleartext;
93
-        }
91
+		if (hash_equals($hmac, $calcmac)) {
92
+			return $cleartext;
93
+		}
94 94
 
95
-        return false;
96
-    }
95
+		return false;
96
+	}
97 97
 }
Please login to merge, or discard this patch.
src/Control/HybridSessionMiddleware.php 1 patch
Indentation   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -8,25 +8,25 @@
 block discarded – undo
8 8
 
9 9
 class HybridSessionMiddleware implements HTTPMiddleware
10 10
 {
11
-    public function process(HTTPRequest $request, callable $delegate)
12
-    {
13
-        try {
14
-            // Start session and execute
15
-            $request->getSession()->init($request);
11
+	public function process(HTTPRequest $request, callable $delegate)
12
+	{
13
+		try {
14
+			// Start session and execute
15
+			$request->getSession()->init($request);
16 16
 
17
-            // Generate output
18
-            $response = $delegate($request);
19
-        } finally {
20
-            // Save session data, even if there was an exception
21
-            // Note that save() will start/resume the session if required.
22
-            $request->getSession()->save($request);
17
+			// Generate output
18
+			$response = $delegate($request);
19
+		} finally {
20
+			// Save session data, even if there was an exception
21
+			// Note that save() will start/resume the session if required.
22
+			$request->getSession()->save($request);
23 23
 
24
-            if (HybridSession::is_enabled()) {
25
-                // Close the session
26
-                session_write_close();
27
-            }
28
-        }
24
+			if (HybridSession::is_enabled()) {
25
+				// Close the session
26
+				session_write_close();
27
+			}
28
+		}
29 29
 
30
-        return $response;
31
-    }
30
+		return $response;
31
+	}
32 32
 }
Please login to merge, or discard this patch.
tests/ConfigurationTest.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -9,12 +9,12 @@
 block discarded – undo
9 9
 
10 10
 class ConfigurationTest extends SapphireTest
11 11
 {
12
-    public function testHybridSessionsSessionMiddlewareReplacesCore()
13
-    {
14
-        $this->assertInstanceOf(
15
-            HybridSessionMiddleware::class,
16
-            Injector::inst()->get(SessionMiddleware::class),
17
-            'HybridSession\'s middleware should replace the default SessionMiddleware'
18
-        );
19
-    }
12
+	public function testHybridSessionsSessionMiddlewareReplacesCore()
13
+	{
14
+		$this->assertInstanceOf(
15
+			HybridSessionMiddleware::class,
16
+			Injector::inst()->get(SessionMiddleware::class),
17
+			'HybridSession\'s middleware should replace the default SessionMiddleware'
18
+		);
19
+	}
20 20
 }
Please login to merge, or discard this patch.
src/HybridSession.php 1 patch
Indentation   +160 added lines, -160 removed lines patch added patch discarded remove patch
@@ -9,164 +9,164 @@
 block discarded – undo
9 9
 class HybridSession extends BaseStore
10 10
 {
11 11
 
12
-    /**
13
-     * List of session handlers
14
-     *
15
-     * @var array
16
-     */
17
-    protected $handlers = [];
18
-
19
-    /**
20
-     * True if this session store has been initialised
21
-     *
22
-     * @var bool
23
-     */
24
-    protected static $enabled = false;
25
-
26
-    /**
27
-     * @param SessionHandlerInterface[]
28
-     *
29
-     * @return $this
30
-     */
31
-    public function setHandlers($handlers)
32
-    {
33
-        $this->handlers = $handlers;
34
-        $this->setKey($this->getKey());
35
-
36
-        return $this;
37
-    }
38
-
39
-    /**
40
-     * @param string
41
-     *
42
-     * @return $this
43
-     */
44
-    public function setKey($key)
45
-    {
46
-        parent::setKey($key);
47
-
48
-        foreach ($this->handlers as $handler) {
49
-            $handler->setKey($key);
50
-        }
51
-
52
-        return $this;
53
-    }
54
-
55
-    /**
56
-     * @return SessionHandlerInterface[]
57
-     */
58
-    public function getHandlers()
59
-    {
60
-        return $this->handlers;
61
-    }
62
-
63
-    /**
64
-     * @param string $save_path
65
-     * @param string $name
66
-     *
67
-     * @return bool
68
-     */
69
-    public function open($save_path, $name)
70
-    {
71
-        if ($this->handlers) {
72
-            foreach ($this->handlers as $handler) {
73
-                $handler->open($save_path, $name);
74
-            }
75
-        }
76
-
77
-        return true;
78
-    }
79
-
80
-    /**
81
-     * @return bool
82
-     */
83
-    public function close()
84
-    {
85
-        if ($this->handlers) {
86
-            foreach ($this->handlers as $handler) {
87
-                $handler->close();
88
-            }
89
-        }
90
-
91
-        return true;
92
-    }
93
-
94
-    /**
95
-     * @param string $session_id
96
-     *
97
-     * @return string
98
-     */
99
-    public function read($session_id)
100
-    {
101
-        if ($this->handlers) {
102
-            foreach ($this->handlers as $handler) {
103
-                if ($data = $handler->read($session_id)) {
104
-                    return $data;
105
-                }
106
-            }
107
-        }
108
-
109
-        return '';
110
-    }
111
-
112
-
113
-    public function write($session_id, $session_data)
114
-    {
115
-        $result = false;
116
-        if ($this->handlers) {
117
-            foreach ($this->handlers as $handler) {
118
-                if ($handler->write($session_id, $session_data)) {
119
-                    $result = true;
120
-                }
121
-            }
122
-        }
123
-        return $result;
124
-    }
125
-
126
-    public function destroy($session_id)
127
-    {
128
-        if ($this->handlers) {
129
-            foreach ($this->handlers as $handler) {
130
-                $handler->destroy($session_id);
131
-            }
132
-        }
133
-        return true;
134
-    }
135
-
136
-    public function gc($maxlifetime)
137
-    {
138
-        if ($this->handlers) {
139
-            foreach ($this->handlers as $handler) {
140
-                $handler->gc($maxlifetime);
141
-            }
142
-        }
143
-    }
144
-
145
-    /**
146
-     * Register the session handler as the default
147
-     *
148
-     * @param string $key Desired session key
149
-     */
150
-    public static function init($key = null)
151
-    {
152
-        $instance = Injector::inst()->get(__CLASS__);
153
-
154
-        if (empty($key)) {
155
-            user_error(
156
-                'HybridSession::init() was not given a $key. Disabling cookie-based storage',
157
-                E_USER_WARNING
158
-            );
159
-        } else {
160
-            $instance->setKey($key);
161
-        }
162
-
163
-        session_set_save_handler($instance, true);
164
-
165
-        self::$enabled = true;
166
-    }
167
-
168
-    public static function is_enabled()
169
-    {
170
-        return self::$enabled;
171
-    }
12
+	/**
13
+	 * List of session handlers
14
+	 *
15
+	 * @var array
16
+	 */
17
+	protected $handlers = [];
18
+
19
+	/**
20
+	 * True if this session store has been initialised
21
+	 *
22
+	 * @var bool
23
+	 */
24
+	protected static $enabled = false;
25
+
26
+	/**
27
+	 * @param SessionHandlerInterface[]
28
+	 *
29
+	 * @return $this
30
+	 */
31
+	public function setHandlers($handlers)
32
+	{
33
+		$this->handlers = $handlers;
34
+		$this->setKey($this->getKey());
35
+
36
+		return $this;
37
+	}
38
+
39
+	/**
40
+	 * @param string
41
+	 *
42
+	 * @return $this
43
+	 */
44
+	public function setKey($key)
45
+	{
46
+		parent::setKey($key);
47
+
48
+		foreach ($this->handlers as $handler) {
49
+			$handler->setKey($key);
50
+		}
51
+
52
+		return $this;
53
+	}
54
+
55
+	/**
56
+	 * @return SessionHandlerInterface[]
57
+	 */
58
+	public function getHandlers()
59
+	{
60
+		return $this->handlers;
61
+	}
62
+
63
+	/**
64
+	 * @param string $save_path
65
+	 * @param string $name
66
+	 *
67
+	 * @return bool
68
+	 */
69
+	public function open($save_path, $name)
70
+	{
71
+		if ($this->handlers) {
72
+			foreach ($this->handlers as $handler) {
73
+				$handler->open($save_path, $name);
74
+			}
75
+		}
76
+
77
+		return true;
78
+	}
79
+
80
+	/**
81
+	 * @return bool
82
+	 */
83
+	public function close()
84
+	{
85
+		if ($this->handlers) {
86
+			foreach ($this->handlers as $handler) {
87
+				$handler->close();
88
+			}
89
+		}
90
+
91
+		return true;
92
+	}
93
+
94
+	/**
95
+	 * @param string $session_id
96
+	 *
97
+	 * @return string
98
+	 */
99
+	public function read($session_id)
100
+	{
101
+		if ($this->handlers) {
102
+			foreach ($this->handlers as $handler) {
103
+				if ($data = $handler->read($session_id)) {
104
+					return $data;
105
+				}
106
+			}
107
+		}
108
+
109
+		return '';
110
+	}
111
+
112
+
113
+	public function write($session_id, $session_data)
114
+	{
115
+		$result = false;
116
+		if ($this->handlers) {
117
+			foreach ($this->handlers as $handler) {
118
+				if ($handler->write($session_id, $session_data)) {
119
+					$result = true;
120
+				}
121
+			}
122
+		}
123
+		return $result;
124
+	}
125
+
126
+	public function destroy($session_id)
127
+	{
128
+		if ($this->handlers) {
129
+			foreach ($this->handlers as $handler) {
130
+				$handler->destroy($session_id);
131
+			}
132
+		}
133
+		return true;
134
+	}
135
+
136
+	public function gc($maxlifetime)
137
+	{
138
+		if ($this->handlers) {
139
+			foreach ($this->handlers as $handler) {
140
+				$handler->gc($maxlifetime);
141
+			}
142
+		}
143
+	}
144
+
145
+	/**
146
+	 * Register the session handler as the default
147
+	 *
148
+	 * @param string $key Desired session key
149
+	 */
150
+	public static function init($key = null)
151
+	{
152
+		$instance = Injector::inst()->get(__CLASS__);
153
+
154
+		if (empty($key)) {
155
+			user_error(
156
+				'HybridSession::init() was not given a $key. Disabling cookie-based storage',
157
+				E_USER_WARNING
158
+			);
159
+		} else {
160
+			$instance->setKey($key);
161
+		}
162
+
163
+		session_set_save_handler($instance, true);
164
+
165
+		self::$enabled = true;
166
+	}
167
+
168
+	public static function is_enabled()
169
+	{
170
+		return self::$enabled;
171
+	}
172 172
 }
Please login to merge, or discard this patch.