This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
forked from faceit/go-steam
/
auth.go
278 lines (224 loc) · 7.81 KB
/
auth.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
package steam
import (
"errors"
"time"
pb "github.com/13k/go-steam-resources/protobuf/steam"
"github.com/13k/go-steam-resources/steamlang"
"github.com/13k/go-steam/cryptoutil"
"github.com/13k/go-steam/protocol"
"github.com/13k/go-steam/steamid"
"google.golang.org/protobuf/proto"
)
type SentryHash []byte
type LogOnDetails struct {
Username string
// Can be omitted if using a LoginKey.
Password string
// Previously saved login key.
LoginKey string
// Steam Guard email code.
AuthCode string
// Steam Guard two-factor authentication code.
TwoFactorCode string
// Tells Steam to generate a login key to be used on subsequent logins without a password.
// A `LoginKeyEvent` event will be emitted with the LoginKey to be saved.
ShouldRememberPassword bool
// Previously saved machine identification hash.
// It can be saved when the event `MachineAuthUpdateEvent` is emitted.
SentryFileHash SentryHash
// Previously saved CellID from a `LoggedOnEvent` event.
CellID uint32
// LoginID uniquely identifies a logon session (required if establishing more than one active
// session to the given account).
LoginID uint32
}
type Auth struct {
client *Client
}
var _ protocol.PacketHandler = (*Auth)(nil)
func NewAuth(client *Client) *Auth {
return &Auth{client: client}
}
// LogOn logs on with the given details.
//
// You must always specify username and password OR username and loginkey. For the first login,
// don't set an authcode or a hash and you'll receive an error (EResult_AccountLogonDenied) and
// Steam will send you an authcode. Then you have to login again, this time with the authcode.
//
// Shortly after logging in, you'll receive a `MachineAuthUpdateEvent` with a hash which allows you
// to login without using an authcode in the future.
//
// If you don't use Steam Guard, username and password are enough.
//
// After the event EMsg_ClientNewLoginKey is received you can use the LoginKey to login instead of
// using the password.
func (a *Auth) LogOn(details *LogOnDetails) error {
if details.Username == "" {
return errors.New("steam/auth: username must be set")
}
if details.Password == "" && details.LoginKey == "" {
return errors.New("steam/auth: Password or LoginKey must be set")
}
machineID, err := NewMachineID()
if err != nil {
return err
}
machineIDAuth, err := machineID.Auth()
if err != nil {
return err
}
steamID := steamid.New(
steamlang.EAccountType_Individual,
steamlang.EUniverse_Public,
0,
steamid.DesktopInstance,
)
logon := &pb.CMsgClientLogon{
ProtocolVersion: proto.Uint32(steamlang.MsgClientLogon_CurrentProtocol),
AccountName: proto.String(details.Username),
Password: proto.String(details.Password),
ShouldRememberPassword: proto.Bool(details.ShouldRememberPassword),
ClientLanguage: proto.String("english"),
ShaSentryfile: details.SentryFileHash,
EresultSentryfile: proto.Int32(int32(steamlang.EResult_FileNotFound)),
SupportsRateLimitResponse: proto.Bool(true),
ChatMode: proto.Uint32(2),
MachineId: machineIDAuth,
}
if details.AuthCode != "" {
logon.AuthCode = proto.String(details.AuthCode)
}
if details.TwoFactorCode != "" {
logon.TwoFactorCode = proto.String(details.TwoFactorCode)
}
if details.LoginKey != "" {
logon.LoginKey = proto.String(details.LoginKey)
}
if details.SentryFileHash != nil {
logon.EresultSentryfile = proto.Int32(int32(steamlang.EResult_OK))
}
if details.CellID != 0 {
logon.CellId = proto.Uint32(details.CellID)
}
if details.LoginID != 0 {
logon.ObfuscatedPrivateIp = &pb.CMsgIPAddress{
Ip: &pb.CMsgIPAddress_V4{V4: details.LoginID},
}
}
msg := protocol.NewProtoMessage(steamlang.EMsg_ClientLogon, logon)
msg.SetSessionID(0)
msg.SetSteamID(steamID)
a.client.setSteamID(steamID)
a.client.Write(msg)
return nil
}
func (a *Auth) HandlePacket(packet *protocol.Packet) {
switch packet.EMsg() {
case steamlang.EMsg_ClientLogOnResponse:
a.handleLogOnResponse(packet)
case steamlang.EMsg_ClientNewLoginKey:
a.handleLoginKey(packet)
case steamlang.EMsg_ClientLoggedOff:
a.handleLoggedOff(packet)
case steamlang.EMsg_ClientUpdateMachineAuth:
a.handleUpdateMachineAuth(packet)
case steamlang.EMsg_ClientAccountInfo:
a.handleAccountInfo(packet)
}
}
func (a *Auth) handleLogOnResponse(packet *protocol.Packet) {
if !packet.IsProto() {
a.client.Fatalf("auth/LogOnResponse: received non-proto message")
return
}
body := &pb.CMsgClientLogonResponse{}
msg, err := packet.ReadProtoMsg(body)
if err != nil {
a.client.Fatalf("auth/LogOnResponse: error reading message: %v", err)
return
}
switch result := steamlang.EResult(body.GetEresult()); result {
case steamlang.EResult_OK:
a.client.setSessionID(msg.Header.Proto.GetClientSessionid())
a.client.setSteamID(steamid.SteamID(msg.Header.Proto.GetSteamid()))
a.client.Web.webLoginKey = *body.WebapiAuthenticateUserNonce
go a.client.heartbeatLoop(time.Duration(body.GetOutOfGameHeartbeatSeconds()))
a.client.Emit(&LoggedOnEvent{
Result: result,
ExtendedResult: steamlang.EResult(body.GetEresultExtended()),
AccountFlags: steamlang.EAccountFlags(body.GetAccountFlags()),
ClientSteamID: steamid.SteamID(body.GetClientSuppliedSteamid()),
Body: body,
})
case steamlang.EResult_Fail, steamlang.EResult_ServiceUnavailable, steamlang.EResult_TryAnotherCM:
// some error on Steam's side, we'll get an EOF later
a.client.Emit(&FailureEvent{Result: result})
default:
a.client.Emit(&LogOnFailedEvent{Result: result})
a.client.Disconnect()
}
}
func (a *Auth) handleLoginKey(packet *protocol.Packet) {
body := &pb.CMsgClientNewLoginKey{}
if _, err := packet.ReadProtoMsg(body); err != nil {
a.client.Errorf("auth/LoginKey: error reading message: %v", err)
return
}
pbAccepted := &pb.CMsgClientNewLoginKeyAccepted{
UniqueId: proto.Uint32(body.GetUniqueId()),
}
a.client.Write(protocol.NewProtoMessage(steamlang.EMsg_ClientNewLoginKeyAccepted, pbAccepted))
a.client.Emit(&LoginKeyEvent{
UniqueID: body.GetUniqueId(),
LoginKey: body.GetLoginKey(),
})
}
func (a *Auth) handleLoggedOff(packet *protocol.Packet) {
var result steamlang.EResult
if packet.IsProto() {
body := &pb.CMsgClientLoggedOff{}
if _, err := packet.ReadProtoMsg(body); err != nil {
a.client.Errorf("auth/LoggedOff: error reading message: %v", err)
return
}
result = steamlang.EResult(body.GetEresult())
} else {
body := &steamlang.MsgClientLoggedOff{}
if _, err := packet.ReadClientMsg(body); err != nil {
a.client.Errorf("auth/LoggedOff: error reading message: %v", err)
return
}
result = body.Result
}
a.client.Emit(&LoggedOffEvent{Result: result})
}
func (a *Auth) handleUpdateMachineAuth(packet *protocol.Packet) {
body := &pb.CMsgClientUpdateMachineAuth{}
if _, err := packet.ReadProtoMsg(body); err != nil {
a.client.Errorf("auth/UpdateMachineAuth: error reading message: %v", err)
return
}
sha1sum := cryptoutil.SHA1Sum(packet.Data)
pbAuthRes := &pb.CMsgClientUpdateMachineAuthResponse{
ShaFile: sha1sum,
}
msg := protocol.NewProtoMessage(steamlang.EMsg_ClientUpdateMachineAuthResponse, pbAuthRes)
msg.SetTargetJobID(packet.SourceJobID())
a.client.Write(msg)
a.client.Emit(&MachineAuthUpdateEvent{Hash: sha1sum})
}
func (a *Auth) handleAccountInfo(packet *protocol.Packet) {
body := &pb.CMsgClientAccountInfo{}
if _, err := packet.ReadProtoMsg(body); err != nil {
a.client.Errorf("auth/AccountInfo: error reading message: %v", err)
return
}
a.client.Emit(&AccountInfoEvent{
PersonaName: body.GetPersonaName(),
Country: body.GetIpCountry(),
CountAuthedComputers: body.GetCountAuthedComputers(),
AccountFlags: steamlang.EAccountFlags(body.GetAccountFlags()),
FacebookID: body.GetFacebookId(),
FacebookName: body.GetFacebookName(),
})
}