-
Notifications
You must be signed in to change notification settings - Fork 23
/
login.go
273 lines (232 loc) · 7.44 KB
/
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
package steam
import (
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
"strings"
"time"
)
type LoginResponse struct {
Success bool `json:"success"`
PublicKeyMod string `json:"publickey_mod"`
PublicKeyExp string `json:"publickey_exp"`
Timestamp string
TokenGID string
}
type OAuth struct {
SteamID SteamID `json:"steamid,string"`
Token string `json:"oauth_token"`
WGToken string `json:"wgtoken"`
WGTokenSecure string `json:"wgtoken_secure"`
WebCookie string `json:"webcookie"`
}
type LoginSession struct {
Success bool `json:"success"`
LoginComplete bool `json:"login_complete"`
RequiresTwoFactor bool `json:"requires_twofactor"`
Message string `json:"message"`
RedirectURI string `json:"redirect_uri"`
OAuthInfo string `json:"oauth"`
}
type Session struct {
client *http.Client
oauth OAuth
sessionID string
apiKey string
deviceID string
umqID string
chatMessage int
language string
}
const (
httpXRequestedWithValue = "com.valvesoftware.android.steam.community"
httpUserAgentValue = "Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"
httpAcceptValue = "text/javascript, text/html, application/xml, text/xml, */*"
)
var (
ErrInvalidUsername = errors.New("invalid username")
ErrNeedTwoFactor = errors.New("invalid twofactor code")
)
func (session *Session) proceedDirectLogin(response *LoginResponse, accountName, password, twoFactorCode string) error {
var n big.Int
n.SetString(response.PublicKeyMod, 16)
exp, err := strconv.ParseInt(response.PublicKeyExp, 16, 32)
if err != nil {
return err
}
pub := rsa.PublicKey{N: &n, E: int(exp)}
rsaOut, err := rsa.EncryptPKCS1v15(rand.Reader, &pub, []byte(password))
if err != nil {
return err
}
req, err := http.NewRequest(
http.MethodPost,
"https://steamcommunity.com/login/dologin/?"+url.Values{
"captcha_text": {""},
"captchagid": {"-1"},
"emailauth": {""},
"emailsteamid": {""},
"password": {base64.StdEncoding.EncodeToString(rsaOut)},
"remember_login": {"true"},
"rsatimestamp": {response.Timestamp},
"twofactorcode": {twoFactorCode},
"username": {accountName},
"oauth_client_id": {"DE45CD61"},
"oauth_scope": {"read_profile write_profile read_client write_client"},
"loginfriendlyname": {"#login_emailauth_friendlyname_mobile"},
"donotcache": {strconv.FormatInt(time.Now().Unix()*1000, 10)},
}.Encode(),
nil,
)
if err != nil {
return err
}
req.Header.Add("X-Requested-With", httpXRequestedWithValue)
req.Header.Add("Referer", "https://steamcommunity.com/mobilelogin?oauth_client_id=DE45CD61&oauth_scope=read_profile%20write_profile%20read_client%20write_client")
req.Header.Add("User-Agent", httpUserAgentValue)
req.Header.Add("Accept", httpAcceptValue)
resp, err := session.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
var loginSession LoginSession
if err := json.NewDecoder(resp.Body).Decode(&loginSession); err != nil {
return err
}
if !loginSession.Success {
if loginSession.RequiresTwoFactor {
return ErrNeedTwoFactor
}
return errors.New(loginSession.Message)
}
if err := json.Unmarshal([]byte(loginSession.OAuthInfo), &session.oauth); err != nil {
return err
}
randomBytes := make([]byte, 6)
if _, err := rand.Read(randomBytes); err != nil {
return err
}
sessionID := make([]byte, hex.EncodedLen(len(randomBytes)))
hex.Encode(sessionID, randomBytes)
session.sessionID = string(sessionID)
url, _ := url.Parse("https://steamcommunity.com")
cookies := session.client.Jar.Cookies(url)
for _, cookie := range cookies {
if cookie.Name == "mobileClient" || cookie.Name == "mobileClientVersion" || cookie.Name == "steamCountry" || strings.Contains(cookie.Name, "steamMachineAuth") {
// remove by setting max age -1
cookie.MaxAge = -1
}
}
sum := md5.Sum([]byte(accountName + password))
session.deviceID = fmt.Sprintf(
"android:%x-%x-%x-%x-%x",
sum[:2], sum[2:4], sum[4:6], sum[6:8], sum[8:10],
)
session.client.Jar.SetCookies(
url,
append(cookies, &http.Cookie{
Name: "sessionid",
Value: session.sessionID,
}),
)
return nil
}
func (session *Session) makeLoginRequest(accountName, password string) (*LoginResponse, error) {
req, err := http.NewRequest(http.MethodPost, "https://steamcommunity.com/login/getrsakey?username="+accountName, nil)
if err != nil {
return nil, err
}
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
req.Header.Add("X-Requested-With", httpXRequestedWithValue)
req.Header.Add("Referer", "https://steamcommunity.com/mobilelogin?oauth_client_id=DE45CD61&oauth_scope=read_profile%20write_profile%20read_client%20write_client")
req.Header.Add("User-Agent", httpUserAgentValue)
req.Header.Add("Accept", httpAcceptValue)
cookies := []*http.Cookie{
{Name: "mobileClientVersion", Value: "0 (2.1.3)"},
{Name: "mobileClient", Value: "android"},
{Name: "Steam_Language", Value: session.language},
{Name: "timezoneOffset", Value: "0,0"},
}
url, _ := url.Parse("https://steamcommunity.com")
jar.SetCookies(url, cookies)
session.client.Jar = jar
resp, err := session.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
var response LoginResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, err
}
if !response.Success {
return nil, ErrInvalidUsername
}
return &response, nil
}
// LoginTwoFactorCode logs in with the @twoFactorCode provided,
// note that in the case of having shared secret known, then it's better to
// use Login() because it's more accurate.
// Note: You can provide an empty two factor code if two factor authentication is not
// enabled on the account provided.
func (session *Session) LoginTwoFactorCode(accountName, password, twoFactorCode string) error {
response, err := session.makeLoginRequest(accountName, password)
if err != nil {
return err
}
return session.proceedDirectLogin(response, accountName, password, twoFactorCode)
}
// Login requests log in information first, then generates two factor code, and proceeds
// to do the actual login, this provides a better chance that the code generated will work
// because of the slowness of the API.
func (session *Session) Login(accountName, password, sharedSecret string, timeOffset time.Duration) error {
response, err := session.makeLoginRequest(accountName, password)
if err != nil {
return err
}
var twoFactorCode string
if len(sharedSecret) != 0 {
if twoFactorCode, err = GenerateTwoFactorCode(sharedSecret, time.Now().Add(timeOffset).Unix()); err != nil {
return err
}
}
return session.proceedDirectLogin(response, accountName, password, twoFactorCode)
}
func (session *Session) GetSteamID() SteamID {
return session.oauth.SteamID
}
func (session *Session) SetLanguage(lang string) {
session.language = lang
}
func NewSessionWithAPIKey(apiKey string) *Session {
return &Session{
client: &http.Client{},
apiKey: apiKey,
language: "english",
}
}
func NewSession(client *http.Client, apiKey string) *Session {
return &Session{
client: client,
apiKey: apiKey,
language: "english",
}
}