Esempio n. 1
0
        /// <summary>
        /// Validates the input token
        /// </summary>
        /// <param name="token"></param>
        /// <param name="requestUrl"></param>
        /// <param name="apiKey"></param>
        /// <param name="timestamp"></param>
        public static bool ValidateToken(string token, string requestUrl, ProjectAuthKey apiKey, out DateTime timestamp)
        {
            if (apiKey is null)
            {
                throw new ArgumentNullException(nameof(apiKey));
            }

            //assign MinValue to expire immediately if the TryParse were to fail
            timestamp = DateTime.MinValue;

            var decodedToken = GetDecodedToken(token, requestUrl);

            double timeStampDouble;

            if (double.TryParse(decodedToken.Timestamp, NumberStyles.Any, CultureInfo.InvariantCulture, out timeStampDouble))
            {
                timestamp = timeStampDouble.FromUnixTime();
            }

            //generate a signature and verify it matches the token signature
            var validationSignature = GetSignature(requestUrl, decodedToken.Timestamp, decodedToken.Nonce, apiKey.AuthKey);

            if (validationSignature != decodedToken.RequestSignature)
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Create a new auth key for the member/project which will generate a 256 bit random key
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public ProjectAuthKey CreateAuthKey(int memberId, int projectId, string description = "")
        {
            var key = new ProjectAuthKey
            {
                DateCreated = DateTime.UtcNow,
                AuthKey     = GenerateKey(32),// generate a 256 bit random key
                MemberId    = memberId,
                ProjectId   = projectId,
                Description = description
            };

            _dbContext.Database.Save(key);

            return(key);
        }