ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.58k stars 1.33k forks source link

how to detach/unset cookies for superagent agent #1348

Open JadyNews opened 6 years ago

JadyNews commented 6 years ago

We use superagent to get SAML integration with AWS, when posting request to the SAML IdP (Okta) we have to use agent to include cookies in order to get the SAML metadata back, but the cookies needs to detach/unset when making request to AWS to assume role.

Is that a way to implement it?

Thank you in advanced, Jady

kornelski commented 6 years ago

By default all server-set cookies are thrown away.

Cookies are preserved only by a copy of an agent (.agent()) so create and use a copy of an agent for as long as you need its cookies.

JadyNews commented 6 years ago

@kornelski , I am new here, could you please confirm that if I use superagent.agent(). After .end() will the session be closed.

For example, user1 uses superagent.agent() to call an API to get response SAML back and do something, after it, user2 uses superagent.agent() to call same API. Will the user1's session still preserved when user2 making the call ?

The code snippet is below:

var request = require('superagent');
var agent = request.agent();
agent
    .get(`${config.oktaAppURL}`)
    .query({ sessionToken: sessionToken })
    .set('Accept',  '*/*')
    .end(function(err, res) {
      if (err) {
        logger.error('Call okta app url to get SAML assertion: ' +
        'can\'t not query to this app url');
        callback(err);
      }
      else {
        // do something
          return callback(null, data);
        }
        else {
          // do something
          return callback(err);
        }
      }
    });
kornelski commented 6 years ago

Each instance of an agent has separate cookies. If you make two agents, they will be like two users.