-
Notifications
You must be signed in to change notification settings - Fork 557
/
googleapps.go
812 lines (644 loc) · 24.1 KB
/
googleapps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
package googleapps
import (
"bufio"
"bytes"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/versent/saml2aws/v2/pkg/cfg"
"github.com/versent/saml2aws/v2/pkg/creds"
"github.com/versent/saml2aws/v2/pkg/prompter"
"github.com/versent/saml2aws/v2/pkg/provider"
)
var logger = logrus.WithField("provider", "googleapps")
// Client wrapper around Google Apps.
type Client struct {
provider.ValidateBase
client *provider.HTTPClient
}
// New create a new Google Apps Client
func New(idpAccount *cfg.IDPAccount) (*Client, error) {
tr := provider.NewDefaultTransport(idpAccount.SkipVerify)
client, err := provider.NewHTTPClient(tr, provider.BuildHttpClientOpts(idpAccount))
if err != nil {
return nil, errors.Wrap(err, "error building http client")
}
return &Client{
client: client,
}, nil
}
// Authenticate logs into Google Apps and returns a SAML response
func (kc *Client) Authenticate(loginDetails *creds.LoginDetails) (string, error) {
// Get the first page
authURL, authForm, err := kc.loadFirstPage(loginDetails)
if err != nil {
return "", errors.Wrap(err, "error loading first page")
}
// Google supports only JavaScript-enabled clients
authForm.Set("bgresponse", "js_enabled")
authForm.Set("Email", loginDetails.Username)
// Post email address w/o password, then Get the password-input page
passwordURL, passwordForm, err := kc.loadLoginPage(authURL+"?hl=en&loc=US", loginDetails.URL+"&hl=en&loc=US", authForm)
if err != nil {
//if failed, try with "identifier"
authForm.Set("Email", "") // Clear previous key
authForm.Set("identifier", loginDetails.Username)
passwordURL, passwordForm, err = kc.loadLoginPage(authURL+"?hl=en&loc=US", loginDetails.URL+"&hl=en&loc=US", authForm)
if err != nil {
return "", errors.Wrap(err, "error loading login page")
}
}
logger.Debugf("loginURL: %s", passwordURL)
passwordForm.Set("Passwd", loginDetails.Password)
passwordForm.Set("TrustDevice", "on")
referingURL := passwordURL
responseDoc, err := kc.loadChallengePage(passwordURL+"?hl=en&loc=US", referingURL, passwordForm, loginDetails)
if err != nil {
return "", errors.Wrap(err, "error loading challenge page")
}
captchaInputIds := []string{
"logincaptcha",
"identifier-captcha-input",
"captchaimg",
}
var captchaFound *goquery.Selection
var captchaInputId string
for _, v := range captchaInputIds {
captchaFound = responseDoc.Find(fmt.Sprintf("#%s", v))
if captchaFound != nil && captchaFound.Length() > 0 {
captchaInputId = v
break
}
}
for captchaFound != nil && captchaFound.Length() > 0 {
captchaImgDiv := responseDoc.Find(".captcha-img")
if captchaImgDiv != nil {
captchaImgDiv = responseDoc.Find("div[data-auto-init='CaptchaInput']")
captchaInputId = "ca"
}
captchaPictureSrc, found := goquery.NewDocumentFromNode(captchaImgDiv.Children().Nodes[0]).Attr("src")
if !found {
return "", errors.New("captcha image not found but requested")
}
captchaPictureURL, err := generateFullURLIfRelative(captchaPictureSrc, passwordURL)
if err != nil {
return "", errors.Wrap(err, "error generating captcha image URL")
}
captcha, err := kc.tryDisplayCaptcha(captchaPictureURL)
if err != nil {
return "", err
}
captchaForm, captchaURL, err := extractInputsByFormID(responseDoc, "gaia_loginform", "challenge")
if err != nil {
return "", errors.Wrap(err, "error extracting captcha")
}
logger.Debugf("captchaURL: %s", captchaURL)
_, captchaV1 := captchaForm["Passwd"]
if captchaV1 {
captchaForm.Set("Passwd", loginDetails.Password)
}
captchaForm.Set(captchaInputId, captcha)
responseDoc, err = kc.loadChallengePage(captchaURL+"?hl=en&loc=US", captchaURL, captchaForm, loginDetails)
if err != nil {
return "", errors.Wrap(err, "error loading challenge page")
}
captchaFound = responseDoc.Find(fmt.Sprintf("#%s", captchaInputId))
}
// New Captcha proceeds back to password page
passworddBeingRequested := responseDoc.Find("#password")
if passworddBeingRequested != nil && passworddBeingRequested.Length() > 0 {
loginForm, loginURL, err := extractInputsByFormID(responseDoc, "challenge")
if err != nil {
return "", errors.Wrap(err, "error parsing password page after captcha")
}
loginForm.Set("Passwd", loginDetails.Password)
responseDoc, err = kc.loadChallengePage(loginURL+"?hl=en&loc=US", loginURL, loginForm, loginDetails)
if err != nil {
return "", errors.Wrap(err, "error loading challenge page")
}
}
samlAssertion := mustFindInputByName(responseDoc, "SAMLResponse")
if samlAssertion == "" {
if responseDoc.Selection.Find("#passwordError").Text() != "" {
return "", errors.New("Password error")
}
if err := isMissing2StepSetup(responseDoc); err != nil {
return "", err
}
return "", errors.New("page is missing saml assertion")
}
return samlAssertion, nil
}
func isMissing2StepSetup(responseDoc *goquery.Document) error {
if responseDoc.Selection.Find("section.aN1Vld ").Text() != "" {
return errors.New("Because of your organization settings, you must set-up 2-Step Verification in your account")
}
return nil
}
func (kc *Client) tryDisplayCaptcha(captchaPictureURL string) (string, error) {
// TODO: check for user flag for easy captcha presentation
if os.Getenv("TERM_PROGRAM") == "iTerm.app" {
// Use iTerm to show the image if available
return kc.iTermCaptchaPrompt(captchaPictureURL)
} else {
return simpleCaptchaPrompt(captchaPictureURL), nil
}
}
func (kc *Client) iTermCaptchaPrompt(captchaPictureURL string) (string, error) {
log.Printf("Detected iTerm, displaying URL: %s\n", captchaPictureURL)
imgResp, err := kc.client.Get(captchaPictureURL)
if err != nil {
return "", errors.Wrap(err, "unable to fetch captcha image")
}
var buf bytes.Buffer
b64Encoder := b64.NewEncoder(b64.StdEncoding, &buf)
_, _ = io.Copy(b64Encoder, imgResp.Body)
_ = b64Encoder.Close()
if os.Getenv("TERM") == "screen" {
log.Println("Detected tmux, using specific workaround...")
fmt.Printf("\033Ptmux;\033\033]1337;File=width=40;preserveAspectRatio=1;inline=1;:%s\a\033\\\n", buf.String())
} else {
fmt.Printf("\033]1337;File=width=40;preserveAspectRatio=1;inline=1;:%s\a\n", buf.String())
}
return prompter.String("Captcha", ""), nil
}
func simpleCaptchaPrompt(captchaPictureURL string) string {
log.Println("Open this link in a browser:\n", captchaPictureURL)
return prompter.String("Captcha", "")
}
func (kc *Client) loadFirstPage(loginDetails *creds.LoginDetails) (string, url.Values, error) {
firstPageURL := loginDetails.URL + "&hl=en&loc=US"
req, err := http.NewRequest("GET", firstPageURL, nil)
if err != nil {
return "", nil, errors.Wrap(err, "error retrieving login form from idp")
}
res, err := kc.client.Do(req)
if err != nil {
return "", nil, errors.Wrap(err, "failed to make request to login form")
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", nil, errors.Wrap(err, "error parsing first page html document")
}
doc.Url, err = url.Parse(firstPageURL)
if err != nil {
return "", url.Values{}, errors.Wrap(err, "failed to define URL for html doc")
}
authForm, submitURL, err := extractInputsByFormID(doc, "gaia_loginform", "challenge")
if err != nil {
return "", nil, errors.Wrap(err, "failed to build login form data")
}
return submitURL, authForm, err
}
func (kc *Client) loadLoginPage(submitURL string, referer string, authForm url.Values) (string, url.Values, error) {
req, err := http.NewRequest("POST", submitURL, strings.NewReader(authForm.Encode()))
if err != nil {
return "", nil, errors.Wrap(err, "error retrieving login form")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept-Language", "en-US")
req.Header.Set("Content-Language", "en-US")
req.Header.Set("Referer", referer)
res, err := kc.client.Do(req)
if err != nil {
return "", nil, errors.Wrap(err, "failed to make request to login form")
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", nil, errors.Wrap(err, "error parsing login page html document")
}
doc.Url, err = url.Parse(submitURL)
if err != nil {
return "", url.Values{}, errors.Wrap(err, "failed to define URL for html doc")
}
loginForm, loginURL, err := extractInputsByFormID(doc, "gaia_loginform", "challenge")
if err != nil {
return "", nil, errors.Wrap(err, "failed to build login form data")
}
return loginURL, loginForm, err
}
func (kc *Client) loadChallengePage(submitURL string, referer string, authForm url.Values, loginDetails *creds.LoginDetails) (*goquery.Document, error) {
authForm.Set("bgresponse", "js_enabled")
req, err := http.NewRequest("POST", submitURL, strings.NewReader(authForm.Encode()))
if err != nil {
return nil, errors.Wrap(err, "error retrieving login form")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept-Language", "en-US")
req.Header.Set("Content-Language", "en-US")
req.Header.Set("Referer", referer)
res, err := kc.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to make request to login form")
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, errors.Wrap(err, "error parsing login page html document")
}
doc.Url, err = url.Parse(submitURL)
if err != nil {
return nil, errors.Wrap(err, "failed to define URL for html doc")
}
errMsg := mustFindErrorMsg(doc)
if errMsg != "" {
return nil, errors.New("Invalid username or password")
}
secondFactorHeader := "This extra step shows it’s really you trying to sign in"
secondFactorHeader2 := "This extra step shows that it’s really you trying to sign in"
secondFactorHeader3 := "2-Step Verification"
secondFactorHeaderJp := "2 段階認証プロセス"
// have we been asked for 2-Step Verification
if extractNodeText(doc, "h2", secondFactorHeader) != "" ||
extractNodeText(doc, "h2", secondFactorHeader2) != "" ||
extractNodeText(doc, "h1", secondFactorHeader3) != "" ||
extractNodeText(doc, "h1", secondFactorHeaderJp) != "" {
responseForm, secondActionURL, err := extractInputsByFormID(doc, "challenge")
if err != nil {
return nil, errors.Wrap(err, "unable to extract challenge form")
}
logger.Debugf("secondActionURL: %s", secondActionURL)
switch {
case strings.Contains(secondActionURL, "challenge/totp"): // handle TOTP challenge
var token = loginDetails.MFAToken
if token == "" {
token = prompter.RequestSecurityCode("000000")
}
responseForm.Set("Pin", token)
responseForm.Set("TrustDevice", "on") // Don't ask again on this computer
return kc.loadResponsePage(secondActionURL, submitURL, responseForm)
case strings.Contains(secondActionURL, "challenge/ipp"): // handle SMS challenge
if extractNodeText(doc, "button", "Send text message") != "" {
responseForm.Set("SendMethod", "SMS") // extractInputsByFormID does not extract the name and value from <button> tag that is the form submit
doc, err = kc.loadResponsePage(secondActionURL, submitURL, responseForm)
if err != nil {
return nil, errors.Wrap(err, "failed to post sms request form")
}
doc.Url, err = url.Parse(submitURL)
if err != nil {
return nil, errors.Wrap(err, "failed to define URL for html doc")
}
submitURL = secondActionURL
responseForm, secondActionURL, err = extractInputsByFormID(doc, "challenge")
if err != nil {
return nil, errors.Wrap(err, "unable to extract challenge form")
}
logger.Debugf("After sms request secondActionURL: %s", secondActionURL)
}
var token = prompter.StringRequired("Enter SMS token: G-")
responseForm.Set("Pin", token)
responseForm.Set("TrustDevice", "on") // Don't ask again on this computer
return kc.loadResponsePage(secondActionURL, submitURL, responseForm)
case strings.Contains(secondActionURL, "challenge/sk"): // handle u2f challenge
facetComponents, err := url.Parse(secondActionURL)
if err != nil {
return nil, errors.Wrap(err, "unable to parse action URL for U2F challenge")
}
facet := facetComponents.Scheme + "://" + facetComponents.Host
challengeNonce := responseForm.Get("id-challenge")
appID, data := extractKeyHandles(doc, challengeNonce)
u2fClient, err := NewU2FClient(challengeNonce, appID, facet, data[0], &U2FDeviceFinder{})
if err != nil {
return nil, errors.Wrap(err, "Failed to prompt for second factor.")
}
response, err := u2fClient.ChallengeU2F()
if err != nil {
logger.WithError(err).Error("Second factor failed.")
return kc.skipChallengePage(doc, submitURL, secondActionURL, loginDetails)
}
responseForm.Set("id-assertion", response)
responseForm.Set("TrustDevice", "on")
return kc.loadResponsePage(secondActionURL, submitURL, responseForm)
case strings.Contains(secondActionURL, "challenge/az"): // handle phone challenge
dataAttrs := extractDataAttributes(doc, "div[data-context]", []string{"data-context", "data-gapi-url", "data-tx-id", "data-api-key", "data-tx-lifetime"})
logger.Debugf("prompt with data values: %+v", dataAttrs)
waitValues := map[string]string{
"txId": dataAttrs["data-tx-id"],
}
log.Println("Open the Google App, and tap 'Yes' on the prompt to sign in")
_, err := kc.postJSON(fmt.Sprintf("https://content.googleapis.com/cryptauth/v1/authzen/awaittx?alt=json&key=%s", dataAttrs["data-api-key"]), waitValues, submitURL)
if err != nil {
return nil, errors.Wrap(err, "unable to extract post wait tx form")
}
// responseForm.Set("Pin", token)
responseForm.Set("TrustDevice", "on") // Don't ask again on this computer
return kc.loadResponsePage(secondActionURL, submitURL, responseForm)
case strings.Contains(secondActionURL, "challenge/dp"): // handle device push challenge
if extraNumber := extractDevicePushExtraNumber(doc); extraNumber != "" {
log.Println("Check your phone and tap 'Yes' on the prompt, then tap the number:")
log.Printf("\t%v\n", extraNumber)
log.Println("Then press ENTER to continue.")
} else {
log.Print("Check your phone and tap 'Yes' on the prompt. Then press ENTER to continue.")
}
_, err := bufio.NewReader(os.Stdin).ReadBytes('\n')
if err != nil {
return nil, errors.Wrap(err, "error reading new line \\n")
}
responseForm.Set("TrustDevice", "on") // Don't ask again on this computer
return kc.loadResponsePage(secondActionURL, submitURL, responseForm)
case strings.Contains(secondActionURL, "challenge/skotp"): // handle one-time HOTP challenge
log.Println("Get a one-time code by visiting https://g.co/sc on another device where you can use your security key")
var token = prompter.RequestSecurityCode("000 000")
responseForm.Set("Pin", token)
responseForm.Set("TrustDevice", "on") // Don't ask again on this computer
return kc.loadResponsePage(secondActionURL, submitURL, responseForm)
}
return kc.skipChallengePage(doc, submitURL, secondActionURL, loginDetails)
} else if extractNodeText(doc, "h2", "To sign in to your Google Account, choose a task from the list below.") != "" {
return kc.loadChallengeEntryPage(doc, submitURL, loginDetails)
}
return doc, nil
}
func (kc *Client) skipChallengePage(doc *goquery.Document, submitURL string, secondActionURL string, loginDetails *creds.LoginDetails) (*goquery.Document, error) {
skipResponseForm, skipActionURL, err := extractInputsByFormQuery(doc, `[action$="skip"]`)
if err != nil {
return nil, errors.Wrap(err, "unable to extract skip form")
}
if skipActionURL == "" {
return nil, errors.Errorf("unsupported second factor: %s", secondActionURL)
}
return kc.loadAlternateChallengePage(skipActionURL, submitURL, skipResponseForm, loginDetails)
}
func (kc *Client) loadAlternateChallengePage(submitURL string, referer string, authForm url.Values, loginDetails *creds.LoginDetails) (*goquery.Document, error) {
req, err := http.NewRequest("POST", submitURL, strings.NewReader(authForm.Encode()))
if err != nil {
return nil, errors.Wrap(err, "error retrieving login form")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept-Language", "en-US")
req.Header.Set("Content-Language", "en-US")
req.Header.Set("Referer", referer)
res, err := kc.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to make request to login form")
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, errors.Wrap(err, "error parsing login page html document")
}
doc.Url, err = url.Parse(submitURL)
if err != nil {
return nil, errors.Wrap(err, "failed to define URL for html doc")
}
return kc.loadChallengeEntryPage(doc, submitURL, loginDetails)
}
func (kc *Client) loadChallengeEntryPage(doc *goquery.Document, submitURL string, loginDetails *creds.LoginDetails) (*goquery.Document, error) {
var challengeEntry string
doc.Find("form[data-challengeentry]").EachWithBreak(func(i int, s *goquery.Selection) bool {
action, ok := s.Attr("action")
if !ok {
return true
}
if strings.Contains(action, "challenge/totp/") ||
strings.Contains(action, "challenge/ipp/") ||
strings.Contains(action, "challenge/az/") ||
strings.Contains(action, "challenge/skotp/") {
challengeEntry, _ = s.Attr("data-challengeentry")
return false
}
return true
})
if challengeEntry == "" {
return nil, errors.New("unable to find supported second factor")
}
query := fmt.Sprintf(`[data-challengeentry="%s"]`, challengeEntry)
responseForm, newActionURL, err := extractInputsByFormQuery(doc, query)
if err != nil {
return nil, errors.Wrap(err, "unable to extract challenge form")
}
return kc.loadChallengePage(newActionURL, submitURL, responseForm, loginDetails)
}
func (kc *Client) postJSON(submitURL string, values map[string]string, referer string) (*http.Response, error) {
data, _ := json.Marshal(values)
req, err := http.NewRequest("POST", submitURL, bytes.NewReader(data))
if err != nil {
return nil, errors.Wrap(err, "error retrieving login form")
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept-Language", "en-US")
req.Header.Set("Content-Language", "en-US")
req.Header.Set("Referer", referer)
res, err := kc.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to post JSON")
}
return res, nil
}
func (kc *Client) loadResponsePage(submitURL string, referer string, responseForm url.Values) (*goquery.Document, error) {
req, err := http.NewRequest("POST", submitURL, strings.NewReader(responseForm.Encode()))
if err != nil {
return nil, errors.Wrap(err, "error retrieving response page")
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept-Language", "en")
req.Header.Set("Content-Language", "en-US")
req.Header.Set("Referer", submitURL)
res, err := kc.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to make request to login form")
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, errors.Wrap(err, "error parsing login page html document")
}
return doc, nil
}
func mustFindInputByName(doc *goquery.Document, name string) string {
var fieldValue string
q := fmt.Sprintf(`input[name="%s"]`, name)
doc.Find(q).Each(func(i int, s *goquery.Selection) {
val, ok := s.Attr("value")
if !ok {
logger.Fatal("unable to locate field value")
}
fieldValue = val
})
return fieldValue
}
func mustFindErrorMsg(doc *goquery.Document) string {
var fieldValue string
doc.Find(".error-msg").Each(func(i int, s *goquery.Selection) {
fieldValue = s.Text()
})
return fieldValue
}
func extractInputsByFormID(doc *goquery.Document, formID ...string) (url.Values, string, error) {
// First try to find form by specific id
for _, id := range formID {
formData, actionURL, err := extractInputsByFormQuery(doc, fmt.Sprintf("#%s", id))
if err == nil && actionURL != "" {
return formData, actionURL, nil
}
}
// If no form found by id or actionURL in the previous forms, search for any form
formData, actionURL, err := extractInputsByFormQuery(doc, "")
if err != nil && actionURL != "" {
return formData, actionURL, errors.New("could not find any forms with actions")
}
if len(formData) == 0 {
return nil, "", errors.New("could not find any forms")
}
// Fallback in case no forms with actionURL were found
return formData, actionURL, err
}
func extractInputsByFormQuery(doc *goquery.Document, formQuery string) (url.Values, string, error) {
formData := url.Values{}
var actionAttr string
query := fmt.Sprintf("form%s", formQuery)
currentURL := doc.Url.String()
//get action url
foundForms := doc.Find(query)
if len(foundForms.Nodes) == 0 {
return formData, "", fmt.Errorf("could not find form with query %q", query)
}
foundForms.Each(func(i int, s *goquery.Selection) {
action, ok := s.Attr("action")
if !ok {
return
}
actionAttr = action
})
actionURL, err := generateFullURLIfRelative(actionAttr, currentURL)
if err != nil {
return formData, "", errors.Wrap(err, "error getting action URL")
}
query = fmt.Sprintf("form%s", formQuery)
// extract form data to passthrough
doc.Find(query).Find("input").Each(func(i int, s *goquery.Selection) {
name, ok := s.Attr("name")
if !ok {
return
}
val, ok := s.Attr("value")
if !ok {
return
}
logger.Debugf("name: %s value: %s", name, val)
formData.Add(name, val)
})
return formData, actionURL, nil
}
func extractNodeText(doc *goquery.Document, tag, txt string) string {
var res string
doc.Find(tag).Each(func(i int, s *goquery.Selection) {
if s.Text() == txt {
res = s.Text()
}
})
return res
}
func extractDataAttributes(doc *goquery.Document, query string, attrsToSelect []string) map[string]string {
dataAttrs := make(map[string]string)
doc.Find(query).Each(func(_ int, sel *goquery.Selection) {
for _, f := range attrsToSelect {
if val, ok := sel.Attr(f); ok {
dataAttrs[f] = val
}
}
})
return dataAttrs
}
func extractKeyHandles(doc *goquery.Document, challengeTxt string) (string, []string) {
appId := ""
keyHandles := []string{}
result := map[string]interface{}{}
doc.Find("div[jsname=C0oDBd]").Each(func(_ int, sel *goquery.Selection) {
val, ok := sel.Attr("data-challenge-ui")
if ok {
firstIdx := strings.Index(val, "{")
lastIdx := strings.LastIndex(val, "}")
obj := []byte(val[firstIdx : lastIdx+1])
_ = json.Unmarshal(obj, &result) // ignore the error and continue
// Key handles
for _, val := range result {
list, ok := val.([]interface{})
if !ok {
continue
}
tmpId, stringList := filterKeyHandleList(list, challengeTxt)
if tmpId != "" {
appId = tmpId
}
if len(stringList) != 0 {
keyHandles = append(keyHandles, stringList...)
}
}
}
})
return appId, keyHandles
}
func filterKeyHandleList(list []interface{}, challengeTxt string) (string, []string) {
appId := ""
newList := []string{}
for _, entry := range list {
if entry == nil {
continue
}
moreList, ok := entry.([]interface{})
if ok {
id, l := filterKeyHandleList(moreList, challengeTxt)
if id != "" {
appId = id
}
newList = append(newList, l...)
continue
}
str, ok := entry.(string)
if !ok {
continue
}
if appId == "" {
appId = isAppId(str)
}
if isKeyHandle(str, challengeTxt) {
newList = append(newList, str)
}
}
return appId, newList
}
func generateFullURLIfRelative(destination, currentPageURL string) (string, error) {
if string(destination[0]) == "/" {
currentURLParsed, err := url.Parse(currentPageURL)
if err != nil {
return "", errors.Wrap(err, "error generating full URL")
}
return fmt.Sprintf("%s://%s%s", currentURLParsed.Scheme, currentURLParsed.Host, destination), nil
} else {
return destination, nil
}
}
func isKeyHandle(key, challengeTxt string) bool {
_, err := b64.StdEncoding.DecodeString(key)
if err != nil {
return false
}
return key != challengeTxt
}
func isAppId(val string) string {
obj := map[string]interface{}{}
err := json.Unmarshal([]byte(val), &obj)
if err != nil {
return ""
}
appId, ok := obj["appid"].(string)
if !ok {
return ""
}
return appId
}
func extractDevicePushExtraNumber(doc *goquery.Document) string {
extraNumber := ""
doc.Find("div[jsname=feLNVc]").Each(func(_ int, s *goquery.Selection) {
extraNumber = s.Text()
})
return extraNumber
}