travisghansen / hass-opnsense

OPNsense integration with Home Assistant
Apache License 2.0
229 stars 29 forks source link

[FR]: Create Voucher in CaptivePortal #89

Closed Riza-Aslan closed 1 month ago

Riza-Aslan commented 1 year ago

It would be awesome to have a button in HASS to create a new voucher in the captiveportal. I had a working solution, where I could set a name and a password for the voucher. But I had to change some files inside of opnsense in order to get it work like this. And I had to repeat it with every opnsense update. The best solution would be, to have a possibility to create new voucher-groups (for example a vouchergroup for every guest) and then vouchers inside of these groups. And opnsense should tell HASS the username and password for the new created voucher, so that it can be viewed in lovelace.

Do you think this would be possible?

Riza-Aslan commented 2 months ago

This was the solution which I used before: https://administrator.de/knowledge/opnsense-captive-portal-mit-vordefinierten-voucher-497235.html

I configured the REST-Integration like this in Home Assistant:

rest_command:
  opnsense_voucher:
    url: "https://*opnsense-ip:port*/api/captiveportal/voucher/generateVouchers/Voucher/"
    method: POST
    headers:
      Authorization: !secret opnsense_token
      User-Agent: Home Assistant
    content_type: "application/json; charset=utf-8"
    payload: '{
      "count": "1",
      "validity": "525600",
      "expirytime": "0",
      "vouchergroup": "HomeAssistant",
      "usernamedef": "{{ username }}",
      "passworddef": "{{ password }}"
      }'
    verify_ssl: false

Created two helpers in HASS and used this Script to send the API Call:

data:
  username: "{{ states('input_text.opnsense_voucher_name') }}"
  password: "{{ states('input_text.opnsense_voucher_password') }}"
action: rest_command.opnsense_voucher

I have a Card on my Dashboard to enter the username and password, and when I click on Submit, it fires the script.

I had to check the modified files after every update, and got tired of it so I stopped doing it. But maybe it could help you with my FR, to find a better solution.

Snuffy2 commented 1 month ago

What files did you have to change in opnsense to get it to work?

Riza-Aslan commented 1 month ago

/usr/local/opnsense/mvc/app/library/OPNsense/Auth/voucher.php /usr/local/opnsense/mvc/app/controllers/OPNsense/CaptivePortal/Api/VoucherController.php

You should find everything here: https://administrator.de/knowledge/opnsense-captive-portal-mit-vordefinierten-voucher-497235.html

Its in german, but the translation should be good enough to understand what he did.

Snuffy2 commented 1 month ago

I believe it can be done using the REST API if you are ok with randomly generated name/passwords.

https://docs.opnsense.org/development/api/core/captiveportal.html

This could be setup as an HA Action (fka. Service) that could be called and the vouchers returned from the Action as response data. Do you think that would work?

Post:
/api/captiveportal/voucher/generateVouchers/Captive%20Portal%20Voucher%20Server/

Body:
{
    "count": "1",
    "expirytime": "43200",
    "validity": "14400",
    "vouchergroup": "20241007201449"
}

Response:
[
    {
        "username": "ve8i88zJ",
        "password": "qJaTsS7BHx",
        "vouchergroup": "20241007201449",
        "validity": 14400,
        "expirytime": 1728389702,
        "starttime": null
    }
]