jeudi 25 juin 2020

How do you mock/mimic a cookie that is set server side?

First post here on SO. I was scraping a website just fine when they made some change. It's my bank website. The 'program' I made just reads the history of my transactions and write it in a file, it runs once a week or so and let me track my accounting. I don't use the API because it lacks some of the informations that I need regarding the type of expenses.

Recently, they start using ember (well I think from the content of the request) to generate a cookie.

I manage to access the sign-in page using requests, but the next request needs a specific cookie deviceId that comes from nowhere, I mean it is not returned by any of the previous requests.

How do you retrieve a cookie that is set server-side when scraping?

Do I have to execute the JS or do a special request ? Do I have to reverse-engineer the cookie writing fron the snippet below ?

In one of the .js file i found this bit of code where the cookie is generated I think :

 define("website/services/device-manager", [
    "exports",
    "ua-parser-js",
    "uuid",
    "website/config/environment",
    "webiste/utils/shared-cookie-domain",
  ], function (e, t, n, a, r) {
    Object.defineProperty(e, "__esModule", { value: !0 }), (e.default = void 0);
    var i = Ember.Service.extend({
      cookies: Ember.inject.service(),
      init() {
        this._super(...arguments);
        var e = this.cookies.read("deviceId"),
          i = new t.default().getDevice().type;
        if (!e) {
          e = (0, n.v4)();
          var o = {
            secure: "production" === a.default.environment,
            domain: r.default,
            maxAge: 31622400,
          };
          this.cookies.write("deviceId", e, o);
        }
        this.set("id", e), this.set("currentDevice", i);
      },
      isMobile: Ember.computed.equal("currentDevice", "mobile"),
    });
    e.default = i;
  }),

Regards, Martin




Aucun commentaire:

Enregistrer un commentaire