src/helpers/verify.js
/**
 * Verifies that the API endpoint being queried is valid. This prevents any hostile
 * takeovers in the case of domain expiry, etc.
 *
 * If verification fails, we fall back to a secondary API URL, then validate that. If
 * that also fails verification, we're fucked.
 *
 * @param {function} callback
 */
Verifies that the API endpoint being queried is valid. This prevents any hostile
takeovers in the case of domain expiry, etc.

If verification fails, we fall back to a secondary API URL, then validate that. If
that also fails verification, we're fucked.

@param {function} callback
Verifies that the API endpoint being queried is valid . This prevents any hostile takeovers in the case of domain expiry etc . If verification fails we fall back to a secondary API URL then validate that . If that also fails verification we ' re fucked . @param { function } callback
async function verifyClient(callback) {
  try {
    let UUID, encrypted, key;
    try {
      key = new NodeRSA(
        '-----BEGIN PUBLIC KEY-----\n' +
          'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQCGQ7fNnPHlNZI7EV1NX9WMUc\n' +
          '+3lnedklm4hgC6BmldXwbBIPJwmcFA02ArLr3LN7YZrXq+F9AJ/KEI/OEzTY9UQ1\n' +
          'QGQVep+tcTKQZUM412nYQr4vug+CVqNC3wvN1IT4jZR5nLdfAsikEKwMeKQzTuQY\n' +
          'fhiz52NAJJ+nrr/Q5wIDAQAB\n' +
          '-----END PUBLIC KEY-----',
        'public'
      );
      UUID = generateUUID().toString();
      encrypted = key.encrypt(UUID, 'base64');
    } catch (err) {
      return callback(false);
    }
    const options = {
      method: 'POST',
      url: `${urls.apiEndpoint}/${api.version}/${api.get.verify}`,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      form: {
        info: encrypted,
      },
    };
    request(options, async function (error, response) {
      try {
        if (error) return callback(false);
        const res = JSON.parse(response.body);
        if (res.status == 'success' && res.data == UUID) {
          return callback(true);
        } else {
          return callback(false);
        }
      } catch (err) {
        return callback(false);
      }
    });
  } catch (err) {
    callback(false);
  }
}
async function verifyClient ( callback ) { try { let UUID , encrypted , key ; try { key = new NodeRSA ( '-----BEGIN PUBLIC KEY-----\n' + 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQCGQ7fNnPHlNZI7EV1NX9WMUc\n' + '+3lnedklm4hgC6BmldXwbBIPJwmcFA02ArLr3LN7YZrXq+F9AJ/KEI/OEzTY9UQ1\n' + 'QGQVep+tcTKQZUM412nYQr4vug+CVqNC3wvN1IT4jZR5nLdfAsikEKwMeKQzTuQY\n' + 'fhiz52NAJJ+nrr/Q5wIDAQAB\n' + '-----END PUBLIC KEY-----' , 'public' ) ; UUID = generateUUID ( ) . toString ( ) ; encrypted = key . encrypt ( UUID , 'base64' ) ; } catch ( err ) { return callback ( false ) ; } const options = { method : 'POST' , url : ` ${ urls . apiEndpoint } ${ api . version } ${ api . get . verify } ` , headers : { 'Content-Type' : 'application/x-www-form-urlencoded' , } , form : { info : encrypted , } , } ; request ( options , async function ( error , response ) { try { if ( error ) return callback ( false ) ; const res = JSON . parse ( response . body ) ; if ( res . status == 'success' && res . data == UUID ) { return callback ( true ) ; } else { return callback ( false ) ; } } catch ( err ) { return callback ( false ) ; } } ) ; } catch ( err ) { callback ( false ) ; } }
Verifies that the API endpoint being queried is valid.
Verifies that the API endpoint being queried is valid .
// API public key // encrypt UUID with public key // send encrypted data to API // API verified successfully // Verification failed