Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate next token? #181

Closed
youthlin opened this issue Aug 16, 2019 · 4 comments
Closed

How to generate next token? #181

youthlin opened this issue Aug 16, 2019 · 4 comments
Labels
support

Comments

@youthlin
Copy link

youthlin commented Aug 16, 2019

Hi,
The 'window ' option can make the next token valid if it's set to 1(or greater),
So how to generate the next token?
I use this lib to generate current token, and send it to another service to check if valid.
But every token can only use once in that service, and on that service the 'window' is 2,
so I want to generate the next token when I use current token twice.
It seems the 'epoch' option is useful, Can you give a example? I'm use it on a browser.
Thanks a lot.

@youthlin
Copy link
Author

youthlin commented Aug 16, 2019

function generate(secret, offset = 0) {
    if (offset === 0) {
        return otplib.authenticator.generate(secret);
    }
    let number = otplib.authenticator.timeRemaining() * 1000;
    if (offset < 0) {
        number = -(otplib.authenticator.timeUsed() + 1) * 1000;
    }
    const offsetEpoch = Date.now() + number;
    const normalOptions = otplib.authenticator.options;
    let offsetOptions = Object.assign({}, normalOptions);
    offsetOptions.epoch = offsetEpoch / 1000;
    otplib.authenticator.options = offsetOptions;
    const offsetToken = otplib.authenticator.generate(secret);
    otplib.authenticator.options = normalOptions;
    return offsetToken;
}

@yeojz
Copy link
Owner

yeojz commented Aug 17, 2019

hi @youthlin,

I think there is an easier way of doing the above.

function generate(offset, options) {
  const delta = options.step * 1000;
  authenticator.options = {
    epoch: (Date.now() + offset * delta) / 1000
  }

  const offsetToken = authenticator.generate(secret);

  // reset your epoch
  authenticator.options = { epoch: null }
  
  return offsetToken;
}

@yeojz yeojz reopened this Aug 17, 2019
@yeojz yeojz added the support label Aug 17, 2019
@yeojz yeojz closed this as completed Aug 19, 2019
@youthlin
Copy link
Author

youthlin commented Aug 19, 2019

@yeojz Thank you so much~ I'm new to JS

@youthlin
Copy link
Author

youthlin commented Feb 24, 2020

v12.x

    function generate(secret, offset = 0) {
        if (offset === 0) {
            return otplib.authenticator.generate(secret);
        }
        const allOptions = otplib.authenticator.allOptions();
        const delta = allOptions.step * 1000;
        // step = 30s == the code changes every 30s.
        // same epoch == same code. so we can get the next code via set the epoch option
        otplib.authenticator.options = {epoch: Date.now() + offset * delta};
        const offsetToken = otplib.authenticator.generate(secret);
        otplib.authenticator.resetOptions();
        return offsetToken;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support
Projects
None yet
Development

No branches or pull requests

2 participants