diff --git a/apps/emqx_gateway_coap/src/emqx_coap_channel.erl b/apps/emqx_gateway_coap/src/emqx_coap_channel.erl index a48589e36..bcafda41f 100644 --- a/apps/emqx_gateway_coap/src/emqx_coap_channel.erl +++ b/apps/emqx_gateway_coap/src/emqx_coap_channel.erl @@ -486,23 +486,21 @@ enrich_conninfo( conninfo = ConnInfo } ) -> - ClientId = - case maps:get(<<"clientid">>, Queries, undefined) of - undefined -> - emqx_gateway_utils:random_clientid(coap); - ClientId0 -> - ClientId0 - end, - Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)), - NConnInfo = ConnInfo#{ - clientid => ClientId, - proto_name => <<"CoAP">>, - proto_ver => <<"1">>, - clean_start => true, - keepalive => Interval, - expiry_interval => 0 - }, - {ok, Channel#channel{conninfo = NConnInfo}}. + case Queries of + #{<<"clientid">> := ClientId} -> + Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)), + NConnInfo = ConnInfo#{ + clientid => ClientId, + proto_name => <<"CoAP">>, + proto_ver => <<"1">>, + clean_start => true, + keepalive => Interval, + expiry_interval => 0 + }, + {ok, Channel#channel{conninfo = NConnInfo}}; + _ -> + {error, "clientid is required", Channel} + end. enrich_clientinfo( {Queries, Msg}, diff --git a/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl b/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl index 999493a79..95fdf8cca 100644 --- a/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl +++ b/apps/emqx_gateway_coap/test/emqx_coap_SUITE.erl @@ -133,6 +133,42 @@ t_connection(_) -> end, do(Action). +t_connection_optional_params(_) -> + UsernamePasswordAreOptional = + fun(Channel) -> + URI = + ?MQTT_PREFIX ++ + "/connection?clientid=client1", + Req = make_req(post), + {ok, created, Data} = do_request(Channel, URI, Req), + #coap_content{payload = Token0} = Data, + Token = binary_to_list(Token0), + + timer:sleep(100), + ?assertNotEqual( + [], + emqx_gateway_cm_registry:lookup_channels(coap, <<"client1">>) + ), + + disconnection(Channel, Token), + + timer:sleep(100), + ?assertEqual( + [], + emqx_gateway_cm_registry:lookup_channels(coap, <<"client1">>) + ) + end, + ClientIdIsRequired = + fun(Channel) -> + URI = + ?MQTT_PREFIX ++ + "/connection", + Req = make_req(post), + {error, bad_request, _} = do_request(Channel, URI, Req) + end, + do(UsernamePasswordAreOptional), + do(ClientIdIsRequired). + t_connection_with_authn_failed(_) -> ChId = {{127, 0, 0, 1}, 5683}, {ok, Sock} = er_coap_udp_socket:start_link(), diff --git a/apps/emqx_gateway_exproto/src/emqx_exproto_channel.erl b/apps/emqx_gateway_exproto/src/emqx_exproto_channel.erl index 2a144ffeb..5de597920 100644 --- a/apps/emqx_gateway_exproto/src/emqx_exproto_channel.erl +++ b/apps/emqx_gateway_exproto/src/emqx_exproto_channel.erl @@ -782,7 +782,7 @@ enrich_clientinfo(InClientInfo = #{proto_name := ProtoName}, ClientInfo) -> default_conninfo(ConnInfo) -> ConnInfo#{ clean_start => true, - clientid => anonymous_clientid(), + clientid => emqx_gateway_utils:random_clientid(exproto), username => undefined, conn_props => #{}, connected => true, @@ -822,6 +822,3 @@ proto_name_to_protocol(<<>>) -> exproto; proto_name_to_protocol(ProtoName) when is_binary(ProtoName) -> binary_to_atom(ProtoName). - -anonymous_clientid() -> - iolist_to_binary(["exproto-", emqx_utils:gen_id()]).