Skip to content

Commit

Permalink
Empty raw data in steam provider
Browse files Browse the repository at this point in the history
  • Loading branch information
leobrines committed Aug 2, 2023
1 parent 4929279 commit 7ea1ff2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
15 changes: 14 additions & 1 deletion providers/steam/steam.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func New(apiKey string, callbackURL string) *Provider {
p := &Provider{
APIKey: apiKey,
CallbackURL: callbackURL,
SummaryURL: apiUserSummaryEndpoint,
providerName: "steam",
}
return p
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
58 changes: 58 additions & 0 deletions providers/steam/steam_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package steam_test

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"

Expand Down Expand Up @@ -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"
}
]
}
}`

0 comments on commit 7ea1ff2

Please sign in to comment.