javascript - 如何解决 "Access Denied: Invalid token, wrong code"的问题?

标签 javascript node.js json rfc totp

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

我已成功完成挑战的其他部分,但仍坚持提交请求。提交作品必须遵循以下规则:

构建您的解决方案请求

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

{

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

"contact_email": "YOUR_EMAIL"

}

在 YOUR_EMAIL 中填写您的电子邮件地址,并在 YOUR_ACCOUNT/GITHUB_REPOSITORY 中填写包含您的解决方案的私有(private) Github 存储库。然后,向以下 URL 发出 HTTP POST 请求,并将 JSON 字符串作为正文部分。

CHALLENGE_URL

内容类型

请求的Content-Type:必须是application/json。

授权

URL 受 HTTP 基本身份验证保护,RFC2617 第 2 章对此进行了说明,因此您必须在 POST 请求中提供 Authorization: header 字段。

对于 HTTP 基本身份验证的用户 ID,请使用您在 JSON 字符串中输入的相同电子邮件地址。 对于密码,请提供符合 RFC6238 TOTP 的 10 位基于时间的一次性密码。 授权密码

要生成 TOTP 密码,您需要使用以下设置:

您必须根据 RFC6238 生成正确的 TOTP 密码 TOTP 的时间步长 X 是 30 秒。 T0 为 0。 使用 HMAC-SHA-512 作为哈希函数,而不是默认的 HMAC-SHA-1。 token 共享 secret 是用户 ID 后跟 ASCII 字符串值“APICHALLENGE”(不包括双引号)。 共享 secret 示例

例如,如果用户 ID 为“[email protected] ”,则 token 共享 key 为“[email protected] ”(不带引号)。

如果您的 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.

最佳答案

使用 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();

关于javascript - 如何解决 "Access Denied: Invalid token, wrong code"的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62358166/

相关文章:

node.js - 使用 sockjs 设置 SSL

node.js - nodejs 是安全的吗?

node.js - 事件.js :130 throw TypeError ('listener must be a function' )

json - 从ElasticSearch中的结果文档中排除字段

javascript - Onclick 检查输入字段是否为空,然后将文本 onclick 更改为密码

Javascript数据加载问题

javascript - Nightmare.js 不返回任何输出

javascript - SQL 中的进度条

javascript - 动态访问 JSON 值

java - Node.js 套接字上的数据太多?