-
Notifications
You must be signed in to change notification settings - Fork 132
/
auth.go
200 lines (175 loc) · 6.93 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
package steam
import (
"crypto/sha1"
"sync/atomic"
"time"
"github.com/Philipp15b/go-steam/v3/protocol"
"github.com/Philipp15b/go-steam/v3/protocol/protobuf"
"github.com/Philipp15b/go-steam/v3/protocol/steamlang"
"github.com/Philipp15b/go-steam/v3/steamid"
"google.golang.org/protobuf/proto"
)
type Auth struct {
client *Client
details *LogOnDetails
}
type SentryHash []byte
type LogOnDetails struct {
Username string
// If logging into an account without a login key, the account's password.
Password string
// If you have a Steam Guard email code, you can provide it here.
AuthCode string
// If you have a Steam Guard mobile two-factor authentication code, you can provide it here.
TwoFactorCode string
SentryFileHash SentryHash
LoginKey string
// true if you want to get a login key which can be used in lieu of
// a password for subsequent logins. false or omitted otherwise.
ShouldRememberPassword bool
}
// Log 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) {
if details.Username == "" {
panic("Username must be set!")
}
if details.Password == "" && details.LoginKey == "" {
panic("Password or LoginKey must be set!")
}
logon := new(protobuf.CMsgClientLogon)
logon.AccountName = &details.Username
logon.Password = &details.Password
if details.AuthCode != "" {
logon.AuthCode = proto.String(details.AuthCode)
}
if details.TwoFactorCode != "" {
logon.TwoFactorCode = proto.String(details.TwoFactorCode)
}
logon.ClientLanguage = proto.String("english")
logon.ProtocolVersion = proto.Uint32(steamlang.MsgClientLogon_CurrentProtocol)
logon.ShaSentryfile = details.SentryFileHash
if details.LoginKey != "" {
logon.LoginKey = proto.String(details.LoginKey)
}
if details.ShouldRememberPassword {
logon.ShouldRememberPassword = proto.Bool(details.ShouldRememberPassword)
}
atomic.StoreUint64(&a.client.steamId, uint64(steamid.NewIdAdv(0, 1, int32(steamlang.EUniverse_Public), int32(steamlang.EAccountType_Individual))))
a.client.Write(protocol.NewClientMsgProtobuf(steamlang.EMsg_ClientLogon, logon))
}
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_ClientSessionToken:
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("Got non-proto logon response!")
return
}
body := new(protobuf.CMsgClientLogonResponse)
msg := packet.ReadProtoMsg(body)
result := steamlang.EResult(body.GetEresult())
if result == steamlang.EResult_OK {
atomic.StoreInt32(&a.client.sessionId, msg.Header.Proto.GetClientSessionid())
atomic.StoreUint64(&a.client.steamId, msg.Header.Proto.GetSteamid())
a.client.Web.webLoginKey = *body.WebapiAuthenticateUserNonce
go a.client.heartbeatLoop(time.Duration(body.GetOutOfGameHeartbeatSeconds()))
a.client.Emit(&LoggedOnEvent{
Result: steamlang.EResult(body.GetEresult()),
ExtendedResult: steamlang.EResult(body.GetEresultExtended()),
OutOfGameSecsPerHeartbeat: body.GetOutOfGameHeartbeatSeconds(),
InGameSecsPerHeartbeat: body.GetInGameHeartbeatSeconds(),
PublicIp: body.GetDeprecatedPublicIp(),
ServerTime: body.GetRtime32ServerTime(),
AccountFlags: steamlang.EAccountFlags(body.GetAccountFlags()),
ClientSteamId: steamid.SteamId(body.GetClientSuppliedSteamid()),
EmailDomain: body.GetEmailDomain(),
CellId: body.GetCellId(),
CellIdPingThreshold: body.GetCellIdPingThreshold(),
Steam2Ticket: body.GetSteam2Ticket(),
UsePics: body.GetDeprecatedUsePics(),
WebApiUserNonce: body.GetWebapiAuthenticateUserNonce(),
IpCountryCode: body.GetIpCountryCode(),
VanityUrl: body.GetVanityUrl(),
NumLoginFailuresToMigrate: body.GetCountLoginfailuresToMigrate(),
NumDisconnectsToMigrate: body.GetCountDisconnectsToMigrate(),
})
} else if result == steamlang.EResult_Fail || result == steamlang.EResult_ServiceUnavailable || result == steamlang.EResult_TryAnotherCM {
// some error on Steam's side, we'll get an EOF later
} else {
a.client.Emit(&LogOnFailedEvent{
Result: steamlang.EResult(body.GetEresult()),
})
a.client.Disconnect()
}
}
func (a *Auth) handleLoginKey(packet *protocol.Packet) {
body := new(protobuf.CMsgClientNewLoginKey)
packet.ReadProtoMsg(body)
a.client.Write(protocol.NewClientMsgProtobuf(steamlang.EMsg_ClientNewLoginKeyAccepted, &protobuf.CMsgClientNewLoginKeyAccepted{
UniqueId: proto.Uint32(body.GetUniqueId()),
}))
a.client.Emit(&LoginKeyEvent{
UniqueId: body.GetUniqueId(),
LoginKey: body.GetLoginKey(),
})
}
func (a *Auth) handleLoggedOff(packet *protocol.Packet) {
result := steamlang.EResult_Invalid
if packet.IsProto {
body := new(protobuf.CMsgClientLoggedOff)
packet.ReadProtoMsg(body)
result = steamlang.EResult(body.GetEresult())
} else {
body := new(steamlang.MsgClientLoggedOff)
packet.ReadClientMsg(body)
result = body.Result
}
a.client.Emit(&LoggedOffEvent{Result: result})
}
func (a *Auth) handleUpdateMachineAuth(packet *protocol.Packet) {
body := new(protobuf.CMsgClientUpdateMachineAuth)
packet.ReadProtoMsg(body)
hash := sha1.New()
hash.Write(packet.Data)
sha := hash.Sum(nil)
msg := protocol.NewClientMsgProtobuf(steamlang.EMsg_ClientUpdateMachineAuthResponse, &protobuf.CMsgClientUpdateMachineAuthResponse{
ShaFile: sha,
})
msg.SetTargetJobId(packet.SourceJobId)
a.client.Write(msg)
a.client.Emit(&MachineAuthUpdateEvent{sha})
}
func (a *Auth) handleAccountInfo(packet *protocol.Packet) {
body := new(protobuf.CMsgClientAccountInfo)
packet.ReadProtoMsg(body)
a.client.Emit(&AccountInfoEvent{
PersonaName: body.GetPersonaName(),
Country: body.GetIpCountry(),
CountAuthedComputers: body.GetCountAuthedComputers(),
AccountFlags: steamlang.EAccountFlags(body.GetAccountFlags()),
FacebookId: body.GetFacebookId(),
FacebookName: body.GetFacebookName(),
})
}