views:

72

answers:

2

I have a webpage that is only accessible over http basic auth. How can I figure out the basic auth username in javascript in that page. i.e. when someone visits it (after logging in), I want to make a popup that says "Hello, you're currently signed in as the user $USERNAME"

+3  A: 

It's not possible purely from JavaScript. You'd have to have the server side script pick up the username from the request, and insert it in the generated page code for the script to pick up. (eg. <script type="text/javascript">var username=(insert JSON-encoded string here);</script>.

bobince
A: 

javascript:window.alert(document.location) seems to give a URL with a username (but no password). This may not be true in other browsers.

tc.
The username is all I need, not the password. However I'm pretty sure most of my users won't be typing "https://[email protected]" in their address bar, they will just enter it into their browsers popup box. Will this approach still work in those circumstances?
Rory
Try it and see. I don't think LiveScript/JavaScript/ECMAScript guarantees much, and many browsers don't implement all of the relevant standards, so there's no easy way to tell whether this is "safe" without testing on all the browsers you intend on supporting.
tc.