Monday, 26 September 2022

Axios.create with authorization header OAUTH 1.0 RSA-SHA1

I am trying to send an Authorization request header to axios i tried to follow this link: https://pandeysoni.medium.com/how-to-create-oauth-1-0a-signature-in-node-js-7d477dead170 but with no luck as i want to do it using RSA-SHA1 with token and token secret and not HMAC-SHA1

this is what i did so far with no luck Getting 500 status code from axios

const fs = require("fs");
let privateKeyData = fs.readFileSync("jira.pem", "utf-8");

function generateOAuthHeader(Config: any) {
const oauth_timestamp = Math.floor(Date.now() / 1000);
const oauth_nonce = crypto.randomBytes(16).toString('hex');;
const parameters = {
    ...Config.queryParameters,
    oauth_consumer_key: Config.consumer_key,
    oauth_signature_method: 'RSA-SHA1',
    oauth_timestamp: oauth_timestamp,
    oauth_nonce: oauth_nonce,
    oauth_version: '1.0'
}
let ordered: any = {};
Object.keys(parameters).sort().forEach(function (key) {
    ordered[key] = parameters[key];
});
let encodedParameters = '';
for (let k in ordered) {
    let encodedValue = escape(ordered[k]);
    let encodedKey = encodeURIComponent(k);
    if(encodedParameters === ''){
        encodedParameters += `${encodedKey}=${encodedValue}`;
    }
    else{
        encodedParameters += `&${encodedKey}=${encodedValue}`;
    } 
}
console.log(encodedParameters);
const encodedUrl = encodeURIComponent(Config.base_url);
encodedParameters = encodeURIComponent(encodedParameters);
const signature_base_string = `${Config.method}&${encodedUrl}&${encodedParameters}`
console.log(signature_base_string);
const signing_key = `${Config.secret_key}&`; //as token is missing in our case.
const oauth_signature = crypto.createHmac('sha1', signing_key).update(signature_base_string).digest().toString('hex');
console.log(oauth_signature);
const encoded_oauth_signature = encodeURIComponent(oauth_signature);
console.log(encoded_oauth_signature);
const authorization_header = `OAuth oauth_consumer_key="${Config.consumer_key}",oauth_token="${Config.token}",oauth_token_secret=${Config.tokenSecret},oauth_signature_method="RSA-SHA1",oauth_timestamp="${oauth_timestamp}",oauth_nonce="${oauth_nonce}",oauth_version="1.0",oauth_signature="${encoded_oauth_signature}"`
console.log(authorization_header);
return authorization_header

}

export function prepareOAuthAuthorization(AccessToken: string, AccessSecret: string, url: string) {
var nonce = Array.from(Array(32), () => Math.floor(Math.random() * 36).toString(36)).join('');
var timestamp = new Date().getTime();

var config = {
    consumer_key: 'otjjiraforoutlook',
    secret_key: privateKeyData,
    base_url: url,
    method: 'PUT',
    queryParameters: null,
    token: AccessToken,
    tokenSecret: AccessSecret
}
var authstr = { Authorization: generateOAuthHeader(config).toString() }
console.log("authstr", authstr)
return authstr
}

console values

Authorization: 'OAuth oauth_consumer_key="otjjiraforoutlook",oauth_token="U3KhAXaYU0oMdjNxpIjfvrYZEIr6Mypw",oauth_token_secret=CIwmYtwSvG4jXthdNGtgqs8WUvKEx1J5,oauth_signature_method="RSA-SHA1",oauth_timestamp="1663929413",oauth_nonce="cd29e6ef708f4edb3f2e4a2b88865286",oauth_version="1.0",oauth_signature="8187e36febc2aedde9d23e291cc022c700c35ee8"'

I guess the main problem here is with the oauth signature and how it is generated i tried to look into google but all i get is regarding HMAC-SHA1



from Axios.create with authorization header OAUTH 1.0 RSA-SHA1

No comments:

Post a Comment