forked from freman/go-steamauth
/
userlogin.go
309 lines (262 loc) · 8.32 KB
/
userlogin.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
// Copyright 2015 Shannon Wynter. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package steamauth
import (
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"errors"
"math/big"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"time"
)
// UserLogin lets you log in to steam as a user.
// Once you provide it with a username and a password
// you are free to try to DoLogin().
//
// This can be used on it's own without SteamGuardAccount
// provided you have the ability to access your email
// and parse the tokens
type UserLogin struct {
Username string
Password string
SteamID SteamID
RequiresCaptcha bool
CaptchaGID CaptchaGID
CaptchaText string
RequiresEmail bool
EmailDomain string
EmailCode string
Requires2FA bool
TwoFactorCode string
Session *SessionData
LoggedIn bool
cookieJar *cookiejar.Jar
}
// NewUserLogin allocates and returns a new UserLogin.
func NewUserLogin(username, password string) *UserLogin {
cookieJar, _ := cookiejar.New(&cookiejar.Options{})
return &UserLogin{
cookieJar: cookieJar,
Session: &SessionData{},
Username: username,
Password: password}
}
// DoLogin actually attempt to login.
// Creates a fresh session if required.
// Grabs the RSA public key.
// Encrypts your password.
// Attempts to authenticate.
func (u *UserLogin) DoLogin() (LoginResult, error) {
postData := url.Values{}
cookieJar := u.cookieJar
if len(cookieJar.Cookies(APIEndpoints.CommunityBase)) == 0 {
log("Creating new 'empty' sesson")
u.Session.SetCookies(cookieJar)
SteamWeb().
SetJar(cookieJar).
AddHeader("X-Requested-With", "com.valvesoftware.android.steam.community").
Get(APIEndpoints.CommunityBase.String() + "/login?oauth_client_id=DE45CD61&oauth_scope=read_profile%20write_profile%20read_client%20write_client").
MobileLoginRequest()
}
logf("Retriving RSAKey for %s", u.Username)
postData.Set("username", u.Username)
rsaResponse := rsaResponse{}
_, err := SteamWeb().
SetJar(cookieJar).
SetParams(postData).
Post(APIEndpoints.CommunityBase.String() + "/login/getrsakey").
HandleJSON(&rsaResponse).
MobileLoginRequest()
if err != nil {
return LoginGeneralFailure, err
}
encryptedPassword, err := rsa.EncryptPKCS1v15(rand.Reader, rsaResponse.PublicKey, []byte(u.Password))
if err != nil {
return BadRSA, err
}
postData.Set("password", base64.StdEncoding.EncodeToString(encryptedPassword))
postData.Set("twofactorcode", u.TwoFactorCode)
postData.Set("captchagid", iif(u.RequiresCaptcha, u.CaptchaGID.String(), "-1"))
postData.Set("captcha_text", iif(u.RequiresCaptcha, u.CaptchaText, ""))
postData.Set("emailsteamid", iif(u.Requires2FA || u.RequiresEmail, u.SteamID.String(), ""))
postData.Set("emailauth", iif(u.RequiresEmail, u.EmailCode, ""))
postData.Set("rsatimestamp", rsaResponse.Timestamp.String())
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))
logf("Attempting to authenticate as %s", u.Username)
loginResponse := LoginResponse{}
_, err = SteamWeb().
SetJar(cookieJar).
SetParams(postData).
Post(APIEndpoints.CommunityBase.String() + "/login/dologin").
HandleJSON(&loginResponse).
MobileLoginRequest()
if err != nil {
logf("Protocol error %s", err)
return LoginGeneralFailure, err
}
if loginResponse.CaptchaNeeded {
log(NeedCaptcha)
u.RequiresCaptcha = true
u.CaptchaGID = loginResponse.CaptchaGID
return NeedCaptcha, nil
}
if loginResponse.EmailAuthNeeded {
log(NeedEmail)
u.RequiresEmail = true
u.SteamID = loginResponse.SteamID
logf("Have steamid... it is %#v", u.SteamID)
return NeedEmail, nil
}
if loginResponse.TwoFactorNeeded && !loginResponse.Success {
log(Need2FA)
u.Requires2FA = true
return Need2FA, nil
}
if !loginResponse.LoginComplete {
log(BadCredentials)
if loginResponse.Message != "" {
log(loginResponse)
return BadCredentials, errors.New(loginResponse.Message)
}
return BadCredentials, nil
}
if loginResponse.OAuth == nil || len(loginResponse.OAuth.OAuthToken) == 0 {
logf("Protocol error: missing oath")
return LoginGeneralFailure, errors.New("missing oauth")
}
var sessionCookie *http.Cookie
for _, cookie := range cookieJar.Cookies(APIEndpoints.CommunityBase) {
if cookie.Name == "sessionid" {
sessionCookie = cookie
break
}
}
if sessionCookie == nil {
logf("Protocol error: missing session cookie")
return LoginGeneralFailure, errors.New("missing session cookie")
}
u.Session = &SessionData{
OAuthToken: loginResponse.OAuth.OAuthToken,
SteamID: loginResponse.OAuth.SteamID,
SteamLogin: loginResponse.OAuth.SteamID.String() + "%7C%7C" + loginResponse.OAuth.SteamLogin,
SteamLoginSecure: loginResponse.OAuth.SteamID.String() + "%7C%7C" + loginResponse.OAuth.SteamLoginSecure,
WebCookie: loginResponse.OAuth.Webcookie,
SessionID: sessionCookie.Value,
}
log(LoginOkay)
return LoginOkay, nil
}
// CaptchaURL returns a fully qualified URL to a given captcha GID
func (u *UserLogin) CaptchaURL() string {
return u.CaptchaGID.URL()
}
// LoginResponse represents the response sent from Steam servers.
type LoginResponse struct {
Success bool `json:"success"`
LoginComplete bool `json:"login_complete"`
OAuth *OAuth `json:"oauth"`
CaptchaNeeded bool `json:"captcha_needed"`
CaptchaGID CaptchaGID `json:"captcha_gid"`
SteamID SteamID `json:"emailsteamid"`
EmailAuthNeeded bool `json:"emailauth_needed"`
TwoFactorNeeded bool `json:"requires_twofactor"`
Message string `json:"message"`
}
// OAuth represents the embedded oauth object sent in the LoginResponse.
// This is actually sent as a string JSON blob but you don't have to
// deal with that, it's all handled for you.
type OAuth struct {
SteamID SteamID `json:"steamid"`
OAuthToken string `json:"oauth_token"`
SteamLogin string `json:"wgtoken"`
SteamLoginSecure string `json:"wgtoken_secure"`
Webcookie string `json:"webcookie"`
}
func (o *OAuth) UnmarshalJSON(b []byte) (err error) {
type localOAuth OAuth
var (
oauthObject = localOAuth{}
jsonString = ""
)
err = json.Unmarshal(b, &jsonString)
if te, ok := err.(*json.UnmarshalTypeError); ok && te.Value == "object" {
err = json.Unmarshal(b, &oauthObject)
} else {
err = json.Unmarshal([]byte(jsonString), &oauthObject)
}
if err == nil {
*o = OAuth(oauthObject)
}
return
}
type rsaResponse struct {
Success bool `json:"success"`
PublicKey *rsa.PublicKey
Timestamp timestamp `json:"timestamp"`
TokenGID string `json:"token_gid"`
SteamID SteamID `json:"steamid"`
}
func (r *rsaResponse) UnmarshalJSON(b []byte) error {
type localRSAResponse rsaResponse
localData := struct {
localRSAResponse
Modulus string `json:"publickey_mod"`
Exponent string `json:"publickey_exp"`
}{}
err := json.Unmarshal(b, &localData)
if err != nil {
return err
}
*r = rsaResponse(localData.localRSAResponse)
// Absolutly no point progressing beyond this point if there is no success
if !r.Success {
return nil
}
exponent, err := strconv.ParseInt(localData.Exponent, 16, 0)
modulus := big.Int{}
if _, ok := modulus.SetString(localData.Modulus, 16); !ok {
return errors.New("invalid modulus")
}
r.PublicKey = &rsa.PublicKey{
N: &modulus,
E: int(exponent),
}
return nil
}
// LoginResult is the type of login result.
// Should probably be replaced with a bunch of errors?
type LoginResult int
// Various LoginResults.
// You can call .String() to get a human representation.
// LoginGeneralFailure is more protocol level and usually has an err also.
const (
LoginOkay LoginResult = iota
LoginGeneralFailure
BadRSA
BadCredentials
NeedCaptcha
Need2FA
NeedEmail
)
var loginResponses = []string{
LoginOkay: "ok",
LoginGeneralFailure: "general failure",
BadRSA: "bad rsa",
BadCredentials: "bad credentials",
NeedCaptcha: "need captcha",
Need2FA: "need two factor authentication",
NeedEmail: "need email verification",
}
func (l LoginResult) String() string {
return loginResponses[l]
}