Skip to content

Commit

Permalink
Node version updated
Browse files Browse the repository at this point in the history
  • Loading branch information
biblioeteca committed Jun 19, 2021
1 parent deb1aca commit d6ea2fd
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
82 changes: 73 additions & 9 deletions lib/nomorepass.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*
*/
var CryptoJS = require('crypto-js');
var request = require('request');
const FormData = require ('form-data');
const axios = require('axios');



var NoMorePass = {};
Expand All @@ -24,14 +26,32 @@ function nmp_newtoken () {
}

function nmp_post (url,params,callback) {
request.post({url: url,
header: {'apikey': NoMorePass.apikey},
form: params},
function (err, httpResponse, body) {
if (err) {
return console.error('call failed:', err);
}
callback(JSON.parse(body));
const formData = new FormData();
for(var name in params) {
formData.append(name,params[name]);
}
let theaders = formData.getHeaders();
theaders.apikey = NoMorePass.apikey;
axios.post(url,formData,{
headers: theaders,
})
.then(function (response) {
callback(response.data);
})
.catch(function (error) {
return console.error('call failed:', error);
});
}

function iot_post (url,params,callback) {
axios.post(url,params,{
headers: {"Content-Type": "application/json"}
})
.then (function (response){
callback(response.data);
})
.catch(function(error){
console.log(error);
});
}

Expand Down Expand Up @@ -279,4 +299,48 @@ exports.getQrNomoreKeys = function (site, user, pass, type, extra, callback) {
exports.send = function (callback){
var txt = "XXXXXXXXXXXX"+NoMorePass.ticket;
setTimeout(function(){nmp_ping(txt,callback);},4000);
}

exports.sendRemotePassToDevice = function (cloud,deviceid,secret,username,password,callback) {
// Envía una contraseña remota a un dispositivo cloud
// cloud: url de /extern/send_ticket
// devideid: id del dispositivo
// secret: md5 del secreto del dispositivo
// username: usuario
// password: contraseña
// callback función a llamar en caso de éxito
let cloudurl = cloud;
if (cloudurl==null)
cloudurl="https://api.nmkeys.com/extern/send_ticket";
let token = secret;
nmp_post(NoMorePass.getidUrl, {
'site': 'Send remote pass',
}, function(data){
if (data.resultado=='ok') {
let ticket = data.ticket;
let ep = CryptoJS.AES.encrypt(password, token);
nmp_post(NoMorePass.grantUrl, {
'grant': 'grant',
'ticket': ticket,
'user': username,
'password' : ''+ep,
'extra': JSON.stringify({'type': 'remote'})
}, function (data){
if (data.resultado=='ok') {
iot_post (cloud,
{'hash': token.substring(0,10),
'deviceid': deviceid,
'ticket': ticket},
function (data) {
if (typeof callback == 'function')
callback (data);
}, (data)=>{console.log(data)})
} else {
console.log (data);
}
})
} else {
console.log (data);
}
})
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"homepage": "https://github.com/yoprogramo/nomorepass#readme",
"dependencies": {
"axios": "^0.21.1",
"crypto-js": "^3.1.9-1",
"request": "^2.81.0"
"form-data": "^4.0.0"
}
}

0 comments on commit d6ea2fd

Please sign in to comment.