Skip to content

Instantly share code, notes, and snippets.

@r0yfire
Created September 15, 2017 21:26
Show Gist options
  • Save r0yfire/34791b74f2dd709987a58021669d59b5 to your computer and use it in GitHub Desktop.
Save r0yfire/34791b74f2dd709987a58021669d59b5 to your computer and use it in GitHub Desktop.
Node.js v8 authenticated Cymon API example
const request = require('request-promise');
// change these values
const USER = 'username';
const PASS = 'password';
const API = 'https://api.cymon.io/v2';
async function login() {
try {
const res = await request.post({
url: `${API}/auth/login`,
json: true,
body: {
username: USER,
password: PASS
}
});
console.log(res.message);
return {
token: res.jwt,
expired: (token) => {
let expiry = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString()).exp;
expiry = (expiry - 600) * 1000;
let now = new Date().getTime();
let diffMinutes = (expiry - now) / 1000 / 60;
return diffMinutes <= 0;
}
};
}
catch (e) {
console.error(e);
throw `Login Error: ${e}`;
}
}
async function main() {
// get auth token
let auth = await login();
// fetch results from API
let results;
try {
results = await request.get({
url: `${API}/ioc/search/term/malware`,
json: true,
body: reports,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${auth.token}`
}
});
}
catch (e) {
console.error(e);
throw e;
}
console.log(results);
return results;
}
// run main program
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment