From 464c04a27795baa9e10ba1bffab0eae07d67d0f9 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Fri, 24 Apr 2026 17:43:23 +0800 Subject: [PATCH] fix auth --- src/adapters/auth/iot_auth.erl | 31 ------------------------------- src/transport/tcp/ssl_channel.erl | 21 +++++++++++++++++++-- 2 files changed, 19 insertions(+), 33 deletions(-) delete mode 100644 src/adapters/auth/iot_auth.erl diff --git a/src/adapters/auth/iot_auth.erl b/src/adapters/auth/iot_auth.erl deleted file mode 100644 index e8ebece..0000000 --- a/src/adapters/auth/iot_auth.erl +++ /dev/null @@ -1,31 +0,0 @@ -%%%------------------------------------------------------------------- -%%% @author aresei -%%% @copyright (C) 2023, -%%% @doc -%%% 授权校验模块 -%%% @end -%%% Created : 27. 6月 2023 09:48 -%%%------------------------------------------------------------------- --module(iot_auth). --author("aresei"). - -%% API --export([check/3]). - -%% 检测token是否是合法值 --spec check(Token :: binary(), UUID :: binary(), Timestamp :: integer()) -> - ok | {error, Reason :: binary()}. -check(Token, UUID, Timestamp) when is_binary(Token), is_binary(UUID), is_integer(Timestamp) -> - BinTimestamp = integer_to_binary(Timestamp), - %% 1分钟内有效 - case iot_util:current_time() - Timestamp =< 60 of - true -> - case efka_client_store:auth(UUID, Token) of - true -> - ok; - false -> - {error, <<"invalid token">>} - end; - false -> - {error, <<"invalid timestamp">>} - end. \ No newline at end of file diff --git a/src/transport/tcp/ssl_channel.erl b/src/transport/tcp/ssl_channel.erl index caab1c5..6b6ec2e 100644 --- a/src/transport/tcp/ssl_channel.erl +++ b/src/transport/tcp/ssl_channel.erl @@ -223,7 +223,7 @@ handle_request_frame(#'RequestFrame'{packet_id = PacketId, Transport, Socket, State) -> logger:debug("[ws_channel] auth uuid: ~p", [UUID]), - case iot_auth:check(Token, UUID, Timestamp) of + case auth(Token, UUID, Timestamp) of ok -> case iot_api_client:get_host_by_uuid(UUID) of undefined -> @@ -250,7 +250,7 @@ handle_request_frame(#'RequestFrame'{packet_id = PacketId, end; {error, Reason} -> send_reply_frame(Transport, Socket, PacketId, {error, #'ReplyFrame.Error'{code = 2, message = Reason}}), - logger:warning("[ws_channel] uuid: ~p, user: ~p, auth failed, reason: ~p", [UUID, Username, Reason]), + logger:warning("[ws_channel] uuid: ~p, token: ~p, auth failed, reason: ~p", [UUID, Token, Reason]), {stop, Reason, State} end; handle_request_frame(#'RequestFrame'{packet_id = PacketId, body = {container_request, ContainerRequest}}, _Transport, _Socket, State) -> @@ -320,3 +320,20 @@ decode_reply({error, #'ReplyFrame.Error'{code = Code, message = Message}}) -> {error, Code, unicode:characters_to_binary(Message)}; decode_reply(undefined) -> undefined. + +%% 检测token是否是合法值 +-spec auth(Token :: binary(), UUID :: binary(), Timestamp :: integer()) -> + ok | {error, Reason :: binary()}. +auth(Token, UUID, Timestamp) when is_binary(Token), is_binary(UUID), is_integer(Timestamp) -> + %% 1分钟内有效 + case iot_util:current_time() - Timestamp =< 60 of + true -> + case efka_client_store:auth(UUID, Token) of + true -> + ok; + false -> + {error, <<"invalid token">>} + end; + false -> + {error, <<"invalid timestamp">>} + end.