How do I generate Basic HTTP RFC2617 Authentication code?

166 Views Asked by At

My problem is exactly same as this one.

The fact is, it's answer didn't worked for me. It is still showing "wrong code".

{ message: 'Access denied: Invalid token, wrong code' }

headers are

headers: {
      'Content-Type': 'application/json',
      Authorization: 'Basic bW9oaXRrdW1hcnNpbmdoMTIzNDRAZ21haWwuY29tOjE1NDg3NjY3Njg='
}

Here is the code used

import axios from 'axios';
import base64 from 'base-64';
import utf8 from 'utf8';

import { totp } from 'otplib';


const reqJSON =
{
    github_url: GITHUB_URL,
    contact_email: EMAIL
}
const stringData = JSON.stringify(reqJSON);

const URL = API_URL;
const sharedSecret = reqJSON.contact_email + "SOME_STRING";

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);

console.log('encoded ->', encoded);



const createReq = async () => {
    try {
        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();

EDIT - Added current code for generating Basic HTTP RFC2617 Authentication Code

Thanks you so much!

0

There are 0 best solutions below