Steam OpenID 签名验证

tro*_*lic 3 openid authentication cryptography node.js steam

这个问题我已经有一段时间了。我正在尝试添加一个通过 Steam 登录的按钮,该按钮在登录时不仅会检索用户 ID,还会验证签名。Steam 使用 OpenID 2.0。

我已按照此处的文档进行操作。我仔细地遵循了这些步骤,花了我一天中的大部分时间来试图解决这个问题。我的代码是这样的:

let s = data['openid.signed'].split(',');
let x = Buffer.from(s.map(x => `${x}:${data['openid.' + x]}`).join('\n') + '\n', 'utf8');
let c = crypto.createHash('sha1').update(x).digest('base64');
console.log(x.toString('utf8')); // This is the key:value string
console.log(c); // This is the final result; the generated signature
Run Code Online (Sandbox Code Playgroud)

其中 data 是 OpenID 提供者给出的响应。日志记录x(键:值对字符串)给出了预期的输出:

signed:signed,op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle
op_endpoint:https://steamcommunity.com/openid/login
claimed_id:https://steamcommunity.com/openid/id/765611981[1234567]
identity:https://steamcommunity.com/openid/id/765611981[1234567]
return_to:http://127.0.0.1:8000/resolve
response_nonce:2018-12-01T17:53: [some_hash]=
assoc_handle:1234567890
Run Code Online (Sandbox Code Playgroud)

但是,我生成的哈希c与给定的签名不匹配openid.sig。请注意,我\n在上述 key:value 对字符串的末尾使用了 a ,因为这是我解释文档的方式。

笔记。我需要身份验证的原因是我想将 Steam 帐户连接到我网站上的一个帐户,并且通过 Steam 登录可以让您完全访问我网站上的帐户,这意味着用户不能简单地输入另一个用户 ID 并访问他们的帐户(重放攻击)。因此,我需要以某种方式验证签名。

我以前从未使用过 OpenID,所以请原谅我的任何愚蠢错误。我强烈建议阅读上面链接的文档,以便您可以验证我所做的是否正确。

亲切的问候,

Mat*_*kan 10

初始请求

将您的 Steam 登录按钮链接到

https://steamcommunity.com/openid/login?openid.ns=http://specs.openid.net/auth/2.0&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select&openid.identity=http://specs.openid.net/auth/2.0/identifier_select&openid.return_to=https://mywebsite.com&openid.realm=https://mywebsite.com&openid.mode=checkid_setup

并替换openid.return_toopenid.realm查询字符串参数。

openid.return_to:这是 Steam 在成功登录并附加查询字符串参数后将重定向到的 URL。

openid.realm Steam 将要求用户信任的 URL。当用户在 Steam 登录页面上时,它将显示为这样的消息:使用您的 Steam 帐户登录 {openid.realm}。请注意,{openid.realm} 不隶属于 Steam 或 Valve

处理响应

成功登录后,Steam 将重定向到类似的 URL

https://mywebsite.com/?openid.ns=http://specs.openid.net/auth/2.0&openid.mode=id_res&openid.op_endpoint=https://steamcommunity.com/openid/login&openid.claimed_id=https://steamcommunity.com/openid/id/76561198002516729&openid.identity=https://steamcommunity.com/openid/id/76561198002516729&openid.return_to=https:/%mywebsite.com&openid.response_nonce=2020-08-27T04:44:16Zs4DPZce8qc+iPCe8JgQKB0BiIDI=&openid.assoc_handle=1234567890&openid.signed=signed,op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle&openid.sig=W0u5DRbtHE1GG0ZKXjerUZDUGmc=

要验证用户,请从后端调用以https://steamcommunity.com/openid/login 复制该响应中的每个查询字符串参数,但有一个例外:替换&openid.mode=id_res&openid.mode=check_authentication. 所以最终调用将是这个 URL:

https://steamcommunity.com/openid/login?openid.ns=http://specs.openid.net/auth/2.0&openid.mode=check_authentication&openid.op_endpoint=https://steamcommunity.com/openid/login&openid.claimed_id=https://steamcommunity.com/openid/id/76561198002516729&openid.identity=https://steamcommunity.com/openid/id/76561198002516729&openid.return_to=https://mywebsite.com&openid.response_nonce=2020-08-28T04:44:16Zs4DPZce8qc+iPCe8JgQKB0BiIDI=&openid.assoc_handle=1234567890&openid.signed=signed,op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle&openid.sig=W0u5DRbtHE1GG0ZKXjerUZDUGmc=

Steam 将返回如下text/plain响应:

ns:http://specs.openid.net/auth/2.0
is_valid:true
Run Code Online (Sandbox Code Playgroud)

如果true用户有效,则false无效。请注意,此调用只会返回true 一次,并且具有相同参数的后续调用将始终返回 false。从这里,您可以决定如何维护正在登录的用户(例如创建唯一的 cookie)并将重定向响应返回到您网站的主页、点击 Steam 登录按钮之前的最后一页或用户详细信息页面等...