Skip to content

Digest authentication #686

Closed
Closed

Description

@chukitow

I was trying to implement some stuff of CloudApp API but I wasn't able to implement the Basic authentication, according to the documentation I just need to specify in the request config the auth parameters, like this

axios({
  url: "https://my.cl.ly/v3/items",
   auth: {
    username: "",
    password: ""
  },
})

This is an example using request where I'm able to make the request.

var options = {
  uri: 'https://my.cl.ly/v3/items',
  auth: {
    user: "",
    pass: "",
    sendImmediately: false
  }
};
request(options, function(error, response, body){
});

Activity

  1. rubennorte commented on Feb 12, 2017

    @rubennorte
    Member

    Is this being run in Node or in the browser? Have you inspected the request in the developer tools? What is being send? Have you seen any error in the console?

  2. nickuraltsev commented on Mar 2, 2017

    @nickuraltsev
    Contributor

    Closing due to inactivity. Please feel free to reopen.

  3. buccfer commented on Sep 13, 2017

    @buccfer

    @nickuraltsev With sendImmediately: false in request library you can perform a digest auth. Is there any way to achieve the same with axios?. Thanks.

  4. shtse8 commented on Nov 23, 2017

    @shtse8
  5. emilian commented on Mar 1, 2018

    @emilian

    Digest authentication would be great. +1 from me as well.

  6. chukitow commented on Mar 1, 2018

    @chukitow
    Author

    @rubennorte I was trying to run the script in node

  7. ashavk commented on Feb 23, 2019

    @ashavk

    Is there any conclusion/update on the same? Still can't find a way to perform digest auth using axios.

  8. tjunussov commented on Mar 13, 2019

    @tjunussov
    • need!
  9. ronkorving commented on May 9, 2019

    @ronkorving

    Need! :'(
    Please re-open, this is such a core feature of an HTTP library...

  10. rmnaderdev commented on May 17, 2019

    @rmnaderdev

    Need!

  11. falcaosurfline-zz commented on May 29, 2019

    @falcaosurfline-zz

    +1 . ... this would be something really nice to have handy

  12. alirezamirsepassi commented on Jun 22, 2019

    @alirezamirsepassi

    Please reopen!!

  13. rakeshta commented on Sep 16, 2019

    @rakeshta

    +1 vote for digest authentication.
    falling back to Basic auth for now (sigh)

  14. bernhardberger commented on Sep 28, 2019

    @bernhardberger

    Agreed.. +1

  15. ronkorving commented on Sep 30, 2019

    @ronkorving

    @chukitow @nickuraltsev I can't reopen, but someone who can, please do?

  16. fgeorges commented on Nov 28, 2019

    @fgeorges

    Digest authentication is a core feature of an HTTP client. In case this can help, I implemented it in the function requestAuth at https://github.com/fgeorges/mlproj/blob/master/src/node.js.

    It computes the right headers involved in the Digest algo as defined in the RFC.

    Feel free to reuse the code. I do not know the code base of Axios, nor the dev process, but if someone is ready to assist me just the minimum on that front, I'd be happy to provide a PR.

  17. chrisdchristo commented on Mar 2, 2020

    @chrisdchristo
  18. determin1st commented on Mar 3, 2020

    @determin1st

    @fgeorges can you provide more info, i may try to integrate your code into my lib

  19. CadsoftBrianW commented on Mar 19, 2020

    @CadsoftBrianW

    @chrisdchristo I have the same use case, not sure how to proceed.

  20. chrisdchristo commented on Mar 19, 2020

    @chrisdchristo

    @CadsoftBrianW see my updated comment above on how to do digest with MongoDB Atlas.

  21. vkarpov15 commented on Apr 8, 2020

    @vkarpov15

    I wrote a quick implementation of digest auth to make requests to MongoDB Atlas' API, hopefully this helps someone:

    const base = `https://cloud.mongodb.com`;
    let count = 0;
    
    module.exports = async function() {
      const uri = `/api/atlas/v1.0`;
    
      const data = await axios.get(`${base}${uri}`).
        catch(err => {
          if (err.response.status === 401) {
            const authDetails = err.response.headers['www-authenticate'].split(', ').map(v => v.split('='));
    
            ++count;
            const nonceCount = ('00000000' + count).slice(-8);
            const cnonce = crypto.randomBytes(24).toString('hex');
    
            const realm = authDetails[0][1].replace(/"/g, '');
            const nonce = authDetails[2][1].replace(/"/g, '');
    
            const md5 = str => crypto.createHash('md5').update(str).digest('hex');
    
            const HA1 = md5(`${publicKey}:${realm}:${privateKey}`);
            const HA2 = md5(`GET:${uri}`);
            const response = md5(`${HA1}:${nonce}:${nonceCount}:${cnonce}:auth:${HA2}`);
    
            const authorization = `Digest username="${publicKey}",realm="${realm}",` +
              `nonce="${nonce}",uri="${uri}",qop="auth",algorithm="MD5",` +
              `response="${response}",nc="${nonceCount}",cnonce="${cnonce}"`;
    
            return axios.get(`${base}${uri}`, { headers: { authorization } });
          }
          throw err;
        }).
        catch(err => {
          throw err;
        });
    
      return data;
    };

    Admittedly HTTP Digest Auth is a bit anachronistic, I hadn't seen anyone use it since the 90's until I started looking into how to figure out Atlas' API. But once you figure out how to calculate the digest you can just stick it in the auth header and you're good. I'll probably wrap this in an error interceptor in my own code.

  22. fgeorges commented on Apr 9, 2020

    @fgeorges

    @determin1st Sorry, I didn't see your message before. The function requestAuth in that code is quite self-contained. It makes a request, and as long as the response is 401 (max 3 times), it computes the Authorization header based on the WWW-Authenticate header.

    If you can drive me through the Axios code base, I would be happy to assist you on that part.

  23. locked and limited conversation to collaborators on May 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions