daegalus / dart-otp

RFC6238 Time-Based One-Time Password / Google Authenticator Library
MIT License
100 stars 25 forks source link

Function to access the interval #32

Closed guilhermedaldim closed 3 years ago

guilhermedaldim commented 3 years ago

@Daegalus nice work with the plugin, it's really good!

That's not a issue, I'd like support for accessing the interval between changing one code to another.

I tried to create a timer to show the code expiry time, however, the plugin starts counting before the layout, so my timer is not accurate.

daegalus commented 3 years ago

I can try to do that, but my library doesn't do any actual counting. For TOTP it just takes the current time, divided by the provided interval (to 30 by default for 30 seconds) and uses that as the number passed to HOTP. This makes it so that the number changes only at your intervals. But it never counts.

For HOTP, it relies on the user to do the counting.

I can expose the value of the current TOTP interval time value if that helps any, so you could pass it into HOTP if needed. Or the current timestamp .

Would that help? If so I can probably add it sometime this week.

guilhermedaldim commented 3 years ago

I'm using TOTP, exposing the interval value would help, so I would be able to create my timer according to the plugin's interval, which would be a great help.

daegalus commented 3 years ago

Ok, give me a few days to get this added in a clean manner and I'll report back.

daegalus commented 3 years ago

Ok, just released 3.0.1 with 2 new static variables you can check for information.

OTP.lastUsedTime
OTP.lastUsedCounter

lastUsedTime is just the timestamp. It should be identical to whatever is passed into the function. lastUsedCounter will be the modified timestamp that is divided by the interval you provide (in this case 30 seconds). This is the one that will probably most useful for you.

If you use lastUsedCounter to generate a code, remember to pass it into generateHOTP, as generateTOTP will try to think of it as a unix timestamp and do all the math magic on it again. Hope this helps.

I will close this ticket, reopen it or start a new one if you have any issues.