簡體   English   中英

如何解決“拒絕訪問:無效令牌,錯誤代碼”的問題?

[英]How to solve the problem of “Access Denied: Invalid token, wrong code”?

我最近分配的一個學校項目有一個我們必須完成的編碼挑戰。 挑戰有多個部分,最后一部分是上傳到私有 GitHub 存儲庫,並通過在特定條件下發出 POST 請求來提交完成請求。

我已經成功完成了挑戰的其他部分,並且一直在提交請求。 提交必須遵循以下規則:

構建您的解決方案請求

首先,構造一個 JSON 字符串,如下所示:

{

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

"contact_email": "YOUR_EMAIL"

}

填寫 YOUR_EMAIL 的 email 地址,以及在 YOUR_ACCOUNT/GITHUB_REPOSITORY 中使用解決方案的私有 Github 存儲庫。 然后,以 JSON 字符串作為正文部分,向以下 URL 發出 HTTP POST 請求。

CHALLENGE_URL

內容類型

請求的 Content-Type: 必須是 application/json。

授權

URL 受 HTTP 基本身份驗證的保護,這在 RFC2617 的第 2 章中進行了說明,因此您必須在您的 POST 請求中的 3 字段中提供授權:Z099FB995346F31C749F6E40DB0F395。

對於 HTTP 基本身份驗證的用戶 ID,使用您在 JSON 字符串中輸入的相同 email 地址。 對於密碼,請提供符合 RFC6238 TOTP 的 10 位基於時間的一次性密碼。 授權密碼

要生成 TOTP 密碼,您需要使用以下設置:

您必須根據 RFC6238 TOTP 的 Time Step X 為 30 秒生成正確的 TOTP 密碼。 T0 為 0。對 hash function 使用 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.

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM