Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Authorization from HTTP-Request header

I already searched within SO for some threads about this, but could only find some which explained what this header is for or how to get the authorization header in c# but I don't want to read it from server side but from client side.

Is there any way to get the Base64 encoded header "Authorization" from the browser? I want to implement a tool where you can log in and if you click on a spezific button your username will be saved.

My problem is that the browser does the authorization automatically, and with jQuery and JavaScript methods you can only set the requestheaders and get the responseheaders. I couldn't find a method to get the requestheaders.

The library gethttp could get some headers, but not the authorization header. My guess is that this header is hidden.

I'm doing a login via SVN and the browser does the authorization the moment you enter the website.

Only the username is enough. I'm searching for solutions where the user doesn't have to input their username.

like image 792
manti Avatar asked Jul 28 '14 08:07

manti


2 Answers

I'm assuming you're trying to use the Basic Realm authorisation mechanism This had already been replied on Stackoverflow and involves the $.ajax() jquery object.
How to use Basic Auth with jQuery and AJAX?
So please don't upvote me on this

$.ajaxSetup({
  headers: {
    'Authorization': "Basic XXXXX"
  },
  data: '{ "comment" }',
  success: function (){
    alert('Thanks for your comment!'); 
  }
});

where XXXXX is your username:password base64 encoded

like image 94
John Doeff Avatar answered Sep 19 '22 21:09

John Doeff


You can use native fetch API:

fetch("http://localhost:8888/validate",{
  method:"GET",
  headers: {"Authorization": "Bearer xxxxx"}
})
.then(res => res.json())
.then(
  (result) => {
    // do something
  },
  // Note: it's important to handle errors here
  // instead of a catch() block so that we don't swallow
  // exceptions from actual bugs in components.
  (error) => {
    // handle error
  }
)
like image 42
Barlas Apaydin Avatar answered Sep 20 '22 21:09

Barlas Apaydin