From 7ea1ff24178380c00f4682a06b744c95881e6f68 Mon Sep 17 00:00:00 2001 From: Leopoldo Brines Date: Tue, 1 Aug 2023 20:32:45 -0300 Subject: [PATCH] Empty raw data in steam provider --- providers/steam/steam.go | 15 ++++++++- providers/steam/steam_test.go | 58 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/providers/steam/steam.go b/providers/steam/steam.go index 79679defe..446e1d8a8 100644 --- a/providers/steam/steam.go +++ b/providers/steam/steam.go @@ -30,6 +30,7 @@ func New(apiKey string, callbackURL string) *Provider { p := &Provider{ APIKey: apiKey, CallbackURL: callbackURL, + SummaryURL: apiUserSummaryEndpoint, providerName: "steam", } return p @@ -39,6 +40,7 @@ func New(apiKey string, callbackURL string) *Provider { type Provider struct { APIKey string CallbackURL string + SummaryURL string HTTPClient *http.Client providerName string } @@ -114,7 +116,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) { return u, fmt.Errorf("%s cannot get user information without SteamID", p.providerName) } - apiURL := fmt.Sprintf(apiUserSummaryEndpoint, p.APIKey, s.SteamID) + apiURL := fmt.Sprintf(p.SummaryURL, p.APIKey, s.SteamID) req, err := http.NewRequest("GET", apiURL, nil) if err != nil { return u, err @@ -185,6 +187,17 @@ func buildUserObject(r io.Reader, u goth.User) (goth.User, error) { u.Location = "No location is provided by the Steam API" } + // Set raw data + b, err := json.Marshal(&player) + if err != nil { + return u, err + } + + err = json.Unmarshal(b, &u.RawData) + if err != nil { + return u, err + } + return u, nil } diff --git a/providers/steam/steam_test.go b/providers/steam/steam_test.go index f800bd0a0..b16108608 100644 --- a/providers/steam/steam_test.go +++ b/providers/steam/steam_test.go @@ -1,6 +1,9 @@ package steam_test import ( + "fmt" + "net/http" + "net/http/httptest" "os" "testing" @@ -50,6 +53,61 @@ func Test_SessionFromJSON(t *testing.T) { a.Equal(s.ResponseNonce, "2016-03-13T16:56:30ZJ8tlKVquwHi9ZSPV4ElU5PY2dmI=") } +func Test_FetchUser(t *testing.T) { + apiUserSummaryPath := "/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s" + + t.Parallel() + a := assert.New(t) + p := provider() + session, err := p.UnmarshalSession(`{"AuthURL":"https://steamcommunity.com/openid/login?openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.realm=%3A%2F%2F&openid.return_to=%2Ffoo","SteamID":"1234567890","CallbackURL":"http://localhost:3030/","ResponseNonce":"2016-03-13T16:56:30ZJ8tlKVquwHi9ZSPV4ElU5PY2dmI="}`) + a.NoError(err) + + expectedPath := fmt.Sprintf(apiUserSummaryPath, p.APIKey, "1234567890") + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + a.Equal("application/json", r.Header.Get("Accept")) + a.Equal(http.MethodGet, r.Method) + a.Equal(expectedPath, r.URL.RequestURI()) + w.Write([]byte(testUserSummaryBody)) + })) + + p.SummaryURL = ts.URL + apiUserSummaryPath + + user, err := p.FetchUser(session) + a.NoError(err) + a.Equal("76561197960435530", user.UserID) + a.Equal("Robin", user.NickName) + a.Equal("Robin Walker", user.Name) + a.Equal("https://avatars.steamstatic.com/81b5478529dce13bf24b55ac42c1af7058aaf7a9_full.jpg", user.AvatarURL) + a.Equal("No email is provided by the Steam API", user.Email) + a.Equal("No description is provided by the Steam API", user.Description) + a.Equal("WA, US", user.Location) + a.Len(user.RawData, 6) + a.Equal("76561197960435530", user.RawData["steamid"]) + a.Equal("Robin", user.RawData["personaname"]) + a.Equal("Robin Walker", user.RawData["realname"]) + a.Equal("https://avatars.steamstatic.com/81b5478529dce13bf24b55ac42c1af7058aaf7a9_full.jpg", user.RawData["avatarfull"]) + a.Equal("US", user.RawData["loccountrycode"]) + a.Equal("WA", user.RawData["locstatecode"]) +} + func provider() *steam.Provider { return steam.New(os.Getenv("STEAM_KEY"), "/foo") } + +// Extracted from: https://developer.valvesoftware.com/wiki/Steam_Web_API +var testUserSummaryBody = `{ + "response": { + "players": [ + { + "steamid": "76561197960435530", + "personaname": "Robin", + "realname": "Robin Walker", + "avatarfull": "https://avatars.steamstatic.com/81b5478529dce13bf24b55ac42c1af7058aaf7a9_full.jpg", + "loccountrycode": "US", + "locstatecode": "WA" + } + ] + + } +}`