emqx_authn_test_lib.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_authn_test_lib).
  17. -include("emqx_authn.hrl").
  18. -compile(nowarn_export_all).
  19. -compile(export_all).
  20. authenticator_example(Id) ->
  21. #{Id := #{value := Example}} = emqx_authn_api:authenticator_examples(),
  22. Example.
  23. http_example() ->
  24. authenticator_example('password_based:http').
  25. built_in_database_example() ->
  26. authenticator_example('password_based:built_in_database').
  27. jwt_example() ->
  28. authenticator_example(jwt).
  29. delete_authenticators(Path, Chain) ->
  30. case emqx_authentication:list_authenticators(Chain) of
  31. {error, _} ->
  32. ok;
  33. {ok, Authenticators} ->
  34. lists:foreach(
  35. fun(#{id := ID}) ->
  36. emqx:update_config(
  37. Path,
  38. {delete_authenticator, Chain, ID},
  39. #{rawconf_with_defaults => true}
  40. )
  41. end,
  42. Authenticators
  43. )
  44. end.
  45. delete_config(ID) ->
  46. {ok, _} =
  47. emqx:update_config(
  48. [authentication],
  49. {delete_authenticator, ?GLOBAL, ID},
  50. #{rawconf_with_defaults => false}
  51. ).
  52. client_ssl_cert_opts() ->
  53. Dir = code:lib_dir(emqx_authn, test),
  54. #{
  55. <<"keyfile">> => filename:join([Dir, <<"data/certs">>, <<"client.key">>]),
  56. <<"certfile">> => filename:join([Dir, <<"data/certs">>, <<"client.crt">>]),
  57. <<"cacertfile">> => filename:join([Dir, <<"data/certs">>, <<"ca.crt">>])
  58. }.