samdjstevens / java-totp

A java library for implementing Time-based One Time Passwords for Multi-Factor Authentication.
MIT License
448 stars 109 forks source link

TimeProvider that can be instantiated by passing a Clock instance #28

Open mmondino opened 4 years ago

mmondino commented 4 years ago

It would be interesting to have a timeprovider that can be instantiated by passing a clock instance as a parameter.

This would be useful for testing and when working with instances of Clock


    private class ClockTimeProvider implements TimeProvider {

        private Clock clock;

        public ClockTimeProvider(Clock clock) {

            this.clock = clock;
        }

        @Override
        public long getTime() throws TimeProviderException {

            return Instant.now(this.clock).getEpochSecond();
        }
    }
samdjstevens commented 4 years ago

Thanks for the suggestion - I can see the use that this could have in testing, but I'm wondering if a user would not just mock a TimeProvider in this case? Could you elabroate more on the non testing use case?