-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathegs.go
More file actions
265 lines (223 loc) · 8.34 KB
/
egs.go
File metadata and controls
265 lines (223 loc) · 8.34 KB
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
package rlapi
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
const (
egsUserAgent = "UELauncher/11.0.1-14907503+++Portal+Release-Live Windows/10.0.19041.1.256.64bit"
egsClientID = "34a02cf8f4414e29b15921876da36f9a"
egsClientSecret = "daafbccc737745039dffe53d94fc76cf"
egsOAuthURL = "account-public-service-prod03.ol.epicgames.com"
eosAuthHeader = "eHl6YTc4OTFwNUQ3czlSNkdtNm1vVEhXR2xvZXJwN0I6S25oMThkdTROVmxGcyszdVErWlBwRENWdG8wV1lmNHlYUDgrT2N3VnQxbw=="
eosDeploymentID = "da32ae9c12ae40e8a112c52e1f17f3ba" // Rocket League
)
type TokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
ExpiresAt string `json:"expires_at"`
TokenType string `json:"token_type"`
ClientID string `json:"client_id"`
InternalClient bool `json:"internal_client"`
ClientService string `json:"client_service"`
AccountID string `json:"account_id"`
DisplayName string `json:"displayName"`
App string `json:"app"`
InAppID string `json:"in_app_id"`
DeviceID string `json:"device_id"`
}
type EOSTokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
IDToken string `json:"id_token"`
ExpiresIn int `json:"expires_in"`
ExpiresAt string `json:"expires_at"`
RefreshExpiresIn int `json:"refresh_expires_in"`
RefreshExpiresAt string `json:"refresh_expires_at"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
ClientID string `json:"client_id"`
ApplicationID string `json:"application_id"`
AccountID string `json:"account_id"`
SelectedAccountID string `json:"selected_account_id"`
MergedAccounts []string `json:"merged_accounts"`
ACR string `json:"acr"`
AuthTime string `json:"auth_time"`
}
// EGS provides an authentication layer for Epic Games Store -- largely adapted from https://github.com/derrod/legendary
type EGS struct {
client *http.Client
}
// NewEGS creates a new Epic Games Store client
func NewEGS() *EGS {
return &EGS{
client: &http.Client{Timeout: 30 * time.Second},
}
}
// GetAuthURL returns the EGS login URL for manual browser authentication
func (e *EGS) GetAuthURL() string {
loginURL := "https://www.epicgames.com/id/login?redirectUrl="
redirectURL := fmt.Sprintf("https://www.epicgames.com/id/api/redirect?clientId=%s&responseType=code", egsClientID)
return loginURL + url.QueryEscape(redirectURL)
}
// AuthenticateWithCode authenticates with EGS using an authorization code
func (e *EGS) AuthenticateWithCode(authCode string) (*TokenResponse, error) {
return e.requestToken(map[string]string{
"grant_type": "authorization_code",
"code": authCode,
"token_type": "eg1",
})
}
// AuthenticateWithRefreshToken authenticates with EGS using a refresh token
func (e *EGS) AuthenticateWithRefreshToken(refreshToken string) (*TokenResponse, error) {
return e.requestToken(map[string]string{
"grant_type": "refresh_token",
"refresh_token": refreshToken,
"token_type": "eg1",
})
}
func (e *EGS) requestToken(params map[string]string) (*TokenResponse, error) {
form := url.Values{}
for k, v := range params {
form.Set(k, v)
}
req, err := http.NewRequest("POST", fmt.Sprintf("https://%s/account/api/oauth/token", egsOAuthURL), strings.NewReader(form.Encode()))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", egsUserAgent)
auth := base64.StdEncoding.EncodeToString([]byte(egsClientID + ":" + egsClientSecret))
req.Header.Set("Authorization", "Basic "+auth)
resp, err := e.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
var tokenResp TokenResponse
if err := json.Unmarshal(body, &tokenResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
if resp.StatusCode != http.StatusOK {
var errorResp struct {
ErrorCode string `json:"errorCode"`
ErrorMessage string `json:"errorMessage"`
}
json.Unmarshal(body, &errorResp)
// TODO: proper error extraction
return nil, fmt.Errorf("authentication failed: %v, %s - %s", resp.StatusCode, errorResp.ErrorCode, errorResp.ErrorMessage)
}
return &tokenResp, nil
}
// GetExchangeCode converts an EGS access token into an exchange code for EOS
func (e *EGS) GetExchangeCode(accessToken string) (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/account/api/oauth/exchange", egsOAuthURL), nil)
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "bearer "+accessToken)
req.Header.Set("User-Agent", egsUserAgent)
resp, err := e.client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code %s: %s", resp.Status, string(body))
}
var tokenResp struct {
Code string `json:"code"`
}
if err := json.Unmarshal(body, &tokenResp); err != nil {
return "", fmt.Errorf("failed to parse response: %w", err)
}
return tokenResp.Code, nil
}
// ExchangeEOSToken exchanges an exchange code for an EOS authentication token
func (e *EGS) ExchangeEOSToken(exchangeCode string) (*EOSTokenResponse, error) {
return e.requestEOSToken(map[string]string{
"grant_type": "exchange_code",
"exchange_code": exchangeCode,
})
}
// ExchangeEOSTokenFromSteam exchanges a Steam session ticket for an EOS authentication token
func (e *EGS) ExchangeEOSTokenFromSteam(steamTicket string) (*EOSTokenResponse, error) {
return e.requestEOSToken(map[string]string{
"grant_type": "external_auth",
"external_auth_type": "steam_session_ticket",
"external_auth_token": steamTicket,
})
}
// RefreshEOSToken refreshes an EOS authentication token using a refresh token
func (e *EGS) RefreshEOSToken(refreshToken string) (*EOSTokenResponse, error) {
return e.requestEOSToken(map[string]string{
"grant_type": "refresh_token",
"refresh_token": refreshToken,
})
}
func (e *EGS) requestEOSToken(params map[string]string) (*EOSTokenResponse, error) {
form := url.Values{}
for k, v := range params {
form.Set(k, v)
}
form.Set("deployment_id", eosDeploymentID)
form.Set("scope", "basic_profile")
req, err := http.NewRequest("POST", "https://api.epicgames.dev/epic/oauth/v2/token", strings.NewReader(form.Encode()))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Authorization", "Basic "+eosAuthHeader)
req.Header.Set("User-Agent", egsUserAgent)
resp, err := e.client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code %s: %s", resp.Status, string(body))
}
var tokenResp EOSTokenResponse
if err := json.Unmarshal(body, &tokenResp); err != nil {
return nil, fmt.Errorf("failed to parse response: %w", err)
}
return &tokenResp, nil
}
// RevokeEOSToken revokes an EOS authentication token
func (e *EGS) RevokeEOSToken(accessToken string) error {
form := url.Values{}
form.Set("token", accessToken)
req, err := http.NewRequest("POST", "https://api.epicgames.dev/epic/oauth/v2/revoke", strings.NewReader(form.Encode()))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", egsUserAgent)
resp, err := e.client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("unexpected status code: %s", resp.Status)
}
return nil
}