使用带有sails-generate-auth的passport-steam

时间:2015-01-06 20:02:14

标签: javascript node.js sails.js passport.js

我正在创建一个SailsJS应用程序,我希望用户只能使用Steam身份验证登录。我使用sails-generate-auth创建了一些带有sails路由的样板代码,但是我无法将passport-steam插入其中。

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

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

报告的错误是:

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)

我有一种感觉,这是由SteamWebAPI返回空响应引起的:{"response":{"players":[]}},这是由请求中的伪SteamID引起的。违规行在这里是护照 - 蒸汽:https://github.com/liamcurry/passport-steam/blob/master/lib/passport-steam/strategy.js#L53

查看identifier getUserProfile参数,它似乎是整个Sails请求范围。如果我将一个好的steam id硬编码到该数组中,我会收到此错误:

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)

我认为这是有道理的,因为蒸汽响应没有用户名或电子邮件,但这是个人资料:{"emails":[{}],"name":{}}

这是我的护照配置:

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'
      }
    }
  }

不确定是否有一些简单的我缺少,或者我需要编写大量的自定义处理。我的配置是否正确?

1 个答案:

答案 0 :(得分:1)

这是由npm中过时的源代码引起的。即使使用最新的0.1.4版本,代码也不正确。使用latest version替换passport-steam中的strategy.js将解决此错误。

此外,在api\services\passport.js中,需要在passport.connect()中添加一些自定义处理。 profile没有username,但有id(用户加油)和displayName。这些可用于设置用户模型属性,例如

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

以下是问题解决的故障单:https://github.com/liamcurry/passport-steam/issues/10