gpt4 book ai didi

OpenID - 生成签名

转载 作者:行者123 更新时间:2023-12-01 15:22:48 26 4
gpt4 key购买 nike

我一直在编写我自己的 Open ID RP 实现(是的,我知道已经构建了很多,我这样做是为了“好玩”)。一切正常,直到我进入验证步骤并计算哈希并将其与我在肯定断言中从 OP 获得的信号进行比较。

我已经上下阅读了规范,但有几件事我不太清楚:

  1. 我是只在 openid 命名空间中包含键值对,还是在 openid.signed 中包含列表中的所有内容? 6.1听起来我应该只使用 openid。键,即使我在 openid.signed (ax) 中还有一些其他东西。

  2. 最后一个键值对是否应该跟一个换行符?

  3. 我假设这些值应该是 url 编码的(因为值中没有冒号)。如果是这样,我还假设十六进制值(例如 %3D)应该是大写的。我在 OAuth 1.0 实现中遇到了这个问题,因为 .NET 的内置 URL 编码使用小写十六进制字母。

我相当确定编码和算法没问题,但我的基本字符串已关闭。这是一个我无法开始工作的示例的完全未更改的版本:

Querystring 我从 OP 那里得到肯定的断言: openid.ns=http://specs.openid.net/auth/2.0&openid.mode=id_res&openid.op_endpoint=https://www.google.com/accounts/o8/ud&openid.response_nonce=2011-05- 13T08:18:42ZBHyiLFGyNT-SqQ&openid.return_to=http://mysite.com/Account/Login.aspx&openid.assoc_handle=AOQobUc4P9MWC3faGcMkfTb2U10KfGQ-6cm9L4pLDQmeoY2DE6XRGtN0&openid.signed=op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle,ns.ext1,ext1.模式,ext1.type.firstname,ext1.value.firstname,ext1.type.email,ext1.value.email,ext1.type.lastname,ext1.value.lastname&openid.sig=KSXw+bv7sLlQyUIflA3Jzx5VoPk=&openid.identity=https://www.google.com/accounts/o8/id?id=AItOawkDYxJln6LwTAdl0kP8xdMT71SoRufUFA4&openid.claimed_id=https://www.google.com/accounts/o8/id?id=AItOawkDYxJln6LwTAdl0kP8xdMT71SoRufUFA4&openid.https.openid.1=://www.google.com/accounts/o8/id?id=AItOawkDYxJln6LwTAdl0kP8xdMT71SoRufUFA4&openid.ns.openid.1=://www.google.com/accounts/o8/id? net/srv/ax/1.0&openid.ext1.mode=fetch_response&openid.ext1.type.firstname=http://axschema.org/namePerson/first&openid.ext1.value.firstname=firstname&openid.ext1.type.email=http://schema.openid.net/contact/em ail&openid.ext1.value.email=testingopenid5132011@gmail.com&openid.ext1.type.lastname=http://axschema.org/namePerson/last&openid.ext1.value.lastname=lastname

我使用该查询字符串构建的基本字符串: op_endpoint:https://www.google.com/accounts/o8/ud\nclaimed_id:https://www.google.com/accounts/o8/id?id=AItOawkDYxJln6LwTAdl0kP8xdMT71SoRufUFA4\nidentity:https://www .google.com/accounts/o8/id?id=AItOawkDYxJln6LwTAdl0kP8xdMT71SoRufUFA4\nreturn_to:http://mysite.com/Account/Login.aspx\nresponse_nonce:2011-05-13T08:18:42ZBHyiLFGyNT-SqQ\nassoc_handle:AOQobUc4P9MWC3faGcMkfTb2U10KfGQ-6cm9L4pLDQmeoY2DE6XRGtN0\nns.ext1:http://openid.net/srv/ax/1.0\next1.mode:fetch_response\next1.type.firstname:http://axschema.org/namePerson/first\next1.value.firstname:firstname\next1.type.email:http://schema.openid.net/contact/email\next1.value.email:testingopenid5132011@gmail.com\next1.type.lastname:http://axschema.org/namePerson/last\next1.value.lastname:姓氏\n

关联请求返回的 mac key : U/1wUBAU2aYIR+2eIsugXyEOpmE=

