dilame / instagram-private-api

NodeJS Instagram private API SDK. Written in TypeScript.
MIT License
5.93k stars 1.14k forks source link

Checkpoints #159

Closed generictitle closed 7 years ago

generictitle commented 7 years ago

Hey I've been using the checkpoint functions and have a view thoughts

First, the requirement that a 'number' type is supplied for email code checkpoints causes leading zeros to be lost, and it seems that Instagram will therefore reject 1 in 10 checkpoints that start with a 0. I suggest using a string type so that zeros aren't lost, or does this cause other problems?

Second, there's a type of checkpoint that isn't covered - the "Was this you?" button click checkpoint. I've written the code for it but don't have time to create a pull request and strip out my other code. Here it is if you'd like to include it :) I'll try and get a pull request created in the next week or two if this isn't much help.

var ButtonVerificationChallenge = function(session, type, checkpointError, body) {
    Challenge.apply(this, arguments);
    this.phoneInserted = false;
}
util.inherits(ButtonVerificationChallenge, Challenge);
exports.ButtonVerificationChallenge = ButtonVerificationChallenge;

ButtonVerificationChallenge.prototype.click = function() {
  var that = this;
  return new WebRequest(that.session)
      .setMethod('POST')
      .setUrl(that.error.url)
      .setHeaders({
          'Host': CONSTANTS.HOSTNAME,
          'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
          'Accept-Language': 'en-us',
          'Content-Type': 'application/x-www-form-urlencoded',
          'Origin': ORIGIN,
          'Connection': 'keep-alive',
          'User-Agent': iPhoneUserAgent({version: that.session.device.version}),
          'Referer': that.error.url
      })
      .setBodyType('form')
      .removeHeader('x-csrftoken') // we actually sending this as post param
      .setData({
          approve: "It Was Me",
          csrfmiddlewaretoken: that.session.CSRFToken
      })
      .send({followRedirect: false})
      .then(function(response) {
          if(response.statusCode == 200 && response.body.indexOf('Your account has been verified') !== -1) {
            return that;
          }
          // Must be redirected
          throw new Exceptions.NotPossibleToResolveChallenge(
              "Probably incorrect code",
              Exceptions.NotPossibleToResolveChallenge.CODE.INCORRECT_CODE
          );
      })
      .catch(errors.StatusCodeError, function(error) {
          if(error.statusCode == 302)
              return true;
          if(error.statusCode == 400)
              throw new Exceptions.NotPossibleToResolveChallenge(
                  "Verification has not been accepted",
                  Exceptions.NotPossibleToResolveChallenge.CODE.NOT_ACCEPTED
              );
          throw error;
      })
}

ButtonVerificationChallenge.prototype.confirmate = function(code) {
    var that = this;
    return new WebRequest(this.session)
        .setMethod('POST')
        .setUrl(that.error.url)
        .setHeaders({
            'Host': CONSTANTS.HOSTNAME,
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-us',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Origin': ORIGIN,
            'Connection': 'keep-alive',
            'User-Agent': iPhoneUserAgent({version: that.session.device.version}),
            'Referer': that.error.url
        })
        .setBodyType('form')
        .removeHeader('x-csrftoken') // we actually sending this as post param
        .setData({
            csrfmiddlewaretoken: that.session.CSRFToken,
            OK: 'OK'
        })
        .send({followRedirect: false, qs: {next: 'instagram://checkpoint/dismiss'}})
        .then(function(response) {

            if(response.statusCode == 200 && response.body.indexOf('instagram://checkpoint/dismiss') !== -1)
                return true;
            throw new Exceptions.NotPossibleToResolveChallenge();
        })
        .catch(errors.StatusCodeError, function(error) {
            if(error.statusCode == 302)
                return true;
            throw error;
        })
};

and in the Challenge.resolve bit add this:

  if(response.body.indexOf('It Was Me') !== -1)
                return new ButtonVerificationChallenge(session, 'button', checkpointError, response.body);
generictitle commented 7 years ago

Usage:

Client.Web.Challenge.resolve(error)
  .then(function(challenge) {
      // challenge instanceof Client.Web.Challenge

      if(challenge.type == 'email') {
        return challenge.email();
      } else if(challenge.type == 'button') {
        return challenge.click();
      }
  }).then(function(challenge) {
    if(challenge.type == 'email') {
      return challenge.code(dataObj.code);
    } else if(challenge.type == 'button') {
      return challenge;
    }
  })
  .then(function(challenge) {
    // Yey Instagram accepted the code
    // now we confirmed that Instagram is happy, weird :P
    return challenge.confirmate()
  })
mavieth commented 7 years ago

Any updates on this?

IvanMMM commented 7 years ago

Checkpoint resolve system was overwritten some time ago. Tell me if there are problems left with it.