2016-11-28 1 views
0

그래서 TOTP 코드가있는 사이트에 로그인해야합니다. 나에게 TOTP 코드를 얻을 수있는 간단한 NodeJS 스크립트를 만들었지 만, 항상 잘못된 코드 다.NodeJS - 잘못된 totp 코드 생성

var notp = require('notp'); 
var base32 = require('thirty-two'); 
var key = 'KEYHERE'; 
var token = notp.totp.gen(key, 30); 
console.log(token); 
var login = notp.totp.verify(token, key); 
if (!login) { 
    return console.log('Token invalid'); 
} 
console.log('Token valid, sync value is %s', login.delta); 

또한, 나는 내 시간 (내가 바로 그때 그것을했다 있는지 확실하지 않습니다)를 동기화했다. 누군가이 코드를 수정하거나 서버에서 시간을 동기화하는 데 도움을 줄 수 있습니까? 서버 프랑스

에서입니다

답변

0

당신이 TIMESYNC 예 NPM 모듈을 사용할 수 있습니다

// create a timesync instance 
var ts = timesync({ 
    server: '...', // either a single server, 
    peers: [...] // or multiple peers 
}); 

// get notified on changes in the offset 
ts.on('change', function (offset) { 
    console.log('offset from system time:', offset, 'ms'); 
} 

// get the synchronized time 
console.log('now:', new Date(ts.now())); 
+0

가 도움을 수행 확인이를? – AJS

+0

미안하지만 이해가 안되니? –

+0

totp 코드를 사용하여 토큰의 유효성을 검사하고 있습니까? – AJS

0

당신이 생성하고 notp

var notp = {}; 
    notp.gen = function(key, opt) { 
     opt = opt || {}; 
     var time = opt.time || 30; 
     var _t = Date.now(); 

     // Time has been overwritten. 
     if(opt._t) { 
      if(process.env.NODE_ENV != 'test') { 
       throw new Error('cannot overwrite time in non-test environment!'); 
      } 
      _t = opt._t; 
     } 

     // Determine the value of the counter, C 
     // This is the number of time steps in seconds since T0 
     opt.counter = Math.floor((_t/1000)/time); 

     return hotp.gen(key, opt); 
    }; 
notp.verify = function(token, key, opt) { 
    opt = opt || {}; 
    var time = opt.time || 30; 
    var _t = Date.now(); 

    // Time has been overwritten. 
    if(opt._t) { 
     if(process.env.NODE_ENV != 'test') { 
      throw new Error('cannot overwrite time in non-test environment!'); 
     } 
     _t = opt._t; 
    } 

    // Determine the value of the counter, C 
    // This is the number of time steps in seconds since T0 
    opt.counter = Math.floor((_t/1000)/time); 

    return hotp.verify(token, key, opt); 
}; 
module.exports.totp = totp; 
+0

안녕하세요, 저는 그것을 생성 할 수 없습니다. 이미 로그인하려는 웹 사이트의 키를 가지고 있습니다. –

+0

당신이 얻고있는 오류가 무엇인지 알 수 없습니까? 당신은 토큰 유효 또는 무효를 얻고 있습니까? 그리고 당신은 notp를 사용하고 있습니다 ..... –