-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauthenticatorlinker.go
274 lines (226 loc) · 7.3 KB
/
authenticatorlinker.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
// 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/sha1"
"encoding/hex"
"net/http/cookiejar"
"net/url"
"strconv"
)
// AuthenticatorLinker will link this Authenticator to your steam account
// Once successfully linked LinkedAccount contains a SteamGuardAccount
// object and save it for reuse in future requests
type AuthenticatorLinker struct {
PhoneNumber string
DeviceID string
LinkedAccount SteamGuardAccount
Finalized bool
session *SessionData
cookieJar *cookiejar.Jar
}
// NewAuthenticatorLinker will create an account linker
//
// Pass it the SessionData from a logged in instance of
// a UserLogin structure.
func NewAuthenticatorLinker(session *SessionData) *AuthenticatorLinker {
cookieJar, _ := cookiejar.New(&cookiejar.Options{})
session.SetCookies(cookieJar)
return &AuthenticatorLinker{
session: session,
DeviceID: generateDeviceID(),
cookieJar: cookieJar,
}
}
// AddAuthenticator will configure your account for steamguard,
// create and link an instance of a SteamGuardAccount structure
//
// If everything goes well you will get a LinkResult of
// AwaitingFinalization which means you must collect the code
// sent to the linked phone and submit it with
// `FinalizeAddAuthenticator`
func (al *AuthenticatorLinker) AddAuthenticator() (LinkResult, error) {
hasPhone := al.hasPhoneAttached()
if hasPhone && al.PhoneNumber != "" {
log(MustRemovePhoneNumber)
return MustRemovePhoneNumber, nil
}
if !hasPhone && al.PhoneNumber == "" {
log(MustProvidePhoneNumber)
return MustProvidePhoneNumber, nil
}
if !hasPhone {
if !al.addPhoneNumber() {
log(LinkGeneralFailure)
return LinkGeneralFailure, nil
}
}
var postData = url.Values{
"access_token": []string{al.session.OAuthToken},
"steamid": []string{al.session.SteamID.String()},
"authenticator_type": []string{"1"},
"device_identifier": []string{al.DeviceID},
"sms_phone_id": []string{"1"},
}
logf("Attempting add authenticator for device %s", al.DeviceID)
addAuthenticatorResponse := AddAuthenticatorResponse{}
_, err := SteamWeb().
SetParams(postData).
Post(APIEndpoints.SteamAPIBase.String() + "/ITwoFactorService/AddAuthenticator/v0001").
HandleJSON(&addAuthenticatorResponse).
MobileLoginRequest()
if err != nil {
logf("Protocol error: %s", err)
return LinkGeneralFailure, err
}
if addAuthenticatorResponse.Response.Status != 1 {
logf("Protocol error: Response.Status was %d, expected 1", addAuthenticatorResponse.Response.Status)
return LinkGeneralFailure, nil
}
// SteamGuardAccount?
al.LinkedAccount = addAuthenticatorResponse.Response
al.LinkedAccount.Session = al.session
al.LinkedAccount.DeviceID = al.DeviceID
log(AwaitingFinalization)
return AwaitingFinalization, nil
}
// FinalizeAddAuthenticator does pretty much what it says
// once you've sucessfully called this method you need
// to save the instance of `SteamGuardAccount` stored in
// `LinkedAccount` or you risk losing access to your
// steam account
func (al *AuthenticatorLinker) FinalizeAddAuthenticator(smsCode string) (FinalizeResult, error) {
smsCodeGood := false
var postData = url.Values{
"steamid": []string{al.session.SteamID.String()},
"access_token": []string{al.session.OAuthToken},
"authenticator_code": []string{""},
"activation_code": []string{smsCode},
}
for tries := 0; tries <= 30; {
postData.Set("authenticator_code", iif(tries == 0, "", al.LinkedAccount.GenerateSteamGuardCode()))
postData.Set("authenticator_time", strconv.FormatInt(TimeAligner.GetSteamTime().Unix(), 10))
if smsCodeGood {
postData.Set("activation_code", "")
}
logf("Attempting finalize authentication, attempt %d of 30", tries+1)
finalizeResponse := FinalizeAuthenticatorResponse{}
_, err := SteamWeb().
Post(APIEndpoints.SteamAPIBase.String() + "/ITwoFactorService/FinalizeAddAuthenticator/v0001").
SetParams(postData).
HandleJSON(&finalizeResponse).
MobileLoginRequest()
if err != nil {
logf("Protocol error: %s", err)
return FinalizeGeneralFailure, err
}
if finalizeResponse.Response.Status == 89 {
log(BadSMSCode)
return BadSMSCode, nil
}
if finalizeResponse.Response.Status == 88 && tries >= 30 {
log(UnableToGenerateCorrectCodes)
return UnableToGenerateCorrectCodes, nil
}
if !finalizeResponse.Response.Success {
log("Protocol error: Response.Success == false")
return FinalizeGeneralFailure, nil
}
if finalizeResponse.Response.WantMore {
log("Steam wants more")
smsCodeGood = true
tries++
continue
}
al.LinkedAccount.FullyEnrolled = true
log(Success)
return Success, nil
}
return FinalizeGeneralFailure, nil
}
func (al *AuthenticatorLinker) addPhoneNumber() bool {
logf("Add phone number %s", al.PhoneNumber)
addPhoneResponse := AddPhoneResponse{}
_, err := SteamWeb().
SetJar(al.cookieJar).
Get(APIEndpoints.CommunityBase.String() + "/steamguard/phoneajax?op=add_phone_number&arg=" + url.QueryEscape(al.PhoneNumber)).
HandleJSON(&addPhoneResponse).
Do()
if err != nil {
log("Internal Error: ", err)
return false
}
return addPhoneResponse.Success
}
func (al *AuthenticatorLinker) hasPhoneAttached() bool {
logf("has phone attached?")
hasPhoneResponse := HasPhoneResponse{}
_, err := SteamWeb().
SetJar(al.cookieJar).
Get(APIEndpoints.CommunityBase.String() + "/steamguard/phoneajax?op=has_phone&arg=").
HandleJSON(hasPhoneResponse).
MobileLoginRequest()
if err != nil {
return false
}
return hasPhoneResponse.HasPhone
}
func generateDeviceID() string {
buf := make([]byte, 8)
if _, err := rand.Read(buf); err != nil {
return ""
}
sum := sha1.Sum(buf)
return "android:" + hex.EncodeToString(sum[:])
}
type LinkResult int
const (
MustProvidePhoneNumber LinkResult = iota // No phone number on the account
MustRemovePhoneNumber // A phone number is already on the account
AwaitingFinalization // Must provide an SMS code
LinkGeneralFailure // General failure (really now!)
)
var linkResults = []string{
MustProvidePhoneNumber: "must provide phone number",
MustRemovePhoneNumber: "must remove phone number",
AwaitingFinalization: "awaiting finalization",
LinkGeneralFailure: "general failure",
}
func (l LinkResult) String() string {
return linkResults[l]
}
type FinalizeResult int
const (
BadSMSCode FinalizeResult = iota // Bad code was given
UnableToGenerateCorrectCodes
Success
FinalizeGeneralFailure
)
var finalizeResults = []string{
BadSMSCode: "bad sms code",
UnableToGenerateCorrectCodes: "unable to generate correct codes",
Success: "success",
FinalizeGeneralFailure: "general failure",
}
func (f FinalizeResult) String() string {
return finalizeResults[f]
}
type AddAuthenticatorResponse struct {
Response SteamGuardAccount `json:"response"`
}
type FinalizeAuthenticatorResponse struct {
Response struct {
Status int `json:"status"`
ServerTime timestamp `json:"server_time"`
WantMore bool `json:"want_more"`
Success bool `json:"success"`
} `json:"response"`
}
type HasPhoneResponse struct {
HasPhone bool `json:"has_phone"`
}
type AddPhoneResponse struct {
Success bool `json:"success"`
}