Captive portal - External RADIUS and site

Started by xaxero, February 21, 2025, 03:24:13 PM

Previous topic - Next topic
I am trying to get the captive portal to use an external commercial service.

I need to get the MAC address to send this to the external site. I have the custom template working and I have also checked the box to provide extended data (in the advanced section) I cannot see any documentation where I can get the data I need to concatenate with the redirect HTML call.

Any help or pointers would be appreciated - I have my Web call below and need to find the variables MAC and IP filled in:

     function redirect() {
            // Get the necessary parameters.  OPNsense *must* be configured to pass these.
            var mac = getParameterByName('mac');
            var ip = getParameterByName('ip');
            var sessionIp = getParameterByName('redirurl');  //Best guess, based on common practice

            //Check for ip in redirurl and extract it.
            const ipRegex = /\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b/;
            const match = sessionIp ? sessionIp.match(ipRegex) : null;

            if (match) {
                sessionIp = match[0];
            } else {
                sessionIp = ''; // Or some default value if extraction fails.
            }

            // Build the redirect URL.
            var redirectUrl = "https://wifihotspot.io/test/login?mac=" + encodeURIComponent(mac) +
                              "&ip=" + encodeURIComponent(ip) +
                              "&sessionip=" + encodeURIComponent(sessionIp);
            // Redirect.
             window.location.replace(redirectUrl);
        }

        // Redirect as soon as the page loads.
        window.onload = redirect;

The answer was in the documentation and a weekend of trial and error. API call is shown below


<script type="text/javascript" src="js/zone.js"></script>
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>

<script type="text/javascript">
  // Initialize macAddress (you might want to use a default or placeholder if needed)
  var mac = "";

  $.ajax({
    type: "POST",
                     url: "/api/captiveportal/access/status/" + zoneid + "/",
                     dataType:"json",
                     data:{ user: $("#inputUsername").val(), password: $("#inputPassword").val() }
   
  }).done(function(data) {
    // Correctly extract the MAC address (assuming data['macAddress'] contains the MAC)
    mac = data['macAddress'];

    // --- Construct the redirect URL AFTER getting the MAC ---
    var externalPortalURL = "https://external.site/test/login";
    var redirectURL = externalPortalURL + "?Zone=" + encodeURIComponent(zoneid) + "&mac=" + encodeURIComponent(mac);

    // Redirect the user
    window.location.href = redirectURL;
  });
</script>