2

我最近分配的一个学校项目有一个我们必须完成的编码挑战。挑战有多个部分,最后一部分是上传到私有 GitHub 存储库并通过在特定条件下发出 POST 请求来提交完成请求。

我已经成功完成了挑战的其他部分,并且一直在提交请求。提交必须遵循以下规则:

构建您的解决方案请求

首先,构造一个 JSON 字符串,如下所示:

{

"github_url": "https://github.com/YOUR_ACCOUNT/GITHUB_REPOSITORY",

"contact_email": "YOUR_EMAIL"

}

填写 YOUR_EMAIL 的电子邮件地址,以及在 YOUR_ACCOUNT/GITHUB_REPOSITORY 中使用您的解决方案的私有 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 的 Time Step X 为 30 秒生成正确的 TOTP 密码。T0 为 0。使用 HMAC-SHA-512 作为哈希函数,而不是默认的 HMAC-SHA-1。令牌共享密钥是用户 ID,后跟 ASCII 字符串值“APICHALLENGE”(不包括双引号)。共享秘密示例

例如,如果用户 ID 是“email@example.com”,则令牌共享密钥是“email@example.comAPICHALLENGE”(不带引号)。

如果您的 POST 请求成功,服务器将返回 HTTP 状态代码 200 。

我试图非常仔细地遵循这个大纲,并以不同的方式测试我的工作。但是,似乎我无法正确处理。我们应该从 Node 服务器后端发出请求。这是我到目前为止所做的。我使用 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.
4

1 回答 1

3

使用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();
于 2020-10-20T13:54:19.123 回答