13

I need to get the username and the password that a browser has send to my node.js application from the url.

I digged through various documentations and objects but I can't find anything useful. Does anybody know how to do that? Using Authentication header is not an option because modern bowsers don't set them.

https://username:[email protected]/
        =================
//         /\
//         ||
// I need this part

Thanks for your help!

2
  • you are sending these details in query string? right Commented Sep 13, 2018 at 6:53
  • 1
    @SyedKashanAli No, It`s not part of query string. It's placed before url as you can see in the sample above Commented Sep 13, 2018 at 8:45

6 Answers 6

7
+25

This method of authentication is called "Basic Auth". You can access username and password via basic-auth npm package using bellow code:

const express = require('express');
const basicAuth = require('basic-auth');
let app = express();
app.get('/', function (req, res) {
    let user = basicAuth(req);
    console.log(user.name); //prints username
    console.log(user.pass); //prints password
});
app.listen(3000, function () {});

Now if you send a request to http://username:password@localhost:3000/ this code will print your username and password in the console.

Be aware that this method of authentication is not supported in most of the browsers anymore.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for helping me, Its used in git, i wrote a git helper and i send it to git command, you can see simple-git package that the way of authentication in git repository. npmjs.com/package/simple-git#authentication
Thanks for elaboration. I edited the post and answered your question. @MehdiYeganeh
basic-auth or express-basic-auth working with request headers (like: www-authentication, proxy-authorization, ... ) and this user & pass stored in url, i`ll checked your code to show you request details, and you can see the result in this image, there isn't any header about auth, user & pass is in the url: Screen Shot
I told you that you can "fetch" username and password that are sent by basic authentication method via this code. Your problem now is "sending" these data and is not related to this question. Because you can test the above code by using curl and see that it fetches username and password: curl -XGET http://user:pass@localhost:3000/ As I told you before, browsers no longer send basic authentication data and if you are sending requests to your server with your browser, you can't access them in your server.
@SeyedAliAkhavani question definitely says that "I need to get the username and the password that a browser has send to my node.js", so your answer is not valid. It is just impossible
|
1

The username:password is contained in the Authorization header as a base64-encoded string:

http.createServer(function(req, res) {
  var header = req.headers['authorization'] || '',        // get the header
      token = header.split(/\s+/).pop()||'',            // and the encoded auth token
      auth = new Buffer(token, 'base64').toString(),    // convert from base64
      parts=auth.split(/:/),                          // split on colon
      username=parts[0],
      password=parts[1];

  res.writeHead(200,{'Content-Type':'text/plain'});
  res.end('username is "'+username+'" and password is "'+password+'"');

}).listen(1337,'127.0.0.1');

see this post: Basic HTTP authentication in Node.JS?

2 Comments

Thank you for your answer but unfortunatly this doesn't work for me. It works if I use curl but not with Chrome (v37) or Firefox (v32). The browsers don't convert the Information to an authorization header.
Doesn´t work for me either, tested in firefox.
1

This is exactly what you're looking for:

http://nodejs.org/api/url.html

If you want to know where to get the URL itself from, it is passed in the request object, also known as "path":

Node.js: get path from the request

3 Comments

Thank you for your answer. But I need to know where to get the full url to pass it to the url.format() function.
Then you aren't looking how to get the username/password, you're looking for the basic URL? Just get it out of the request object: req.url
req.url only contains the path after the fqdn (at least when I test on localhost).
1

The http://username:[email protected] format is no longer supported by either IE or Chrome, wouldn't be surprised if others followed suit if they haven't already.

(From T.J. Crowder comment here)

1 Comment

This answer is incorrect, at least with regards to Chrome
0
/createserver/:username/:password

let params={
userName:req.params.username,
password:req.params.password
}
console.log(params);

Is this is what you wanted .?

3 Comments

No, i want catch it from url, not query string
have you checked this thread? stackoverflow.com/questions/10183291/…
Thanks for helping, Yes, I checked it before, there is two things, the first i didn't use express and i want it from pure node, and the second express like node didn't give me back the username & password, we just could make full url with joining host and path and we haven`t username & password in url
-2

the URL is accessible by the server in the request object :

http.createServer(function(req, res) {
    var url = req.url
    console.log(url) //echoes https://username:[email protected]/
    //do something with url
}).listen(8081,'127.0.0.1');

1 Comment

This only displays "/" in the console, it does not contain fqdn, username or password.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.