-
Notifications
You must be signed in to change notification settings - Fork 7
/
user_login.go
287 lines (257 loc) · 7.36 KB
/
user_login.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
package mobileauth
import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
)
// ErrBadRSA is returned by DoLogin
// when steam returns success false to rsa public key request
var ErrBadRSA = errors.New("bad RSA")
// ErrBadCredentials is returned by DoLogin
// when steam fails to authenticate us
var ErrBadCredentials = errors.New("bad credentials")
// ErrNeedCaptcha is returned by DoLogin
// when captcha code is required
var ErrNeedCaptcha = errors.New("need captcha")
// ErrNeed2FA is returned by DoLogin
// when mobile code is required
var ErrNeed2FA = errors.New("need 2FA")
// ErrNeedEmail is returned by DoLogin
// when email code is required
var ErrNeedEmail = errors.New("need email")
// ErrNeedTooManyFailedLogins is returned by DoLogin
var ErrTooManyFailedLogins = errors.New("two many failed logins")
// Handles logging the user into the mobile Steam website.
// Necessary to generate OAuth token and session cookies.
type UserLogin struct {
Username string
Password string
SteamID uint64
RequiresCaptcha bool
CaptchaGID string
CaptchaText string
RequiresEmail bool
EmailDomain string
EmailCode string
Requires2FA bool
TwoFactorCode string
Session *SessionData
LoggedIn bool
_cookies *cookiejar.Jar
}
func NewUserLogin(username, password string) *UserLogin {
cookies, _ := cookiejar.New(nil)
return &UserLogin{
Username: username,
Password: password,
_cookies: cookies,
}
}
func (ul *UserLogin) DoLogin() error {
cookies := ul._cookies
if len(cookies.Cookies(cookiePath)) == 0 {
//Generate a SessionID
cookies.SetCookies(cookiePath, []*http.Cookie{
&http.Cookie{
Name: "mobileClientVersion",
Value: "0 (2.1.3)",
Path: "/",
Domain: ".steamcommunity.com",
},
&http.Cookie{
Name: "mobileClient",
Value: "android",
Path: "/",
Domain: ".steamcommunity.com",
},
&http.Cookie{
Name: "Steam_Language",
Value: "english",
Path: "/",
Domain: ".steamcommunity.com",
},
})
headers := make(map[string]string)
headers["X-Requested-With"] = "com.valvesoftware.android.steam.community"
_, err := MobileLoginRequest(UrlCommunityBase+"/login?oauth_client_id=DE45CD61&oauth_scope=read_profile%20write_profile%20read_client%20write_client", "GET", nil, cookies, &headers)
if err != nil {
return err
}
}
postData := url.Values{}
postData.Set("username", ul.Username)
respBody, err := MobileLoginRequest(UrlCommunityBase+"/login/getrsakey", "POST", &postData, cookies, nil)
if err != nil {
return err
}
r := rsaResponse{}
if err = json.Unmarshal(respBody, &r); err != nil {
return fmt.Errorf("failed to unmarshal rsa response: %v", err)
}
if !r.Success {
return ErrBadRSA
}
// Rsa modulus
modulusBytes, err := hex.DecodeString(r.Modulus)
if err != nil {
return err
}
modulus := big.NewInt(0)
modulus.SetBytes(modulusBytes)
// Rsa exponent
exponentBytes, err := hex.DecodeString(r.Exponent)
if err != nil {
return err
}
exponent := big.NewInt(0)
exponent.SetBytes(exponentBytes)
// Generate encrypted password
publicKey := rsa.PublicKey{
N: modulus,
E: int(exponent.Int64()),
}
passwordBytes := []byte(ul.Password)
encryptedPasswordBytes, err := rsa.EncryptPKCS1v15(rand.Reader, &publicKey, passwordBytes)
if err != nil {
return err
}
encryptedPassword := base64.StdEncoding.EncodeToString(encryptedPasswordBytes)
// Create request params
postData = url.Values{}
postData.Set("username", ul.Username)
postData.Set("password", encryptedPassword)
postData.Set("twofactorcode", ul.TwoFactorCode)
// TODO: is this parameters not required???
if ul.RequiresCaptcha {
postData.Set("captchagid", ul.CaptchaGID)
postData.Set("captcha_text", ul.CaptchaText)
}
if ul.Requires2FA || ul.RequiresEmail {
postData.Set("emailsteamid", strconv.FormatUint(ul.SteamID, 10))
}
if ul.RequiresEmail {
postData.Set("emailauth", ul.EmailCode)
}
postData.Set("rsatimestamp", r.Timestamp)
postData.Set("remember_login", "false")
postData.Set("oauth_client_id", "DE45CD61")
postData.Set("oauth_scope", "read_profile write_profile read_client write_client")
postData.Set("loginfriendlyname", "#login_emailauth_friendlyname_mobile")
postData.Set("donotcache", strconv.FormatInt(time.Now().Unix(), 10))
// Make request
respBody, err = MobileLoginRequest(UrlCommunityBase+"/login/dologin", "POST", &postData, cookies, nil)
if err != nil {
return err
}
// Process response
r2 := loginResponse{}
if err = json.Unmarshal(respBody, &r2); err != nil {
return err
}
if r2.Message != "" && strings.Contains(r2.Message, "Incorrect login") {
return ErrBadCredentials
}
if r2.CaptchaNeeded {
ul.RequiresCaptcha = true
ul.CaptchaGID = r2.CaptchaGID.String()
return ErrNeedCaptcha
} else {
ul.RequiresCaptcha = false
}
if r2.EmailAuthNeeded {
ul.RequiresEmail = true
ul.SteamID = r2.EmailSteamID
return ErrNeedEmail
} else {
ul.RequiresEmail = false
}
if r2.TwoFactorNeeded && !r2.Success {
ul.Requires2FA = true
return ErrNeed2FA
} else {
ul.Requires2FA = false
}
if r2.Message != "" && strings.Contains(r2.Message, "too many login failures") {
return ErrTooManyFailedLogins
}
if !r2.LoginComplete {
return ErrBadCredentials
}
if r2.OAuth == nil || r2.OAuth.OAuthToken == "" {
return errors.New("steam does not return oauth data")
}
// Get sessionid from cookies
var sessionID string
for _, cookie := range cookies.Cookies(cookiePath) {
if cookie.Name == "sessionid" {
sessionID = cookie.Value
}
}
// Set session data
stringSteamID := strconv.FormatUint(r2.OAuth.SteamID, 10)
session := SessionData{
OAuthToken: r2.OAuth.OAuthToken,
SteamID: r2.OAuth.SteamID,
SteamLogin: stringSteamID + "%7C%7C" + r2.OAuth.SteamLogin,
SteamLoginSecure: stringSteamID + "%7C%7C" + r2.OAuth.SteamLoginSecure,
WebCookie: r2.OAuth.Webcookie,
SessionID: sessionID,
}
ul.Session = &session
ul.LoggedIn = true
return nil
}
type loginResponse struct {
Success bool `json:"success"`
LoginComplete bool `json:"login_complete"`
OAuth *oAuthResult `json:"oauth"`
CaptchaNeeded bool `json:"captcha_needed"`
CaptchaGID uniStr `json:"captcha_gid"`
EmailSteamID uint64 `json:"emailsteamid,string"`
EmailAuthNeeded bool `json:"emailauth_needed"`
TwoFactorNeeded bool `json:"requires_twofactor"`
Message string `json"message"`
}
type oAuthResult struct {
SteamID uint64 `json:"steamid,string"`
OAuthToken string `json:"oauth_token"`
SteamLogin string `json:"wgtoken"`
SteamLoginSecure string `json:"wgtoken_secure"`
Webcookie string `json:"webcookie"`
}
func (o *oAuthResult) UnmarshalJSON(data []byte) error {
// no oauth data
if len(data) < 4 {
return nil
}
// unquote
unquotedData, err := strconv.Unquote(string(data))
if err != nil {
return errors.New("failed to unquote oauth data")
}
// unmarshal
type Alias oAuthResult
aux := (*Alias)(o)
if err = json.Unmarshal([]byte(unquotedData), &aux); err != nil {
return err
}
return nil
}
type rsaResponse struct {
Success bool `json:"success"`
Exponent string `json:"publickey_exp"`
Modulus string `json:"publickey_mod"`
Timestamp string `json:"timestamp"`
SteamID uint64 `json:"steamid"`
}