-
Notifications
You must be signed in to change notification settings - Fork 0
/
twofactor.go
288 lines (238 loc) · 8.71 KB
/
twofactor.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
package api
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/shangate/steam-web/status"
"github.com/shangate/steam-web/utils"
"strconv"
"strings"
"time"
)
const (
STEAM_TWOFACTOR_REQUEST_ERROR = iota + 500000
STEAM_INVALID_SMS_CODE
STEAM_UNABLE_TO_GENERATE_CORRECT_CODE
)
var (
TWO_FACTOR_BASE = STEAM_API_WEB_BASE + "/ITwoFactorService/%s/v0001"
TWO_FACTOR_TIME_QUERY = strings.ReplaceAll(TWO_FACTOR_BASE, "%s", "QueryTime")
)
const (
STEAM_ADD_AUTHENTICATOR_WEBAPI_V1 = STEAM_API_WEB_BASE + "/ITwoFactorService/AddAuthenticator/v0001"
STEAM_FINALIZE_AUTHENTICATOR_WEBAPI_V1 = STEAM_API_WEB_BASE + "/ITwoFactorService/FinalizeAddAuthenticator/v0001"
STEAM_REMOVE_AUTHENTICATOR_WEBAPI_V1 = STEAM_API_WEB_BASE + "/ITwoFactorService/RemoveAuthenticator/v0001"
STEAM_QUERY_TIME_WEBAPI_V1 = STEAM_API_WEB_BASE + "/ITwoFactorService/QueryTime/v0001?steamid=0"
)
var (
chars = "23456789BCDFGHJKMNPQRTVWXY"
charsLen = uint32(len(chars))
)
func GetTwoFactor(sharedSecret string) string {
currentSteamTime := GetSteamTime()
data, err := base64.StdEncoding.DecodeString(sharedSecret)
if err != nil {
fmt.Printf("[TwoFactor] Error while decoding shared secret: %v", err.Error())
return ""
}
ful := make([]byte, 8)
binary.BigEndian.PutUint32(ful[4:], uint32(currentSteamTime/30))
hmac := hmac.New(sha1.New, data)
hmac.Write(ful)
sum := hmac.Sum(nil)
start := sum[19] & 0x0F
slice := binary.BigEndian.Uint32(sum[start:start+4]) & 0x7FFFFFFF
buf := make([]byte, 5)
for i := 0; i < 5; i++ {
buf[i] = chars[slice%charsLen]
slice /= charsLen
}
return string(buf)
}
type Authenticator struct {
SharedSecret string `json:"shared_secret"`
SerialNumber string `json:"serial_number"`
RevocationCode string `json:"revocation_code"`
Uri string `json:"uri"`
ServerTime string `json:"server_time"`
AccountName string `json:"account_name"`
TokenGid string `json:"token_gid"`
IdentitySecret string `json:"identity_secret"`
Secret1 string `json:"secret_1"`
Status int `json:"status"`
DeviceId string `json:"device_id"`
SteamId string `json:"steam_id"`
FullyEnrolled bool `json:"fully_enrolled"`
}
type authenticatorResponse struct {
Auth Authenticator `json:"response"`
}
type AddAuthenticatorRequest struct {
DeviceId string
SteamId uint64
AccessToken string
}
func AddAuthenticator(request AddAuthenticatorRequest) (auth Authenticator, err *status.Exception) {
headers := getDefaultMobileHeader()
query := map[string][]string{
"access_token": {request.AccessToken},
"steamid": {strconv.FormatUint(request.SteamId, 10)},
"device_identifier": {request.DeviceId},
"sms_phone_id": {"1"},
"authenticator_type": {"1"},
}
_, _, _, response, _, e := utils.HttpWebRequest("POST", STEAM_ADD_AUTHENTICATOR_WEBAPI_V1, headers, query, nil, nil, false, false)
if e != nil {
return auth, status.NewError(STEAM_REQUEST_ERROR, fmt.Sprintf("Add authenticator Error %s", e.Error()))
}
responseStr := string(response)
if responseStr == "" {
return auth, status.NewError(STEAM_REQUEST_ERROR, "Add authenticator Error")
}
var authResponse authenticatorResponse
er := json.Unmarshal(response, &authResponse)
if er != nil {
return auth, status.NewError(STEAM_REQUEST_ERROR, fmt.Sprintf("Add authenticator Error %s", er.Error()))
}
if authResponse.Auth.Status != 1 {
return auth, status.NewError(STEAM_REQUEST_FAIL, fmt.Sprintf("Add authenticator Failed %d", auth.Status))
}
if authResponse.Auth.SteamId == "" {
authResponse.Auth.SteamId = strconv.FormatUint(request.SteamId, 10)
}
if authResponse.Auth.DeviceId == "" {
authResponse.Auth.DeviceId = request.DeviceId
}
return authResponse.Auth, nil
}
type FinalizeAuthenticatorRequest struct {
SteamId uint64
SmsCode string
PhoneNum string
GuardCode string
AccessToken string
}
type finalizeAuthenticatorResponse struct {
Response struct {
Status int `json:"status"`
ServerTime string `json:"server_time"`
WantMore bool `json:"want_more"`
Success bool `json:"success"`
} `json:"response"`
}
func FinalizeAuthenticator(request FinalizeAuthenticatorRequest) (ok bool, err *status.Exception) {
headers := getDefaultMobileHeader()
query := map[string][]string{
"access_token": {request.AccessToken},
"steamid": {strconv.FormatUint(request.SteamId, 10)},
"activation_code": {request.SmsCode},
}
query["authenticator_code"] = []string{request.GuardCode}
query["authenticator_time"] = []string{strconv.FormatInt(GetSteamTime(), 10)}
_, _, _, responseData, _, e := utils.HttpWebRequest("POST", STEAM_FINALIZE_AUTHENTICATOR_WEBAPI_V1, headers, query, nil, nil, false, false)
if e != nil {
return false, status.NewError(STEAM_REQUEST_ERROR, fmt.Sprintf("Finalize authenticator Error %s", e.Error()))
}
responseDataStr := string(responseData)
if responseDataStr == "" {
return false, status.NewError(STEAM_REQUEST_ERROR, "Finalize authenticator Error")
}
var finalizeAuthResponse finalizeAuthenticatorResponse
er := json.Unmarshal(responseData, &finalizeAuthResponse)
if er != nil {
return false, status.NewError(STEAM_REQUEST_ERROR, fmt.Sprintf("Finalize authenticator Error %s", er.Error()))
}
if finalizeAuthResponse.Response.Status == 89 {
return false, status.NewError(STEAM_INVALID_SMS_CODE, "Finalize authenticator Error invalid sms code")
}
if finalizeAuthResponse.Response.Status == 88 {
return false, status.NewError(STEAM_INVALID_SMS_CODE, "Finalize authenticator Error unable to generate correct code")
}
if !finalizeAuthResponse.Response.Success {
return false, status.NewError(STEAM_REQUEST_FAIL, "Finalize authenticator Request failed")
}
return true, nil
}
type RemoveAuthenticatorRequest struct {
RevocationCode string
Scheme int
SteamId uint64
AccessToken string
}
type removeAuthenticatorResponse struct {
Response struct {
Success bool `json:"success"`
} `json:"response"`
}
func RemoveAuthenticator(request RemoveAuthenticatorRequest) (ok bool, err *status.Exception) {
headers := getDefaultMobileHeader()
query := map[string][]string{
"access_token": {request.AccessToken},
"steamid": {strconv.FormatUint(request.SteamId, 10)},
"steamguard_scheme": {strconv.Itoa(request.Scheme)},
"revocation_code": {request.RevocationCode},
}
_, _, _, response, _, requestError := utils.HttpWebRequest("POST", STEAM_REMOVE_AUTHENTICATOR_WEBAPI_V1, headers, query, nil, nil, false, false)
if requestError != nil {
return ok, status.NewError(STEAM_REQUEST_ERROR, fmt.Sprintf("Remove authenticator Error %s", requestError.Error()))
}
responseDataStr := string(response)
if responseDataStr == "" {
return false, status.NewError(STEAM_REQUEST_ERROR, "Remove authenticator Error")
}
var removeAuthResponse removeAuthenticatorResponse
er := json.Unmarshal(response, &removeAuthResponse)
if er != nil {
return false, status.NewError(STEAM_REQUEST_ERROR, fmt.Sprintf("Remove authenticator Error %s", er.Error()))
}
return removeAuthResponse.Response.Success, nil
}
var (
aligned = false
timeDifference int64 = 0
)
type TimeQuery struct {
Response struct {
ServerTime string `json:"server_time"`
SkewToleranceSeconds string `json:"skew_tolerance_seconds"`
LargeTimeJink string `json:"large_time_jink"`
ProbeFrequencySeconds int `json:"probe_frequency_seconds"`
AdjustedTimeProbeFrequencySeconds int `json:"adjusted_time_probe_frequency_seconds"`
HintProbeFrequencySeconds int `json:"hint_probe_frequency_seconds"`
SyncTimeout int `json:"sync_timeout"`
TryAgainSeconds int `json:"try_again_seconds"`
MaxAttempts int `json:"max_attempts"`
} `json:"response"`
}
func GetSteamTime() int64 {
if !aligned {
alignSteamTime()
}
return time.Now().Unix() + timeDifference
}
func GetCurrentSteamChunk() int64 {
steamTime := GetSteamTime()
currentSteamChunk := steamTime / 30
secondsUntilChange := steamTime - (currentSteamChunk * 30)
return 30 - secondsUntilChange
}
func alignSteamTime() {
for i := 0; i < 3; i++ {
currentTime := time.Now().Unix()
_, _, _, resp, _, err := utils.HttpWebRequest("POST", STEAM_QUERY_TIME_WEBAPI_V1, nil, nil, nil, nil, false, false)
if err != nil {
fmt.Printf("[TimeAlligner] Error while sending request to Steam API Time: %v", err.Error())
time.Sleep(time.Second * 3)
continue
}
var timeQuery TimeQuery
json.Unmarshal(resp, &timeQuery)
steamServerTimeToInt, _ := strconv.Atoi(timeQuery.Response.ServerTime)
timeDifference = int64(steamServerTimeToInt) - currentTime
aligned = true
return
}
}