使用基本身份验证和TOTP密码向api发送发布请求

时间:2020-05-30 09:59:19

标签: javascript basic-authentication totp

我应该在给定正文的情况下向myUrl发送发帖请求。该api受基本身份验证保护,我应该使用通过HMAC-SHA-512算法生成的TOTP密码,T0 = 0,其余选项为默认值。共享密码应与下面的代码段相同。但是,我不断收到403,无效令牌,错误代码。我可能会缺少什么?

密码已生成并且看似有效

const fetch = require('node-fetch');
const base64 = require('base-64');
const utf8 = require('utf8');

const { totp } = require('otplib');


const body = 
{
    "github_url": "myGitHubRepo",
    "contact_email": "myEmail"
}
const bodyString = JSON.stringify(body);

const url = "apiUrl";
const sharedSecret = `${body.contact_email}Givenword`;

totp.options = { digits: 10, algorithm: 'sha512', epoch: 0 }

const password = totp.generate(sharedSecret);
const isValid = totp.check(password, sharedSecret);

console.log({password, isValid});

const authStringUTF = `${body.contact_email}:${password}`;
const bytes = utf8.encode(authStringUTF);
const encoded = base64.encode(bytes);

const headers = {
  'Content-Type': 'application/json',
  'Authorization': `Basic ${encoded}`,
  Accept: 'application/json'
}

fetch(url, { method: 'POST', headers, body: bodyString})
  .then((resp) => {
    console.log({headers, url, body});
    return resp.json()
  })
  .then((data) => 
    console.log(data)
  )

0 个答案:

没有答案