0

Im trying to connect to the REST API of FreeNAS (http://api.freenas.org/authentication.html) within my AngularJS app. The API uses basic authentication with username and password.

In python this is a very easy thing as there is only one line of code:

requests.get('http://freenas.mydomain/api/v1.0/account/bsdusers/',auth=('root', 'freenas'))

I tried to find something for AngularJS but stumbled only over excrutiating code, e.g. How do I get basic auth working in angularjs?

Is there anything available like this:

$http({
  method: 'GET',
  url: 'http://freenas.mydomain/api/v1.0/account/bsdusers/',
  auth: ['username':'root', 'password':'pw']
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
Community
  • 1
  • 1
jazz
  • 349
  • 3
  • 13

2 Answers2

1

You need to create a function for encoding the user and password in Base64("username:password") and add Authorization header.

You can try encoding your username and password over here https://www.base64encode.org/ and see if it works. "root:freenas" being cm9vdDpmcmVlbmFz you can try the code below.

$http.defaults.headers.common['Authorization'] = 'Basic cm9vdDpmcmVlbmFz';

Once you get it working get implement the Base64 factory you posted ( How do I get basic auth working in angularjs? )

Hope it helps :)

Community
  • 1
  • 1
Konkko
  • 662
  • 4
  • 15
0

You can try like this.

$http.defaults.headers.common = {"Access-Control-Request-Headers": "accept, origin, authorization"}; 
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('root' + ':' + 'freenas');
$http({
  method: 'GET',
  url: 'http://freenas.mydomain/api/v1.0/account/bsdusers/'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
Thanigainathan
  • 1,505
  • 14
  • 25