refactor: move api to emqx_rule_engine_jwt module
This commit is contained in:
parent
270f47aafc
commit
b4837302aa
|
@ -0,0 +1,45 @@
|
|||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(emqx_rule_engine_jwt).
|
||||
|
||||
-include_lib("emqx_rule_engine/include/rule_engine.hrl").
|
||||
-include_lib("emqx_rule_engine/include/rule_actions.hrl").
|
||||
|
||||
%% API
|
||||
-export([ lookup_jwt/1
|
||||
, lookup_jwt/2
|
||||
]).
|
||||
|
||||
-type jwt() :: binary().
|
||||
|
||||
-spec lookup_jwt(resource_id()) -> {ok, jwt()} | {error, not_found}.
|
||||
lookup_jwt(ResourceId) ->
|
||||
?MODULE:lookup_jwt(?JWT_TABLE, ResourceId).
|
||||
|
||||
-spec lookup_jwt(ets:table(), resource_id()) -> {ok, jwt()} | {error, not_found}.
|
||||
lookup_jwt(TId, ResourceId) ->
|
||||
try
|
||||
case ets:lookup(TId, {ResourceId, jwt}) of
|
||||
[{{ResourceId, jwt}, JWT}] ->
|
||||
{ok, JWT};
|
||||
[] ->
|
||||
{error, not_found}
|
||||
end
|
||||
catch
|
||||
error:badarg ->
|
||||
{error, not_found}
|
||||
end.
|
|
@ -20,8 +20,6 @@
|
|||
|
||||
%% API
|
||||
-export([ start_link/2
|
||||
, lookup_jwt/1
|
||||
, lookup_jwt/2
|
||||
]).
|
||||
|
||||
%% gen_server API
|
||||
|
@ -84,24 +82,6 @@ start_link(#{ private_key := _
|
|||
Ref) ->
|
||||
gen_server:start_link(?MODULE, {Config, Ref}, []).
|
||||
|
||||
-spec lookup_jwt(resource_id()) -> {ok, jwt()} | {error, not_found}.
|
||||
lookup_jwt(ResourceId) ->
|
||||
?MODULE:lookup_jwt(?JWT_TABLE, ResourceId).
|
||||
|
||||
-spec lookup_jwt(ets:table(), resource_id()) -> {ok, jwt()} | {error, not_found}.
|
||||
lookup_jwt(TId, ResourceId) ->
|
||||
try
|
||||
case ets:lookup(TId, {ResourceId, jwt}) of
|
||||
[{{ResourceId, jwt}, JWT}] ->
|
||||
{ok, JWT};
|
||||
[] ->
|
||||
{error, not_found}
|
||||
end
|
||||
catch
|
||||
error:badarg ->
|
||||
{error, not_found}
|
||||
end.
|
||||
|
||||
%%-----------------------------------------------------------------------------------------
|
||||
%% gen_server API
|
||||
%%-----------------------------------------------------------------------------------------
|
||||
|
|
|
@ -133,9 +133,9 @@ t_refresh(_Config) ->
|
|||
emqx_rule_engine_jwt_worker:start_link(Config, Ref),
|
||||
#{?snk_kind := jwt_worker_token_stored},
|
||||
5_000),
|
||||
{ok, FirstJWT} = emqx_rule_engine_jwt_worker:lookup_jwt(Table, ResourceId),
|
||||
{ok, FirstJWT} = emqx_rule_engine_jwt:lookup_jwt(Table, ResourceId),
|
||||
?block_until(#{?snk_kind := rule_engine_jwt_worker_refresh}, 15_000),
|
||||
{ok, SecondJWT} = emqx_rule_engine_jwt_worker:lookup_jwt(Table, ResourceId),
|
||||
{ok, SecondJWT} = emqx_rule_engine_jwt:lookup_jwt(Table, ResourceId),
|
||||
?assertNot(is_expired(SecondJWT)),
|
||||
?assert(is_expired(FirstJWT)),
|
||||
{FirstJWT, SecondJWT}
|
||||
|
@ -182,7 +182,7 @@ t_lookup_ok(_Config) ->
|
|||
500 ->
|
||||
error(timeout)
|
||||
end,
|
||||
Res = emqx_rule_engine_jwt_worker:lookup_jwt(Table, ResourceId),
|
||||
Res = emqx_rule_engine_jwt:lookup_jwt(Table, ResourceId),
|
||||
?assertMatch({ok, _}, Res),
|
||||
{ok, JWT} = Res,
|
||||
?assert(is_binary(JWT)),
|
||||
|
@ -212,14 +212,14 @@ t_lookup_not_found(_Config) ->
|
|||
Table = ets:new(test_jwt_table, [ordered_set, public]),
|
||||
InexistentResource = <<"xxx">>,
|
||||
?assertEqual({error, not_found},
|
||||
emqx_rule_engine_jwt_worker:lookup_jwt(Table, InexistentResource)),
|
||||
emqx_rule_engine_jwt:lookup_jwt(Table, InexistentResource)),
|
||||
ok.
|
||||
|
||||
t_lookup_badarg(_Config) ->
|
||||
InexistentTable = i_dont_exist,
|
||||
InexistentResource = <<"xxx">>,
|
||||
?assertEqual({error, not_found},
|
||||
emqx_rule_engine_jwt_worker:lookup_jwt(InexistentTable, InexistentResource)),
|
||||
emqx_rule_engine_jwt:lookup_jwt(InexistentTable, InexistentResource)),
|
||||
ok.
|
||||
|
||||
t_start_supervised_worker(_Config) ->
|
||||
|
|
Loading…
Reference in New Issue