Code and tests for returning single stage auth data back to client.
parent
494f35bd8d
commit
0f6e51d582
@ -0,0 +1,88 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Multi tests for extended auth with a single step - multiple plugins at once.
|
||||||
|
# * Error in plugin
|
||||||
|
# * No matching authentication method
|
||||||
|
# * Matching authentication method, but auth rejected
|
||||||
|
# * Matching authentication method, auth succeeds
|
||||||
|
# * Matching authentication method, auth succeeds, new auth data sent back to client
|
||||||
|
|
||||||
|
|
||||||
|
from mosq_test_helper import *
|
||||||
|
|
||||||
|
def write_config(filename, port):
|
||||||
|
with open(filename, 'w') as f:
|
||||||
|
f.write("port %d\n" % (port))
|
||||||
|
f.write("auth_plugin c/auth_plugin_extended_single.so\n")
|
||||||
|
f.write("auth_plugin c/auth_plugin_extended_single2.so\n")
|
||||||
|
|
||||||
|
port = mosq_test.get_port()
|
||||||
|
conf_file = os.path.basename(__file__).replace('.py', '.conf')
|
||||||
|
|
||||||
|
|
||||||
|
def do_test(suffix):
|
||||||
|
write_config(conf_file, port)
|
||||||
|
rc = 1
|
||||||
|
# Single, error in plugin
|
||||||
|
props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_METHOD, "error%s" % (suffix))
|
||||||
|
connect1_packet = mosq_test.gen_connect("client-params-test1", keepalive=42, proto_ver=5, properties=props)
|
||||||
|
|
||||||
|
# Single, no matching authentication method
|
||||||
|
props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_METHOD, "non-matching%s" % (suffix))
|
||||||
|
connect2_packet = mosq_test.gen_connect("client-params-test2", keepalive=42, proto_ver=5, properties=props)
|
||||||
|
connack2_packet = mosq_test.gen_connack(rc=mqtt5_rc.MQTT_RC_BAD_AUTHENTICATION_METHOD, proto_ver=5, properties=None)
|
||||||
|
|
||||||
|
# Single step, matching method, failure
|
||||||
|
props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_METHOD, "single%s" % (suffix))
|
||||||
|
props += mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_DATA, "baddata")
|
||||||
|
connect3_packet = mosq_test.gen_connect("client-params-test3", keepalive=42, proto_ver=5, properties=props)
|
||||||
|
connack3_packet = mosq_test.gen_connack(rc=mqtt5_rc.MQTT_RC_NOT_AUTHORIZED, proto_ver=5, properties=None)
|
||||||
|
|
||||||
|
# Single step, matching method, success
|
||||||
|
props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_METHOD, "single%s" % (suffix))
|
||||||
|
props += mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_DATA, "data")
|
||||||
|
connect4_packet = mosq_test.gen_connect("client-params-test5", keepalive=42, proto_ver=5, properties=props)
|
||||||
|
props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_METHOD, "single%s" % (suffix))
|
||||||
|
connack4_packet = mosq_test.gen_connack(rc=0, proto_ver=5, properties=props)
|
||||||
|
|
||||||
|
# Single step, matching method, success, auth data back to client
|
||||||
|
props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_METHOD, "mirror%s" % (suffix))
|
||||||
|
props += mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_DATA, "somedata")
|
||||||
|
connect5_packet = mosq_test.gen_connect("client-params-test6", keepalive=42, proto_ver=5, properties=props)
|
||||||
|
props = mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_METHOD, "mirror%s" % (suffix))
|
||||||
|
props += mqtt5_props.gen_string_prop(mqtt5_props.PROP_AUTHENTICATION_DATA, "atademos")
|
||||||
|
connack5_packet = mosq_test.gen_connack(rc=0, proto_ver=5, properties=props)
|
||||||
|
|
||||||
|
|
||||||
|
broker = mosq_test.start_broker(filename=os.path.basename(__file__), use_conf=True, port=port)
|
||||||
|
|
||||||
|
try:
|
||||||
|
sock = mosq_test.do_client_connect(connect1_packet, b"", timeout=20, port=port)
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
sock = mosq_test.do_client_connect(connect2_packet, connack2_packet, timeout=20, port=port)
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
sock = mosq_test.do_client_connect(connect3_packet, connack3_packet, timeout=20, port=port)
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
sock = mosq_test.do_client_connect(connect4_packet, connack4_packet, timeout=20, port=port)
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
sock = mosq_test.do_client_connect(connect5_packet, connack5_packet, timeout=20, port=port)
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
rc = 0
|
||||||
|
finally:
|
||||||
|
os.remove(conf_file)
|
||||||
|
broker.terminate()
|
||||||
|
broker.wait()
|
||||||
|
(stdo, stde) = broker.communicate()
|
||||||
|
if rc:
|
||||||
|
print(stde.decode('utf-8'))
|
||||||
|
exit(rc)
|
||||||
|
|
||||||
|
do_test("")
|
||||||
|
do_test("2")
|
||||||
|
exit(0)
|
||||||
|
|
@ -0,0 +1,78 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <mosquitto.h>
|
||||||
|
#include <mosquitto_broker.h>
|
||||||
|
#include <mosquitto_plugin.h>
|
||||||
|
|
||||||
|
int mosquitto_auth_plugin_version(void)
|
||||||
|
{
|
||||||
|
return MOSQ_AUTH_PLUGIN_VERSION;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mosquitto_auth_plugin_init(void **user_data, struct mosquitto_opt *auth_opts, int auth_opt_count)
|
||||||
|
{
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mosquitto_auth_plugin_cleanup(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count)
|
||||||
|
{
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mosquitto_auth_security_init(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count, bool reload)
|
||||||
|
{
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mosquitto_auth_security_cleanup(void *user_data, struct mosquitto_opt *auth_opts, int auth_opt_count, bool reload)
|
||||||
|
{
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mosquitto_auth_acl_check(void *user_data, int access, struct mosquitto *client, const struct mosquitto_acl_msg *msg)
|
||||||
|
{
|
||||||
|
return MOSQ_ERR_PLUGIN_DEFER;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int mosquitto_auth_start(void *user_data, struct mosquitto *client, const char *method, const void *data, uint16_t data_len, void **data_out, uint16_t *data_out_len)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if(!strcmp(method, "error2")){
|
||||||
|
return MOSQ_ERR_INVAL;
|
||||||
|
}else if(!strcmp(method, "non-matching2")){
|
||||||
|
return MOSQ_ERR_NOT_SUPPORTED;
|
||||||
|
}else if(!strcmp(method, "single2")){
|
||||||
|
data_len = data_len>strlen("data")?strlen("data"):data_len;
|
||||||
|
if(!memcmp(data, "data", data_len)){
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}else{
|
||||||
|
return MOSQ_ERR_AUTH;
|
||||||
|
}
|
||||||
|
}else if(!strcmp(method, "change2")){
|
||||||
|
return mosquitto_set_username(client, "new_username");
|
||||||
|
}else if(!strcmp(method, "mirror2")){
|
||||||
|
if(data_len > 0){
|
||||||
|
*data_out = malloc(data_len);
|
||||||
|
if(!(*data_out)){
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
for(i=0; i<data_len; i++){
|
||||||
|
((uint8_t *)(*data_out))[i] = ((uint8_t *)data)[data_len-i-1];
|
||||||
|
}
|
||||||
|
*data_out_len = data_len;
|
||||||
|
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}else{
|
||||||
|
return MOSQ_ERR_INVAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MOSQ_ERR_NOT_SUPPORTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mosquitto_auth_continue(void *user_data, struct mosquitto *client, const char *method, const void *data, int data_len)
|
||||||
|
{
|
||||||
|
return MOSQ_ERR_AUTH;
|
||||||
|
}
|
Loading…
Reference in New Issue