emqttd_auth_mod.erl 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
  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(emqttd_auth_mod).
  17. -author("Feng Lee <feng@emqtt.io>").
  18. -include("emqttd.hrl").
  19. -export([passwd_hash/2]).
  20. -type(hash_type() :: plain | md5 | sha | sha256 | pbkdf2 | bcrypt).
  21. %%--------------------------------------------------------------------
  22. %% Authentication behavihour
  23. %%--------------------------------------------------------------------
  24. -ifdef(use_specs).
  25. -callback(init(AuthOpts :: list()) -> {ok, State :: any()}).
  26. -callback(check(Client :: mqtt_client(),
  27. Password :: binary(),
  28. State :: any())
  29. -> ok | | {ok, boolean()} | ignore | {error, string()}).
  30. -callback(description() -> string()).
  31. -else.
  32. -export([behaviour_info/1]).
  33. behaviour_info(callbacks) ->
  34. [{init, 1}, {check, 3}, {description, 0}];
  35. behaviour_info(_Other) ->
  36. undefined.
  37. -endif.
  38. %% @doc Password Hash
  39. -spec(passwd_hash(hash_type(), binary() | tuple()) -> binary()).
  40. passwd_hash(plain, Password) ->
  41. Password;
  42. passwd_hash(md5, Password) ->
  43. hexstring(crypto:hash(md5, Password));
  44. passwd_hash(sha, Password) ->
  45. hexstring(crypto:hash(sha, Password));
  46. passwd_hash(sha256, Password) ->
  47. hexstring(crypto:hash(sha256, Password));
  48. passwd_hash(pbkdf2, {Salt, Password, Macfun, Iterations, Dklen}) ->
  49. case pbkdf2:pbkdf2(Macfun, Password, Salt, Iterations, Dklen) of
  50. {ok, Hexstring} -> pbkdf2:to_hex(Hexstring);
  51. {error, Error} -> lager:error("PasswdHash with pbkdf2 error:~p", [Error]), <<>>
  52. end;
  53. passwd_hash(bcrypt, {Salt, Password}) ->
  54. case bcrypt:hashpw(Password, Salt) of
  55. {ok, HashPassword} -> list_to_binary(HashPassword);
  56. {error, Error}-> lager:error("PasswdHash with bcrypt error:~p", [Error]), <<>>
  57. end.
  58. hexstring(<<X:128/big-unsigned-integer>>) ->
  59. iolist_to_binary(io_lib:format("~32.16.0b", [X]));
  60. hexstring(<<X:160/big-unsigned-integer>>) ->
  61. iolist_to_binary(io_lib:format("~40.16.0b", [X]));
  62. hexstring(<<X:256/big-unsigned-integer>>) ->
  63. iolist_to_binary(io_lib:format("~64.16.0b", [X])).