mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00
NOISSUE - Add MQTT UserName check on register and InstanceId in Redis (#884)
* Add InstanceId and verify UserName Signed-off-by: drasko <drasko.draskovic@gmail.com> * Fix thingID check (compare with UserName) Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
This commit is contained in:
parent
351b04cd2e
commit
b3ce48aca6
@ -65,8 +65,14 @@ auth_on_register({_IpAddr, _Port} = Peer, {_MountPoint, _ClientId} = SubscriberI
|
|||||||
%% 5. return {error, whatever} -> CONNACK_AUTH is sent
|
%% 5. return {error, whatever} -> CONNACK_AUTH is sent
|
||||||
|
|
||||||
case identify(Password) of
|
case identify(Password) of
|
||||||
{ok, _Id} ->
|
{ok, Id} ->
|
||||||
ok;
|
case Id of
|
||||||
|
UserName ->
|
||||||
|
ok;
|
||||||
|
_ ->
|
||||||
|
error_logger:info_msg("auth_on_register failed - Id: ~p, Username: ~p", [Id, UserName]),
|
||||||
|
{error, invalid_credentials}
|
||||||
|
end;
|
||||||
Other ->
|
Other ->
|
||||||
Other
|
Other
|
||||||
end.
|
end.
|
||||||
@ -142,11 +148,13 @@ auth_on_subscribe(UserName, ClientId, [{Topic, _QoS}|_] = Topics) ->
|
|||||||
%%% Redis ES
|
%%% Redis ES
|
||||||
publish_event(UserName, Type) ->
|
publish_event(UserName, Type) ->
|
||||||
Timestamp = os:system_time(second),
|
Timestamp = os:system_time(second),
|
||||||
|
[{_, InstanceId}] = ets:lookup(mfx_cfg, instance_id),
|
||||||
KeyValuePairs = [
|
KeyValuePairs = [
|
||||||
"mainflux.mqtt", "*",
|
"mainflux.mqtt", "*",
|
||||||
"thing_id", binary_to_list(UserName),
|
"thing_id", binary_to_list(UserName),
|
||||||
"timestamp", integer_to_list(Timestamp),
|
"timestamp", integer_to_list(Timestamp),
|
||||||
"event_type", Type
|
"event_type", Type,
|
||||||
|
"instance", InstanceId
|
||||||
],
|
],
|
||||||
mfx_redis:publish(KeyValuePairs).
|
mfx_redis:publish(KeyValuePairs).
|
||||||
|
|
||||||
|
@ -26,11 +26,17 @@ start(_StartType, _StartArgs) ->
|
|||||||
false -> "tcp://localhost:6379";
|
false -> "tcp://localhost:6379";
|
||||||
RedisEnv -> RedisEnv
|
RedisEnv -> RedisEnv
|
||||||
end,
|
end,
|
||||||
|
InstanceId = case os:getenv("MF_MQTT_INSTANCE_ID") of
|
||||||
|
false -> "";
|
||||||
|
InstanceEnv -> InstanceEnv
|
||||||
|
end,
|
||||||
|
|
||||||
|
|
||||||
ets:insert(mfx_cfg, [
|
ets:insert(mfx_cfg, [
|
||||||
{grpc_url, GrpcUrl},
|
{grpc_url, GrpcUrl},
|
||||||
{nats_url, NatsUrl},
|
{nats_url, NatsUrl},
|
||||||
{redis_url, RedisUrl}
|
{redis_url, RedisUrl},
|
||||||
|
{instance_id, InstanceId}
|
||||||
]),
|
]),
|
||||||
|
|
||||||
% Also, init one ETS table for keeping the #{ClientId => Username} mapping
|
% Also, init one ETS table for keeping the #{ClientId => Username} mapping
|
||||||
|
@ -45,7 +45,7 @@ handle_call({identify, Message}, _From, #state{conn = GrpcConn} = State) ->
|
|||||||
|
|
||||||
case HttpStatus of
|
case HttpStatus of
|
||||||
200 ->
|
200 ->
|
||||||
{reply, {ok, ThingId}, State};
|
{reply, {ok, list_to_binary(ThingId)}, State};
|
||||||
_ ->
|
_ ->
|
||||||
{reply, {error, HttpStatus}, error}
|
{reply, {error, HttpStatus}, error}
|
||||||
end;
|
end;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user