/* * This file is part of Socialvoid.NET Project (https://github.com/Intellivoid/Socialvoid.NET). * Copyright (c) 2021 Socialvoid.NET Authors. * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this source code of library. * If not, see . */ /* * Credits to Devin Martin and the original OtpSharp library: * https://github.com/kspearrin/Otp.NET */ using System; namespace Socialvoid.Security.Otp { /// /// Class to apply a correction factor to the system time. /// since: v0.0.0 /// /// /// In cases where the local system time is incorrect it is preferable to simply correct the system time. /// This class is provided to handle cases where it isn't possible for the client, the server, or both, to be on the correct time. /// /// This library provides limited facilities to to ping NIST for a correct network time. This class can be used manually however in cases where a server's time is off /// and the consumer of this library can't control it. In that case create an instance of this class and provide the current server time as the correct time parameter /// /// This class is immutable and therefore threadsafe. /// public class TimeCorrection { //------------------------------------------------- #region Properties Region /// /// Applies the correction factor to the current system UTC time and /// returns a corrected time. /// since: v0.0.0 /// public DateTime CorrectedUtcNow { get => GetCorrectedTime(DateTime.UtcNow); } /// /// The timespan that is used to calculate a corrected time. /// since: v0.0.0 /// public TimeSpan CorrectionFactor { get => _timeCorrectionFactor; } #endregion //------------------------------------------------- #region static field's Region /// /// An instance that provides no correction factor. /// since: v0.0.0 /// public static readonly TimeCorrection UncorrectedInstance = new(); #endregion //------------------------------------------------- #region field's Region /// /// The timespan that is used as a correction factor. /// since: v0.0.0 /// private readonly TimeSpan _timeCorrectionFactor; #endregion //------------------------------------------------- #region Constructor's Region /// /// Constructor used solely for the static /// field to provide an instance without a correction factor. /// since: v0.0.0 /// private TimeCorrection() { _timeCorrectionFactor = TimeSpan.FromSeconds(0); } /// /// Creates a corrected time object by providing the known correct /// current UTC time. /// The current system UTC time will be used as the reference. /// since: v0.0.0 /// /// /// This overload assumes UTC. /// If a base and reference time other than UTC are required then use the /// other overlaod. /// /// The current correct UTC time public TimeCorrection(DateTime correctUtc) { _timeCorrectionFactor = DateTime.UtcNow - correctUtc; } /// /// Creates a corrected time object by providing the known correct current time /// and the current reference time that needs correction. /// since: v0.0.0 /// /// /// The current correct time. /// /// /// The current reference time (time that will have the correction factor /// applied in subsequent calls). /// public TimeCorrection(DateTime correctTime, DateTime referenceTime) { _timeCorrectionFactor = referenceTime - correctTime; } #endregion //------------------------------------------------- #region Get Method's Region /// /// Applies the correction factor to the reference time and returns a /// corrected time. /// since: v0.0.0 /// /// /// The reference time. /// /// /// The reference time with the correction factor applied. /// public DateTime GetCorrectedTime(DateTime referenceTime) => referenceTime - _timeCorrectionFactor; #endregion //------------------------------------------------- } }