emqx_auth_pgsql_app.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2021 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_auth_pgsql_app).
  17. -behaviour(application).
  18. -emqx_plugin(auth).
  19. -include("emqx_auth_pgsql.hrl").
  20. -import(emqx_auth_pgsql_cli, [parse_query/2]).
  21. %% Application callbacks
  22. -export([ start/2
  23. , stop/1
  24. ]).
  25. %%--------------------------------------------------------------------
  26. %% Application callbacks
  27. %%--------------------------------------------------------------------
  28. start(_StartType, _StartArgs) ->
  29. {ok, Sup} = emqx_auth_pgsql_sup:start_link(),
  30. if_enabled(auth_query, fun(AuthQuery) ->
  31. SuperQuery = parse_query(super_query, application:get_env(?APP, super_query, undefined)),
  32. {ok, HashType} = application:get_env(?APP, password_hash),
  33. AuthEnv = #{auth_query => AuthQuery,
  34. super_query => SuperQuery,
  35. hash_type => HashType,
  36. pool => ?APP},
  37. ok = emqx_auth_pgsql:register_metrics(),
  38. ok = emqx:hook('client.authenticate', fun emqx_auth_pgsql:check/3, [AuthEnv])
  39. end),
  40. if_enabled(acl_query, fun(AclQuery) ->
  41. ok = emqx:hook('client.check_acl', fun emqx_acl_pgsql:check_acl/5, [#{acl_query => AclQuery, pool => ?APP}])
  42. end),
  43. {ok, Sup}.
  44. stop(_State) ->
  45. ok = emqx:unhook('client.authenticate', fun emqx_auth_pgsql:check/3),
  46. ok = emqx:unhook('client.check_acl', fun emqx_acl_pgsql:check_acl/5).
  47. if_enabled(Par, Fun) ->
  48. case application:get_env(?APP, Par) of
  49. {ok, Query} -> Fun(parse_query(Par, Query));
  50. undefined -> ok
  51. end.