-
Notifications
You must be signed in to change notification settings - Fork 3
Accept profile URLs and resolve to Steam64 #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package steam | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "reverse-watch/domain/models" | ||
| "reverse-watch/errors" | ||
| "reverse-watch/middleware" | ||
| "reverse-watch/render" | ||
| steamservice "reverse-watch/service/steam" | ||
| ) | ||
|
|
||
| type resolveSteamIDResponse struct { | ||
| SteamID models.SteamID `json:"steam_id"` | ||
| } | ||
|
|
||
| func resolveSteamID(w http.ResponseWriter, r *http.Request) { | ||
| steamSvc, ok := r.Context().Value(middleware.SteamServiceContextKey).(*steamservice.Service) | ||
| if !ok || steamSvc == nil { | ||
| render.Errorf(w, r, errors.InternalServerError, "steam service not configured") | ||
| return | ||
| } | ||
|
|
||
| raw := strings.TrimSpace(r.URL.Query().Get("vanityUrl")) | ||
| if raw == "" { | ||
| render.Errorf(w, r, errors.BadRequest, "missing vanityUrl") | ||
| return | ||
| } | ||
|
|
||
| id, err := steamSvc.ResolveSteamID(r.Context(), raw) | ||
| if err != nil { | ||
| render.Error(w, r, err) | ||
| return | ||
| } | ||
|
|
||
| render.JSON(w, r, &resolveSteamIDResponse{SteamID: *id}) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package steam | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "reverse-watch/ratelimit" | ||
|
|
||
| "github.com/go-chi/chi/v5" | ||
| ) | ||
|
|
||
| func Router() chi.Router { | ||
| r := chi.NewRouter() | ||
| r.With(ratelimit.ThrottleByIP(time.Minute, 100)).Get("/resolve-vanity", resolveSteamID) | ||
| return r | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
|
|
||
| steamservice "reverse-watch/service/steam" | ||
| ) | ||
|
|
||
| const SteamServiceContextKey ContextKey = "steamService" | ||
|
|
||
| func SteamServiceMiddleware(svc *steamservice.Service) func(http.Handler) http.Handler { | ||
| return func(next http.Handler) http.Handler { | ||
| fn := func(w http.ResponseWriter, r *http.Request) { | ||
| ctx := context.WithValue(r.Context(), SteamServiceContextKey, svc) | ||
| next.ServeHTTP(w, r.WithContext(ctx)) | ||
| } | ||
| return http.HandlerFunc(fn) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| package steam | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
|
|
||
| "reverse-watch/domain/models" | ||
| "reverse-watch/errors" | ||
| "reverse-watch/util" | ||
| ) | ||
|
|
||
| const maxVanityLen = 64 | ||
| const maxSteamAPIBodyBytes = 64 << 10 | ||
|
|
||
| type Service struct { | ||
| client *http.Client | ||
| keys *util.Ring[string] | ||
| } | ||
|
|
||
| type Options struct { | ||
| HTTPClient *http.Client | ||
| WebAPIKeys *util.Ring[string] | ||
| } | ||
|
|
||
| func New(opts Options) *Service { | ||
| client := opts.HTTPClient | ||
| if client == nil { | ||
| client = http.DefaultClient | ||
| } | ||
|
|
||
| keys := opts.WebAPIKeys | ||
| if keys == nil { | ||
| keys = util.NewRing[string](nil) | ||
| } | ||
|
|
||
| return &Service{ | ||
| client: client, | ||
| keys: keys, | ||
| } | ||
| } | ||
|
|
||
| func (s *Service) ResolveSteamID(ctx context.Context, raw string) (*models.SteamID, error) { | ||
hayesZach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| raw = strings.TrimSpace(raw) | ||
| if raw == "" { | ||
| return nil, errors.New(errors.BadRequest, "missing vanityUrl") | ||
| } | ||
|
|
||
| // This endpoint should accept a vanity URL, not a raw numeric SteamID64. | ||
| if _, err := models.ToSteamID(raw); err == nil { | ||
| return nil, errors.New(errors.BadRequest, "expected vanityUrl, got steam id") | ||
| } | ||
|
|
||
| u, err := url.Parse(raw) | ||
| if err != nil { | ||
| return nil, errors.New(errors.BadRequest, "invalid vanityUrl", err) | ||
| } | ||
|
|
||
| // If the scheme is missing, default to https so the host/path parsing works. | ||
| if u.Scheme == "" { | ||
| u, err = url.Parse("https://" + raw) | ||
| if err != nil { | ||
| return nil, errors.New(errors.BadRequest, "invalid vanityUrl", err) | ||
| } | ||
| } | ||
|
Comment on lines
+64
to
+69
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why reparse again if the first one succeeded? |
||
|
|
||
| if !strings.EqualFold(u.Scheme, "http") && !strings.EqualFold(u.Scheme, "https") { | ||
| return nil, errors.New(errors.BadRequest, "url scheme must be http or https") | ||
| } | ||
|
Comment on lines
+71
to
+73
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just omit this check since the parsing succeeded. |
||
|
|
||
| host := strings.ToLower(strings.TrimPrefix(u.Hostname(), "www.")) | ||
| if host != "steamcommunity.com" { | ||
| return nil, errors.New(errors.BadRequest, fmt.Sprintf("expected steamcommunity.com, got %q", host)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can use |
||
| } | ||
|
|
||
| path := strings.Trim(u.Path, "/") | ||
| segments := strings.Split(path, "/") | ||
| if len(segments) < 2 || !strings.EqualFold(segments[0], "id") { | ||
| return nil, errors.New(errors.BadRequest, "expected steam vanity URL path /id/{vanity}") | ||
| } | ||
|
|
||
| vanity := segments[1] | ||
| if vanity == "" { | ||
| return nil, errors.New(errors.BadRequest, "missing vanity url") | ||
| } | ||
| if len(vanity) > maxVanityLen { | ||
| return nil, errors.New(errors.BadRequest, "vanity too long") | ||
| } | ||
| return s.resolveVanity(ctx, vanity) | ||
| } | ||
|
|
||
| func (s *Service) resolveVanity(ctx context.Context, vanity string) (*models.SteamID, error) { | ||
| key, ok := s.keys.Next() | ||
| if !ok { | ||
| return nil, errors.New(errors.BadRequest, "steam web api keys not configured") | ||
| } | ||
| return resolveVanityWebAPI(ctx, s.client, key, vanity) | ||
| } | ||
|
Comment on lines
+96
to
+102
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really necessary to have this a separate function. Unlikely it will ever be called by something else. |
||
|
|
||
| func resolveVanityWebAPI(ctx context.Context, client *http.Client, apiKey, vanity string) (*models.SteamID, error) { | ||
| apiKey = strings.TrimSpace(apiKey) | ||
| if apiKey == "" { | ||
| return nil, errors.New(errors.BadRequest, "empty steam web api key") | ||
| } | ||
| vanity = strings.TrimSpace(vanity) | ||
| if vanity == "" { | ||
| return nil, errors.New(errors.BadRequest, "empty vanity") | ||
| } | ||
|
|
||
| q := url.Values{} | ||
| q.Set("key", apiKey) | ||
| q.Set("vanityurl", vanity) | ||
| q.Set("url_type", "1") | ||
| reqURL := "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v1/?" + q.Encode() | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil) | ||
| if err != nil { | ||
| return nil, errors.New(errors.InternalServerError, "failed to create request", err) | ||
| } | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return nil, errors.New(errors.InternalServerError, "steam web api request failed", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, errors.New(errors.BadRequest, fmt.Sprintf("steam api returned status %d", resp.StatusCode)) | ||
| } | ||
hayesZach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Decode directly from the response body (still bounded to avoid large reads). | ||
| var envelope struct { | ||
| Response struct { | ||
| Success int `json:"success"` | ||
| SteamID string `json:"steamid"` | ||
| Message string `json:"message"` | ||
| } `json:"response"` | ||
| } | ||
|
|
||
| if err := json.NewDecoder(io.LimitReader(resp.Body, maxSteamAPIBodyBytes)).Decode(&envelope); err != nil { | ||
| return nil, errors.New(errors.JSONDecode, "failed to decode steam api json", err) | ||
| } | ||
|
|
||
| if envelope.Response.Success != 1 { | ||
| msg := strings.TrimSpace(envelope.Response.Message) | ||
| if msg == "" { | ||
| return nil, errors.New(errors.BadRequest, fmt.Sprintf("steam api could not resolve vanity (success=%d)", envelope.Response.Success)) | ||
| } | ||
| return nil, errors.New(errors.BadRequest, "steam api: "+msg) | ||
| } | ||
| if envelope.Response.SteamID == "" { | ||
| return nil, errors.New(errors.BadRequest, "steam api returned empty steamid") | ||
| } | ||
|
|
||
| id, err := models.ToSteamID(envelope.Response.SteamID) | ||
| if err != nil { | ||
| return nil, errors.New(errors.BadRequest, "steam api returned invalid steamid", err) | ||
| } | ||
| return id, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.