GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — rlapi/egs.go at master · dank/rlapi Expand file tree/ Copy pathegs.goMore file actions 326 lines (274 loc) · 10.4 KB / Copy pathegs.goFile metadata and controls326 lines (274 loc) · 10.4 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 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" eosDeploymentID = "da32ae9c12ae40e8a112c52e1f17f3ba" // Rocket League eosClientID = "xyza7891p5D7s9R6Gm6moTHWGloerp7B" eosSecret = "Knh18du4NVlFs+3uQ+ZPpDCVto0WYf4yXP8+OcwVt1o" ) 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"` } type DeviceAuthResponse struct { UserCode string `json:"user_code"` DeviceCode string `json:"device_code"` VerificationURI string `json:"verification_uri"` ExpiresIn int `json:"expires_in"` Interval int `json:"interval"` } // 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 "+base64.StdEncoding.EncodeToString([]byte(eosClientID+":"+eosSecret))) 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 } // AuthenticateWithDevice initiates the OAuth 2.0 Device Authorization Grant ([RFC 8628]). // Display VerificationURI and UserCode to the user, then call [EGS.WaitForDeviceAuthorization] to exchange for an EOS token. // // [RFC 8628]: https://datatracker.ietf.org/doc/html/rfc8628 func (e *EGS) AuthenticateWithDevice() (*DeviceAuthResponse, error) { req, err := http.NewRequest("POST", "https://api.epicgames.dev/epic/oauth/v2/deviceAuthorization", strings.NewReader("client_id="+eosClientID)) 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) 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 deviceAuthResp DeviceAuthResponse if err := json.Unmarshal(body, &deviceAuthResp); err != nil { return nil, fmt.Errorf("failed to parse response: %w", err) } return &deviceAuthResp, nil } // WaitForDeviceAuthorization polls EOS until the user completes authorization at VerificationURI, then returns an EOS token. func (e *EGS) WaitForDeviceAuthorization(device *DeviceAuthResponse) (*EOSTokenResponse, error) { for range device.ExpiresIn / device.Interval { token, err := e.requestEOSToken(map[string]string{ "grant_type": "device_code", "device_code": device.DeviceCode, }) if err == nil { return token, nil } time.Sleep(time.Duration(device.Interval) * time.Second) } return nil, fmt.Errorf("device authorization timed out") } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |