Issue with ConnectyCube User Session Token Creation - "Unexpected Signature"

68 Views Asked by At

I am encountering a perplexing issue while working with the ConnectyCube REST API for user management. My code successfully generates a signature for actions, such as creating an app session token, but when attempting to create a user session token, the server consistently returns an "unexpected signature" error.

Here's the method I use to generate the signature:

const getSignature = () => {
  const nonce = Math.floor(Math.random() * 1000000);
  const timestamp = Math.floor(Date.now() / 1000);
  const stringForSignature = `application_id=${config.applicationId}&auth_key=${config.authKey}&nonce=${nonce}&timestamp=${timestamp}`;
  const signature = crypto.createHmac('sha1', config.authSecret).update(stringForSignature).digest('hex');
  return { signature: signature, timestamp: timestamp, nonce: nonce };
}

The signature generation appears to work correctly, as I can successfully create an app session token using it:

const getAppSessionToken = async () => {
  const signature = getSignature();
  const response = await axios.post(
    'https://api.connectycube.com/session', {
    'application_id': config.applicationId,
    'auth_key': config.authKey,
    'nonce': signature.nonce,
    'timestamp': signature.timestamp,
    'signature': signature.signature
  }, { headers: { 'Content-Type': 'application/json' } });
  return response.data.session.token;
}

However, when attempting to create a user session token, the server responds with an "unexpected signature" error:

const createSessionWithLogin = async (emailAddress, password) => {
  const signature = getSignature();
  return await axios.post(
    'https://api.connectycube.com/session',
    {
      'application_id': config.applicationId,
      'auth_key': config.authKey,
      'nonce': signature.nonce,
      'timestamp': signature.timestamp,
      'signature': signature.signature,
      'user': {
        'email': emailAddress,
        'password': password
      }
    },
    {
      headers: {
        'Content-Type': 'application/json'
      }
    }
  );
}

I would appreciate any insights into what might be causing this issue or suggestions for troubleshooting. Thank you!

1

There are 1 best solutions below

6
Bench Vue On

You can get user token by this code

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

// Config object with your application credentials
const config = {
  applicationId: 'your App ID',
  authKey: 'your authKey',
  authSecret: 'your authSecret',
  emailAddress: 'your login id',
  password: 'your password'
};

// Function to generate the signature
const getSignature = () => {
  const nonce = Math.floor(Math.random() * 1000000);
  const timestamp = Math.floor(Date.now() / 1000);
  const stringForSignature = `application_id=${config.applicationId}&auth_key=${config.authKey}&nonce=${nonce}&timestamp=${timestamp}`;
  const signature = crypto.createHmac('sha1', config.authSecret).update(stringForSignature).digest('hex');
  return { signature: signature, timestamp: timestamp, nonce: nonce };
};

// Function to get the application session token
const getAppSessionToken = async () => {
  const signature = getSignature();
  const response = await axios.post(
    'https://api.connectycube.com/session', {
      'application_id': config.applicationId,
      'auth_key': config.authKey,
      'nonce': signature.nonce,
      'timestamp': signature.timestamp,
      'signature': signature.signature
    }, { headers: { 'Content-Type': 'application/json' } }
  );
  return response.data.session.token;
};

// Function to create a session with user login
const createSessionWithLogin = async (login, password) => {
    const signature = getSignature();
    const userData = {
      'user[login]': login,
      'user[password]': password
    };
  
    const requestData = {
      'application_id': config.applicationId,
      'auth_key': config.authKey,
      'nonce': signature.nonce,
      'timestamp': signature.timestamp,
      'signature': signature.signature,
      ...userData
    };
  
    return await axios.post(
      'https://api.connectycube.com/session',
      requestData,
      {
        headers: {
          'Content-Type': 'application/json'
        }
      }
    );
  };
  

// Example usage
getAppSessionToken()
  .then(token => console.log("Session Token:", token))
  .catch(error => console.error("Error fetching token:", error));

// Example usage of createSessionWithLogin
createSessionWithLogin(config.emailAddress, config.password)
  .then(response => console.log("Session with login:", response.data))
  .catch(error => console.error("Error creating session with login:", JSON.stringify(error)));

Result enter image description here