Skip to content

Commit bcd105c

Browse files
committed
Add the ZMQAuth class
The ZMQAuth class wraps the CZMQ zauth API. Instances of the ZMQAuth class can: * install a ZAP handler for a ZMQContext * whitelist/blacklist IP addresses * use plain or curve authentication for one or more domains
1 parent fe8d16e commit bcd105c

7 files changed

+308
-1
lines changed

api.php

+3
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ public function __construct(ZMQContext $context) {}
625625
* whitelisted addresses are treated as if they were blacklisted.
626626
*
627627
* @param string $address
628+
* @return ZMQAuth Provides a fluent interface
628629
*/
629630
public function allow($address) {}
630631

@@ -638,6 +639,7 @@ public function allow($address) {}
638639
* whitelist will be used to authenticate incoming connections.
639640
*
640641
* @param string $address
642+
* @return ZMQAuth Provides a fluent interface
641643
*/
642644
public function deny($address) {}
643645

@@ -659,6 +661,7 @@ public function deny($address) {}
659661
* @param string $domain The ZAP domain. Use "*" to configure the PLAIN or
660662
* CURVE authentication mechanism for all domains
661663
* @param string $filename
664+
* @return ZMQAuth Provides a fluent interface
662665
*/
663666
public function configure($type, $domain, $filename) {}
664667
}

package.xml

+3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@
9696
<file name="041-cert-meta.phpt" role="test" />
9797
<file name="042-cert-save.phpt" role="test" />
9898
<file name="043-cert-load.phpt" role="test" />
99+
<file name="044-auth-construct.phpt" role="test" />
100+
<file name="045-auth-allow-deny.phpt" role="test" />
99101
<file name="046-cert-apply.phpt" role="test" />
102+
<file name="047-auth-configure.phpt" role="test" />
100103
<file name="bug_gh_43.phpt" role="test" />
101104
<file name="bug_gh_49.phpt" role="test" />
102105
<file name="bug_gh_50.phpt" role="test" />

php_zmq_private.h

+11
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ typedef struct _php_zmq_device_object {
245245

246246
#define PHP_ZMQ_VERSION_LEN 24
247247

248+
#ifdef HAVE_CZMQ_2
249+
# define PHP_ZMQ_AUTH_TYPE_PLAIN 0
250+
# define PHP_ZMQ_AUTH_TYPE_CURVE 1
251+
#endif
252+
248253
PHP_METHOD(zmqsocket, getsockopt);
249254
PHP_METHOD(zmqsocket, setsockopt);
250255
zend_bool php_zmq_device(php_zmq_device_object *intern TSRMLS_DC);
@@ -275,6 +280,12 @@ typedef struct _php_zmq_cert {
275280
zend_object zend_object;
276281
zcert_t *zcert;
277282
} php_zmq_cert;
283+
284+
typedef struct _php_zmq_auth {
285+
zend_object zend_object;
286+
zctx_t *shadow_context;
287+
zauth_t *zauth;
288+
} php_zmq_auth;
278289
#endif
279290

280291
#endif /* _PHP_ZMQ_PRIVATE_H_ */

tests/044-auth-construct.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Test a ZMQAuth can be constructed.
3+
--SKIPIF--
4+
<?php
5+
require_once __DIR__ . '/skipif.inc';
6+
7+
if (!class_exists('ZMQAuth')) {
8+
die('skip');
9+
}
10+
--FILE--
11+
<?php
12+
13+
$context = new ZMQContext();
14+
$auth = new ZMQAuth($context);
15+
var_dump((bool)$auth);
16+
--EXPECT--
17+
bool(true)

tests/045-auth-allow-deny.phpt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test a ZMQAuth can whitelist or blacklist an IP address.
3+
--SKIPIF--
4+
<?php
5+
require_once __DIR__ . '/skipif.inc';
6+
7+
if (!class_exists('ZMQAuth')) {
8+
die('skip');
9+
}
10+
--FILE--
11+
<?php
12+
13+
$context = new ZMQContext();
14+
$auth = new ZMQAuth($context);
15+
var_dump($auth->allow('127.0.0.1') === $auth);
16+
var_dump($auth->deny('192.168.0.1') === $auth);
17+
--EXPECT--
18+
bool(true)
19+
bool(true)

tests/047-auth-configure.phpt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
Test a ZMQAuth can be configured.
3+
--SKIPIF--
4+
<?php
5+
require_once __DIR__ . '/skipif.inc';
6+
7+
if (!class_exists('ZMQAuth')) {
8+
die('skip');
9+
}
10+
--FILE--
11+
<?php
12+
13+
define('TEST_DIR', '/tmp');
14+
define('PASSWORDS_FILE', TEST_DIR . '/passwords');
15+
define('CERTS_DIR', '/tmp/certs');
16+
define('CERT_FILE', CERTS_DIR . '/cert');
17+
18+
$context = new ZMQContext();
19+
$auth = new ZMQAuth($context);
20+
21+
// Test a ZMQAuth can be configured to use PLAIN authentication.
22+
touch(PASSWORDS_FILE);
23+
var_dump($auth->configure(ZMQAuth::AUTH_TYPE_PLAIN, '*', PASSWORDS_FILE) === $auth);
24+
unlink(PASSWORDS_FILE);
25+
26+
// Test a ZMQAuth can be configured to use CURVE authentication.
27+
mkdir(CERTS_DIR);
28+
$cert = new ZMQCert();
29+
$cert->save(CERT_FILE);
30+
31+
var_dump($auth->configure(ZMQAuth::AUTH_TYPE_CURVE, '*', CERTS_DIR) === $auth);
32+
33+
// Test ZMQAuth#configure throws an exception when the auth type isn't
34+
// recognised.
35+
try {
36+
$auth->configure(-1, '*', CERTS_DIR);
37+
} catch (ZMQAuthException $e) {
38+
var_dump($e->getMessage());
39+
}
40+
41+
unlink(CERT_FILE);
42+
unlink(CERT_FILE . '_secret');
43+
rmdir(CERTS_DIR);
44+
--EXPECT--
45+
bool(true)
46+
bool(true)
47+
string(62) "Unknown auth type. Are you using one of the ZMQAuth constants?"

0 commit comments

Comments
 (0)