Created
September 15, 2017 21:26
-
-
Save r0yfire/34791b74f2dd709987a58021669d59b5 to your computer and use it in GitHub Desktop.
Node.js v8 authenticated Cymon API example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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