将所有这些与 HMAC-SHA1 结合使用,我得到的哈希是: 9HMRL4je44Oz90s1f8pw5qpZ8HQ=

但是从openid.sig可以看出,应该是 KSXw+bv7sLlQyUIflA3Jzx5VoPk=

我是否错误地制定了基本字符串?我计算的哈希值错了吗?为什么这么“简单”的事情要花这么长时间才能正确实现?

最佳答案

我在生成匹配签名时也遇到了问题,最终找到了解决方案。

  1. 正如您已经怀疑的那样,您必须从 openid.ax 命名空间添加值,应用添加键/值对而不使用 openid 的相同规则。前缀。如果没有 openid.ax key ,则有问题。

  2. 是的,最后一个键/值对后跟一个换行符(注意:只有一个\n)。这本可以在 OpenID 规范中更清楚地提及。

  3. 您对 URL 编码的看法是错误的,恰恰相反:值必须经过 URL 解码。这在规范中也没有明确说明。不要混淆冒号和分号,你不能有冒号,但只能在关键部分,所以这个没有问题。

因此,如果您尝试使用此字符串并添加缺少的键/值对,它应该可以工作:

ns:http://specs.openid.net/auth/2.0op_endpoint:https://www.google.com/accounts/o8/udclaimed_id:https://www.google.com/accounts/o8/id?id=AItOawlvj7acGYj-NH1kKKl3RswJlLCKpl9LIwkidentity:https://www.google.com/accounts/o8/id?id=AItOawlvj7acGYj-NH1kKKl3RswJlLCKpl9LIwkreturn_to:http://mysite.com/Account/Login.aspxresponse_nonce:2011-05-12T03:56:09ZoeDC9WFOgOBaAQassoc_handle:AOQobUdHugprvbsK2-8NCtS2uBomRDGJQGOKDmqEwxco8Rny47rdZlBpns.ext1:http://openid.net/srv/ax/1.0ext1.mode:fetch_responseext1.type.firstname:http://axschema.org/namePerson/firstext1.value.firstname:Firstext1.type.email:http://schema.openid.net/contact/emailext1.value.email:myemail@gmail.comext1.type.lastname:http://axschema.org/namePerson/lastext1.value.lastname:Name

This little console application re-generates the signature (using HMAC-SHA256), it needs two parameters:

  • the complete redirect URL after the successful OpenID authentication (containing the positive assertion keys), can be copied from the web browser's address bar
  • the Base64-encoded MAC key, as returned in the prior association response

Code:

using System;

public class OpenIdSignatureVerification {

public static void Main(string[] args) {
if (args.Length != 2) {
Console.Error.WriteLine("Usage: assertion_url mac_key");
Environment.Exit(1);
}

string url = args[0];
int pos = url.IndexOf('?');
if (pos == -1) {
Console.Error.WriteLine("No query string found");
Environment.Exit(1);
}
url = url.Substring(pos + 1);
Console.WriteLine(String.Format("Query string: {0}", url));

System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>();

foreach (string part in url.Split('&')) {
string[] keyValue = part.Split('=');
if (keyValue.Length != 2) continue;
dict[keyValue[0]] = System.Web.HttpUtility.UrlDecode(keyValue[1]);
}

string hashInput = String.Empty;
string[] signed = dict["openid.signed"].Replace("%2C", ",").Split(',');
foreach (string key in signed) hashInput += key + ":" + dict["openid." + key] + "\n";

string macKey = args[1];

Console.WriteLine(String.Format("Hash input: {0}\n", hashInput));
Console.WriteLine(String.Format("MAC Key: {0}", macKey));

byte[] encodedHashInput = System.Text.Encoding.UTF8.GetBytes(hashInput);

System.Security.Cryptography.HMACSHA256 signer = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(macKey));

string hashOutput = Convert.ToBase64String(signer.ComputeHash(encodedHashInput));

Console.WriteLine(String.Format("Signature hash (expected) : {0}", dict["openid.sig"]));
Console.WriteLine(String.Format("Signature hash (calculated): {0}", hashOutput));
}

}

关于OpenID - 生成签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5975514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com