Using passport-steam with sails-generate-auth

2019-07-06 01:33发布

问题:

I'm creating a SailsJS application, and I want users to log in only with Steam authentication. I used sails-generate-auth to create some boilerplate code with sails routes, but I'm having trouble plugging passport-steam into it.

https://github.com/kasperisager/sails-generate-auth

https://github.com/liamcurry/passport-steam

The reported error is:

C:\Users\Joe\testProject\node_modules\passport-steam\lib\passport-steam\strategy.js:67
            id: result.response.players[0].steamid,
                                          ^
TypeError: Cannot read property 'steamid' of undefined
    at steamapi.getPlayerSummaries.callback (C:\Users\Joe\testProject\node_modules\passport-steam\lib\passport-steam\strategy.js:67:43)
    at IncomingMessage.<anonymous> (C:\Users\Joe\testProject\node_modules\passport-steam\node_modules\steam-web\lib\steam.js:218:7)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:944:16
    at process._tickDomainCallback (node.js:486:13)

I have a feeling that this is caused by SteamWebAPI returning an empty response: {"response":{"players":[]}}, which is caused by a bogus SteamID in the request. The offending line is here in passport-steam: https://github.com/liamcurry/passport-steam/blob/master/lib/passport-steam/strategy.js#L53

Looking at identifier parameter to getUserProfile, it appears to be the entire Sails request scope. If I hardcode a good steam id into that array, I get this error:

C:\Users\Joe\testProject\api\services\passport.js:98
    return next(new Error('Neither a username nor email was available'));
           ^
TypeError: undefined is not a function
    at Authenticator.passport.connect (C:\Users\Joe\testProject\api\services\passport.js:98:12)
    at module.exports (C:\Users\Joe\testProject\api\services\protocols\openid.js:24:12)
    at steamapi.getPlayerSummaries.callback (C:\Users\Joe\testProject\node_modules\passport-steam\lib\passport-steam\strategy.js:72:11)
    at IncomingMessage.<anonymous> (C:\Users\Joe\testProject\node_modules\passport-steam\node_modules\steam-web\lib\steam.js:218:7)
    at IncomingMessage.emit (events.js:117:20)
    at _stream_readable.js:944:16
    at process._tickDomainCallback (node.js:486:13)

I think that makes sense since the steam response doesn't have a username nor email, but this is the profile: {"emails":[{}],"name":{}}

This is my passport configuration:

steam: {
    name: 'Steam',
    protocol: 'openid',
    strategy: require('passport-steam').Strategy,
    options: {
      returnURL: 'http://localhost:1337/auth/steam/callback',
      realm: 'http://localhost:1337/',
      apiKey:'STEAM-API-KEY-REMOVED'
      }
    }
  }

Not sure if there is something simple I'm missing, or I need to write a ton of custom handling. Is my configuration correct?

回答1:

This is caused by out-of-date source code in npm. Even with the latest 0.1.4 version, the code is not correct. Replacing strategy.js in passport-steam with the latest version will fix this error.

Also, in api\services\passport.js, a little custom handling in passport.connect() needs to be added. The profile does not have a username, but has an id (user steamid) and displayName. These can be used to set the user model properties, e.g.

//steam auth
if (profile.hasOwnProperty('displayName')) { 
  user.username = profile.displayName; 
}

Here is the ticket where the problem was solved: https://github.com/liamcurry/passport-steam/issues/10