This commit is contained in:
anlicheng 2026-04-24 17:43:23 +08:00
parent 650576dee8
commit 464c04a277
2 changed files with 19 additions and 33 deletions

View File

@ -1,31 +0,0 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @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.

View File

@ -223,7 +223,7 @@ handle_request_frame(#'RequestFrame'{packet_id = PacketId,
Transport, Socket, State) -> Transport, Socket, State) ->
logger:debug("[ws_channel] auth uuid: ~p", [UUID]), logger:debug("[ws_channel] auth uuid: ~p", [UUID]),
case iot_auth:check(Token, UUID, Timestamp) of case auth(Token, UUID, Timestamp) of
ok -> ok ->
case iot_api_client:get_host_by_uuid(UUID) of case iot_api_client:get_host_by_uuid(UUID) of
undefined -> undefined ->
@ -250,7 +250,7 @@ handle_request_frame(#'RequestFrame'{packet_id = PacketId,
end; end;
{error, Reason} -> {error, Reason} ->
send_reply_frame(Transport, Socket, PacketId, {error, #'ReplyFrame.Error'{code = 2, message = 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} {stop, Reason, State}
end; end;
handle_request_frame(#'RequestFrame'{packet_id = PacketId, body = {container_request, ContainerRequest}}, _Transport, _Socket, State) -> 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)}; {error, Code, unicode:characters_to_binary(Message)};
decode_reply(undefined) -> decode_reply(undefined) ->
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.