cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 
cancel
116
Views
0
Helpful
0
Replies

Not able to hit the Add User api

kalimuthu-r
Level 1
Level 1

Hi team

Iam using the same credentials to hit  Add User api as I use for List users and List groups api  and while the later two apis are working fine the Add User api failing with Invalid signature error though Iam using same method to generate signature to both of List apis which is working fine.

Can anyone please help me here and here is the script I use for hit all those apis

 

 

 

 

 

 

const axios = require('axios');
const crypto = require('crypto');
const qs = require('querystring');

const ikey = '****************';
const skey = '*****************************';
const host = 'api-b57e83f8.duosecurity.com';
const method = 'GET';
const addMethod = 'POST';
const userspath = '/admin/v1/users';
const groupspath = '/admin/v1/groups';
const params = {
  limit: 10, 
};
const adduserparams = {
  username: 'sumit888'
}

// Function to generate the HMAC signature
function signRequest(method, host, path, params, skey) {
  const date = new Date().toUTCString();
  const canonicalString = `${date}\n${method}\n${host}\n${path}\n${qs.stringify(params)}`;
  const sig = crypto.createHmac('sha1', skey).update(canonicalString).digest('hex');
  return { date, sig };
}

// Function to list users
async function listUsers() {
  const { date, sig } = signRequest(method, host, userspath, params, skey);

  try {
    const response = await axios.get(`https://${host}${userspath}`, {
      headers: {
        'Authorization': `Basic ${Buffer.from(`${ikey}:${sig}`).toString('base64')}`,
        'Date': date,
      },
      params,
    });

    console.log('Users:', response.data.response);
  } catch (error) {
    console.error('Error fetching users:', error.response ? error.response.data : error.message);
  }
}

async function listGroups() {
    const { date, sig } = signRequest(method, host, groupspath, params, skey);
  
    try {
      const response = await axios.get(`https://${host}${groupspath}`, {
        headers: {
          'Authorization': `Basic ${Buffer.from(`${ikey}:${sig}`).toString('base64')}`,
          'Date': date,
        },
        params,
      });
  
      console.log('Groups:', response.data.response);
    } catch (error) {
      console.error('Error fetching groups:', error.response ? error.response.data : error.message);
    }
}

async function addUser() {
  const { date, sig } = signRequest(addMethod, host, userspath, adduserparams, skey);
  try {
    const response = await axios.get(`https://${host}${userspath}`, {
      headers: {
        'Authorization': `Basic ${Buffer.from(`${ikey}:${sig}`).toString('base64')}`,
        'Date': date,
      },
      params: adduserparams
    });

    console.log('AddUser:', response.data.response);
  } catch (error) {
    console.error('Error Adding User:', error.response ? error.response.data : error.message);
  }
}

listUsers();
listGroups();
addUser();

 

 

 

 

 

 

and here is the responses for those three functions

 

 

 

 

 

 

Error Adding User: {
  code: 40103,
  message: 'Invalid signature in request credentials',
  stat: 'FAIL'
}
Users: [
  {
    alias1: null,
    alias2: null,
    alias3: null,
    alias4: null,
    aliases: {},
    created: 1718346966,
    desktoptokens: [],
    email: 'kalimuthu.r@zluri.com',
    enable_auto_prompt: true,
    firstname: '',
    groups: [],
    is_enrolled: false,
    last_directory_sync: null,
    last_login: null,
    lastname: '',
    lockout_reason: null,
    notes: '',
    phones: [],
    realname: 'Kalimuthu Ramachandran',
    status: 'active',
    tokens: [],
    u2ftokens: [],
    user_id: 'DUJCIYMAVS6E0Z7430KT',
    username: 'kalie',
    webauthncredentials: []
  }
]
Groups: [
  {
    desc: '',
    group_id: 'DG6XQZFKE5T8ET4D2QDI',
    mobile_otp_enabled: false,
    name: 'zluri',
    push_enabled: false,
    sms_enabled: false,
    status: 'Active'
  }
]

 

 

 

 

 

 



0 Replies 0