最近分配给我的一个学校项目有一个编码挑战,我们必须完成。挑战包括多个部分,最后一部分是上传到私有GitHub存储库,并在一定条件下通过发出POST请求来提交完成请求。
我已经成功完成了挑战的其他部分,并且坚持提交请求。提交内容必须遵循以下规则:
建立您的解决方案请求
首先,构造一个如下所示的JSON字符串:
{
"github_url": "https://github.com/YOUR_ACCOUNT/GITHUB_REPOSITORY",
"contact_email": "YOUR_EMAIL"
}
在YOUR_ACCOUNT / GITHUB_REPOSITORY中填写您的电子邮件地址以获取YOUR_EMAIL和私有的Github存储库。然后,以JSON字符串作为主体部分,对以下URL发出HTTP POST请求。
CHALLENGE_URL
内容类型
请求的Content-Type:必须为application / json。
授权
URL受HTTP基本身份验证保护,这在RFC2617的第2章中进行了说明,因此您必须在POST请求中提供一个Authorization:标头字段。
对于HTTP基本认证的用户ID,请使用您在JSON字符串中输入的相同电子邮件地址。 对于密码,请提供一个符合RFC6238 TOTP的10位基于时间的一次性密码。 授权密码
要生成TOTP密码,您将需要使用以下设置:
您必须根据RFC6238生成正确的TOTP密码 TOTP的时间步长X为30秒。 T0为0。 使用HMAC-SHA-512作为哈希函数,而不是默认的HMAC-SHA-1。 令牌共享密钥是用户ID,后跟ASCII字符串值“ APICHALLENGE”(不包括双引号)。 共享的秘密例子
例如,如果用户标识为“ email@example.com”,则令牌共享密钥为“ email@example.comAPICHALLENGE”(不带引号)。
如果您的POST请求成功,则服务器返回HTTP状态代码200。
我尝试非常仔细地遵循此概述,并以不同的方式测试我的作品。但是,看来我做错了。我们应该从节点服务器后端发出请求。到目前为止,这是我所做的。我使用npm init创建了一个新的npm项目,并安装了将在下面的代码中看到的依赖项:
const base64 = require('base-64');
const utf8 = require('utf8');
const { totp } = require('otplib');
const reqJSON =
{
github_url: GITHUB_URL,
contact_email: MY_EMAIL
}
const stringData = JSON.stringify(reqJSON);
const URL = CHALLENGE_URL;
const sharedSecret = reqJSON.contact_email + "APICHALLENGE";
totp.options = { digits: 10, algorithm: "sha512" , epoch: 0}
const myTotp = totp.generate(sharedSecret);
const isValid = totp.check(myTotp, sharedSecret);
console.log("Token Info:", {myTotp, isValid});
const authStringUTF = reqJSON.contact_email + ":" + myTotp;
const bytes = utf8.encode(authStringUTF);
const encoded = base64.encode(bytes);
const createReq = async () =>
{
try
{
// set the headers
const config = {
headers: {
'Content-Type': 'application/json',
"Authorization": "Basic " + encoded
}
};
console.log("Making req", {URL, reqJSON, config});
const res = await axios.post(URL, stringData, config);
console.log(res.data);
}
catch (err)
{
console.error(err.response.data);
}
};
createReq();```
As far as I understand, I'm not sure where I'm making a mistake. I have tried to be very careful in my understanding of the requirements. I have briefly looked into all of the documents the challenge outlines, and gathered the necessary requirements needed to correctly generate a TOTP under the given conditions.
I have found the npm package otplib can satisfy these requirements with the options I have passed in.
However, my solution is incorrect. When I try to submit my solution, I get the error message, "Invalid token, wrong code". Can someone please help me see what I'm doing wrong?
I really don't want all my hard work to be for nothing, as this was a lengthy project.
Thank you so much in advance for your time and help on this. I am very grateful.
答案 0 :(得分:0)
使用 hotp-totp-generator 库
尝试此代码const axios = require('axios');
const base64 = require('base-64');
const utf8 = require('utf8');
const hotpTotpGenerator = require('hotp-totp-generator');
const ReqJSON = {
github_url: 'GITHUB_REPO',
contact_email: 'YOUR_MAIL',
};
const stringData = JSON.stringify(ReqJSON);
const URL = 'CHALLENGE_URL';
const sharedSecret = ReqJSON.contact_email + 'SPECIAL_CODE';
const MyTOTP = hotpTotpGenerator.totp({
key: sharedSecret,
T0: 0,
X: 30,
algorithm: 'sha512',
digits: 10,
});
const authStringUTF = ReqJSON.contact_email + ':' + MyTOTP;
const bytes = utf8.encode(authStringUTF);
const encoded = base64.encode(bytes);
const createReq = async () => {
try {
const config = {
withCredentials: true,
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic ' + encoded,
},
};
console.log('Making request', { URL, ReqJSON, config });
const response = await axios.post(URL, stringData, config);
console.log(response.data);
} catch (err) {
console.error(err.response.data);
}
};
createReq();