Mnesia Authentication

Mnesia authentication uses the built-in Mnesia database of EMQ X to store client Client ID/Username and password, and supports management of authentication data through HTTP API.

Mnesia authentication does not depend on external data sources, and it is simple and lightweight to use.

Plugin:

  1. emqx_auth_mnesia

Authentication rules

Hash method

Mnesia authentication uses sha256 for password hash encryption by default, which can be changed in etc/plugins/emqx_auth_mnesia.conf:

  1. # etc/plugins/emqx_auth_mnesia.conf
  2. ## Value: plain | md5 | sha | sha256
  3. auth.mnesia.password_hash = sha256

After configuring [Hash Method](./auth.md#Password salting rules and hash methods), the newly added preset authentication data and authentication data added through the HTTP API will be stored in the EMQ X built-in database in the format of hash ciphertext.

Preset authentication data

You can preset authentication data through the configuration file and edit the configuration file: etc/plugins/emqx_auth_mnesia.conf

  1. # etc/plugins/emqx_auth_mnesia.conf
  2. ## The first group of authentication data
  3. auth.mnesia.1.login = admin
  4. auth.mnesia.1.password = public
  5. auth.mnesia.1.is_superuser = true
  6. ## The second group of authentication data
  7. auth.mnesia.2.login = client
  8. auth.mnesia.2.password = public
  9. auth.mnesia.2.is_superuser = false

When the plugin starts, it will read the preset authentication data and load it into the EMQ X built-in database, and the authentication data on the node will be synchronized to the cluster at this stage.

WARNING

The preset authentication data uses a clear text password in the configuration file. For security and maintainability, this function should be avoided.

The preset authentication data cannot be modified or deleted through the API, please use it with caution.

Use the HTTP API to manage authentication data

Add authentication data

API definition:

  1. # Request
  2. POST api/v4/auth_user
  3. {
  4. "login": "emqx_c",
  5. "password": "emqx_p",
  6. "is_superuser": false
  7. }
  8. # Response
  9. {
  10. "data": {
  11. "emqx_c": "ok"
  12. },
  13. "code": 0
  14. }

Use POST request to add login as emqx_c, password as emqx_p, which is an non-super user authentication information, and “code = 0` in the returned message means success.

Add authentication data in batch

API definition:

  1. # Request
  2. POST api/v4/auth_user
  3. [
  4. {
  5. "login": "emqx_c_1",
  6. "password": "emqx_p",
  7. "is_superuser": false
  8. },
  9. {
  10. "login": "emqx_c_2",
  11. "password": "emqx_p",
  12. "is_superuser": false
  13. }
  14. ]
  15. # Response
  16. {
  17. "data": {
  18. "emqx_c_2": "ok",
  19. "emqx_c_1": "ok"
  20. },
  21. "code": 0
  22. }

Check the added authentication data

API definition:

  1. # Request
  2. GET api/v4/auth_user
  3. # Response
  4. {
  5. "meta": {
  6. "page": 1,
  7. "limit": 10,
  8. "count": 1
  9. },
  10. "data": [
  11. {
  12. "password": "ceb5e917f7930ae8f0dc3ceb496a428f7e644736eebca36a2b8f6bbac756171a",
  13. "login": "emqx_c",
  14. "is_superuser": false
  15. }
  16. ],
  17. "code": 0
  18. }

Change the added authentication data

API definition:

  1. # Request
  2. PUT api/v4/auth_user/${login}
  3. {
  4. "password": "emqx_new_p",
  5. "is_superuser": false
  6. }
  7. # Response
  8. {
  9. "code": 0
  10. }

Check the specified authentication data

Note that the password returned here is the password encrypted using the hash method specified in the configuration file:

API definition:

  1. # Request
  2. GET api/v4/auth_user/${login}
  3. # Response
  4. {
  5. "data": {
  6. "password": "3b20ff0218af39d01252844ccaac8ce0160f969ad00c601e23f6e57cd26fad4e",
  7. "login": "emqx_c",
  8. "is_superuser": false
  9. },
  10. "code": 0
  11. }

Delete the authentication data

Delete the specified authentication data:

API definition:

  1. # Request
  2. DELETE api/v4/auth_user/${login}
  3. # Response
  4. {
  5. "code": 0
  6. }