fix auth
This commit is contained in:
parent
650576dee8
commit
464c04a277
@ -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.
|
|
||||||
@ -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.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user