-
Notifications
You must be signed in to change notification settings - Fork 2
/
check.go
118 lines (109 loc) · 3.04 KB
/
check.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
const (
apiURL = "https://login.microsoftonline.com/common/GetCredentialType"
userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
)
func checkEmail(username, proxy string) error {
resp, err := doPost(username, proxy)
if err != nil {
return err
}
// Return early to avoid false positives
if resp.ThrottleStatus == 1 {
return ErrRateLimiting{}
}
switch resp.IfExistsResult {
case 0:
return nil
case 1:
return ErrUsernameDoesNotExist{}
case 5:
return nil
case 6:
return nil
default:
return fmt.Errorf("undefined error")
}
}
type GetCredentialTypeBody struct {
Username string `json:"username"`
}
type GetCredentialTypeResponse struct {
Credentials struct {
CertAuthParams interface{} `json:"CertAuthParams"`
FacebookParams interface{} `json:"FacebookParams"`
FidoParams interface{} `json:"FidoParams"`
GoogleParams interface{} `json:"GoogleParams"`
HasPassword bool `json:"HasPassword"`
PrefCredential int64 `json:"PrefCredential"`
RemoteNgcParams interface{} `json:"RemoteNgcParams"`
SasParams interface{} `json:"SasParams"`
} `json:"Credentials"`
Display string `json:"Display"`
EstsProperties struct {
DomainType int64 `json:"DomainType"`
UserTenantBranding interface{} `json:"UserTenantBranding"`
} `json:"EstsProperties"`
IfExistsResult int64 `json:"IfExistsResult"`
IsSignupDisallowed bool `json:"IsSignupDisallowed"`
IsUnmanaged bool `json:"IsUnmanaged"`
ThrottleStatus int64 `json:"ThrottleStatus"`
Username string `json:"Username"`
APICanary string `json:"apiCanary"`
}
func doPost(username, proxy string) (GetCredentialTypeResponse, error) {
body, err := json.Marshal(&GetCredentialTypeBody{
Username: username,
})
if err != nil {
return GetCredentialTypeResponse{}, err
}
client := &http.Client{
Timeout: 5 * time.Second,
}
if proxy != "" {
transport, err := createTransport(proxy)
if err != nil {
return GetCredentialTypeResponse{}, fmt.Errorf("error creating transport: %v", err)
}
client.Transport = transport
}
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(body))
if err != nil {
return GetCredentialTypeResponse{}, err
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Accept", "application/json")
req.Header.Set("Connection", "close")
var getCredResp GetCredentialTypeResponse
resp, err := client.Do(req)
if err != nil {
return GetCredentialTypeResponse{}, err
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(b, &getCredResp)
if err != nil {
return GetCredentialTypeResponse{}, err
}
return getCredResp, nil
}
func createTransport(proxy string) (*http.Transport, error) {
proxyURL, err := url.Parse(proxy)
if err != nil {
return &http.Transport{}, fmt.Errorf("cannot parse proxy url: %v", proxy)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
return transport, nil
}