示例#1
0
 public static void Init(AuthTolkenTimeout authTolkenTimeout)
 {
     Debug.Assert(null == AuthString);
     AcceptedAuthTolkenTimeouts = authTolkenTimeout;
     AuthString = GetRandomString();
     s_connectionCounter = new List<int>(0);
 }
示例#2
0
        public static bool IsValid(AuthTolkenTimeout timeout,
                                   string authTolken,
                                   int adjustedAmount,
                                   int clientId,
                                   bool adjustForTransmission)
        {
            bool valid = false;
            // This has to be single threaded to avoid race conditions for authenticating the client packet count
            lock (typeof (TolkenAuthenticator))
            {
                if (AcceptedAuthTolkenTimeouts == AuthTolkenTimeout.NoAuth)
                {
                    valid = true;
                }
                else if (0 < clientId &&
                         clientId <= s_connectionCounter.Count &&
                         AuthTolkenTimeout.Any != timeout &&
                         (AcceptedAuthTolkenTimeouts == AuthTolkenTimeout.Any || AcceptedAuthTolkenTimeouts == timeout))
                {
                    DateTime now = DateTime.UtcNow;
                    int numAdjustSeconds = 0;

                    // We have to round our seconds to the every nth second based on timeout
                    switch (timeout)
                    {
                        case AuthTolkenTimeout.FiveSeconds:
                            numAdjustSeconds = 5 + (5 - (now.Second%5));
                            numAdjustSeconds -= adjustForTransmission && numAdjustSeconds > adjustedAmount ? 5 : 0;
                            break;
                        case AuthTolkenTimeout.TenSecionds:
                            numAdjustSeconds = 10 + (10 - (now.Second%10));
                            numAdjustSeconds -= adjustForTransmission && numAdjustSeconds > adjustedAmount ? 10 : 0;
                            break;
                        case AuthTolkenTimeout.ThirtySeconds:
                            numAdjustSeconds = 30 + (30 - (now.Second%30));
                            numAdjustSeconds -= adjustForTransmission && numAdjustSeconds > adjustedAmount ? 30 : 0;
                            break;
                        case AuthTolkenTimeout.SixtySeconds:
                            numAdjustSeconds = 60 + (60 - (now.Second%60));
                            numAdjustSeconds -= adjustForTransmission && numAdjustSeconds > adjustedAmount ? 60 : 0;
                            break;
                        default:
                            Debug.Assert(false, "Unexpected timeout");
                            break;
                    }

                    DateTime adjustedTime = now.AddSeconds(numAdjustSeconds);
                    // we only want granularity down to seconds
                    var adjustedTimeToSecond = new DateTime(adjustedTime.Year,
                                                            adjustedTime.Month,
                                                            adjustedTime.Day,
                                                            adjustedTime.Hour,
                                                            adjustedTime.Minute,
                                                            adjustedTime.Second);

                    int currentClientCounter = s_connectionCounter[clientId - 1];

                    string computedAuthTolken =
                        Sha1HashOfString(adjustedTimeToSecond.ToString("yyyy-MM-dd:HH:mm:ss") + AuthString +
                                         currentClientCounter);

                    valid = authTolken == computedAuthTolken;

                    if (!valid && !adjustForTransmission)
                    {
                        // lets try one last time adjusting
                        valid = IsValid(timeout, authTolken, adjustedAmount, clientId, true /*adjustForTransmission*/);
                    }
                    else if (valid)
                    {
                        IncrementClient(clientId);
                    }
                }
            }
            return valid;
        }
示例#3
0
 public static bool IsValid(AuthTolkenTimeout timeout, string authTolken, int adjustedAmount, int clientId)
 {
     return IsValid(timeout, authTolken, adjustedAmount, clientId, false /*adjustForTransmission*/);
 }