This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add verify API support (#198) * Remove unreachable code
- Loading branch information
Showing
5 changed files
with
250 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package goinsta | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
type ChallengeStepData struct { | ||
Choice string `json:"choice"` | ||
FbAccessToken string `json:"fb_access_token"` | ||
BigBlueToken string `json:"big_blue_token"` | ||
GoogleOauthToken string `json:"google_oauth_token"` | ||
Email string `json:"email"` | ||
SecurityCode string `json:"security_code"` | ||
ResendDelay interface{} `json:"resend_delay"` | ||
ContactPoint string `json:"contact_point"` | ||
FormType string `json:"form_type"` | ||
} | ||
type Challenge struct { | ||
insta *Instagram | ||
StepName string `json:"step_name"` | ||
StepData ChallengeStepData `json:"step_data"` | ||
LoggedInUser *Account `json:"logged_in_user,omitempty"` | ||
UserID int64 `json:"user_id"` | ||
NonceCode string `json:"nonce_code"` | ||
Action string `json:"action"` | ||
Status string `json:"status"` | ||
} | ||
|
||
type challengeResp struct { | ||
*Challenge | ||
} | ||
|
||
func newChallenge(insta *Instagram) *Challenge { | ||
time := &Challenge{ | ||
insta: insta, | ||
} | ||
return time | ||
} | ||
|
||
// updateState updates current data from challenge url | ||
func (challenge *Challenge) updateState() error { | ||
insta := challenge.insta | ||
|
||
data, err := insta.prepareData(map[string]interface{}{ | ||
"guid": insta.uuid, | ||
"device_id": insta.dID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, err := insta.sendRequest( | ||
&reqOptions{ | ||
Endpoint: challenge.insta.challengeURL, | ||
Query: generateSignature(data), | ||
}, | ||
) | ||
if err == nil { | ||
resp := challengeResp{} | ||
err = json.Unmarshal(body, &resp) | ||
if err == nil { | ||
*challenge = *resp.Challenge | ||
challenge.insta = insta | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// selectVerifyMethod selects a way and verify it (Phone number = 0, email = 1) | ||
func (challenge *Challenge) selectVerifyMethod(choice string, isReplay ...bool) error { | ||
insta := challenge.insta | ||
|
||
url := challenge.insta.challengeURL | ||
if len(isReplay) > 0 && isReplay[0] { | ||
url = strings.Replace(url, "/challenge/", "/challenge/replay/", -1) | ||
} | ||
|
||
data, err := insta.prepareData(map[string]interface{}{ | ||
"choice": choice, | ||
"guid": insta.uuid, | ||
"device_id": insta.dID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, err := insta.sendRequest( | ||
&reqOptions{ | ||
Endpoint: url, | ||
Query: generateSignature(data), | ||
IsPost: true, | ||
}, | ||
) | ||
if err == nil { | ||
resp := challengeResp{} | ||
err = json.Unmarshal(body, &resp) | ||
if err == nil { | ||
*challenge = *resp.Challenge | ||
challenge.insta = insta | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// sendSecurityCode sends the code received in the message | ||
func (challenge *Challenge) SendSecurityCode(code string) error { | ||
insta := challenge.insta | ||
url := challenge.insta.challengeURL | ||
|
||
data, err := insta.prepareData(map[string]interface{}{ | ||
"security_code": code, | ||
"guid": insta.uuid, | ||
"device_id": insta.dID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, err := insta.sendRequest( | ||
&reqOptions{ | ||
Endpoint: url, | ||
Query: generateSignature(data), | ||
IsPost: true, | ||
}, | ||
) | ||
if err == nil { | ||
resp := challengeResp{} | ||
err = json.Unmarshal(body, &resp) | ||
if err == nil { | ||
*challenge = *resp.Challenge | ||
challenge.insta = insta | ||
} | ||
} | ||
return err | ||
} | ||
|
||
// deltaLoginReview process with choice (It was me = 0, It wasn't me = 1) | ||
func (challenge *Challenge) deltaLoginReview() error { | ||
return challenge.selectVerifyMethod("0") | ||
} | ||
|
||
func (challenge *Challenge) Process(apiURL string) error { | ||
challenge.insta.challengeURL = apiURL[1:] | ||
|
||
if err := challenge.updateState(); err != nil { | ||
return err | ||
} | ||
|
||
switch challenge.StepName { | ||
case "select_verify_method": | ||
return challenge.selectVerifyMethod(challenge.StepData.Choice) | ||
case "delta_login_review": | ||
return challenge.deltaLoginReview() | ||
} | ||
|
||
return ErrChallengeProcess{StepName: challenge.StepName} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/ahmdrz/goinsta" | ||
"github.com/tcnksm/go-input" | ||
) | ||
|
||
func main() { | ||
insta := goinsta.New( | ||
os.Getenv("INSTAGRAM_USERNAME"), | ||
os.Getenv("INSTAGRAM_PASSWORD"), | ||
) | ||
if err := insta.Login(); err != nil { | ||
switch v := err.(type) { | ||
case goinsta.ChallengeError: | ||
err := insta.Challenge.Process(v.Challenge.APIPath) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
ui := &input.UI{ | ||
Writer: os.Stdout, | ||
Reader: os.Stdin, | ||
} | ||
|
||
query := "What is SMS code for instagram?" | ||
code, err := ui.Ask(query, &input.Options{ | ||
Default: "000000", | ||
Required: true, | ||
Loop: true, | ||
}) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
err = insta.Challenge.SendSecurityCode(code) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
log.Fatalln(err) | ||
} | ||
defer insta.Logout() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters