Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Digest Authentication

I'm trying to connect to a RESTFul API on a home automation server in my house (so massive security not required). When I try to connect with no credentials, I get:

[Error] XMLHttpRequest cannot load http://localhost:8176/devices/. Origin http://localhost is not allowed by Access-Control-Allow-Origin. 

The documentation says:

Indigo uses "digest authentificaction" (not basic nor token)

curl -X PUT --digest -u {username}:{password} -d value={value} http://{IPAddress}:8176/variables/{VariableToChange}

curl -X PUT --digest -u admin:p@ssw0rd -d value=true http://{IPAddress}:8176/variables/Var1

To get a list of devices, I can use one of these 3:

http://127.0.0.1:8176/devices/
http://127.0.0.1:8176/devices.xml/
http://127.0.0.1:8176/devices.txt/

I'm at a total loss on how to pass the creeds. Here's my code so far:

 function DeviceController($scope, $http) {
    $http.get("http://localhost:8176/devices/")
        .then(function (results) {
            //Success;
            console.log("Succss: " + results.status);
            $scope.devices = results.data;
        }, function (results) {
            //error
            console.log("Error: " + results.data + "; "
                                  + results.status);
            $scope.error = results.data;
        })
};

Can anyone point me in right direction?

Thanks

Mark

like image 301
mark1234 Avatar asked Sep 26 '26 10:09

mark1234


1 Answers

Your server has to be configured to accept CORS (Cross-origin resource sharing).

EDIT: It looks like your Indigo (don't know what it is) server is not the one serving your angular app. Please refer to your server's documentation on how to enable CORS.

As far as passing your credentials during your request, do:

$http({
    method: "GET",
    url: "http://localhost:8176/devices/",
    headers: {"Authorization": "Basic " + btoa("admin:p@ssw0rd")},
})...
like image 147
gtramontina Avatar answered Sep 28 '26 00:09

gtramontina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!