Example #1
0
        /// <summary>
        /// Creates a random ApiKey V4.
        /// </summary>
        public static ApiKeyV4 Create()
        {
            var apiKey = new ApiKeyV4();

            apiKey.CreateInternal();

            return(apiKey);
        }
        public void VerifyFailsForIllegalInput(string hashedApiKey)
        {
            // Arrange
            var apiKey = ApiKeyV4.Create();

            // Act & Assert
            Assert.False(apiKey.Verify(hashedApiKey));
        }
        public void VerifySucceedsOnValidApiKeys(string inputApiKey, string hashedApiKey)
        {
            // Arrange
            Assert.True(ApiKeyV4.TryParse(inputApiKey, out var apiKey));

            // Act & Assert
            Assert.True(apiKey.Verify(hashedApiKey));
        }
        public void TryParseFailsForIllegalApiKeys(string inputApiKey)
        {
            // Act
            bool result = ApiKeyV4.TryParse(inputApiKey, out var apiKey);

            // Assert
            Assert.False(result);
        }
        public void VerifyFailsOnIncorrectHashedKey()
        {
            // Arrange
            var apiKey       = ApiKeyV4.Create();
            var hashedApiKey = "oy2iuvoucviouojnrbzaAEAAAAABAAACOEAAAAABAMHUIIUXSFAIOTEAFWF3K2E7L6YRUDOZDPHKSRX3YEOFUWPCID325EZH5JXBBGBMYBECT4KCGMWOQQ======";

            // Act & Assert
            Assert.False(apiKey.Verify(hashedApiKey));
        }
        public void VerifySucceedsOnKeysCreatedByCreate()
        {
            // Arrange
            var apiKey = ApiKeyV4.Create();

            ApiKeyV4.TryParse(apiKey.PlaintextApiKey, out var parsedApiKey);

            // Act & Assert
            Assert.True(parsedApiKey.Verify(apiKey.HashedApiKey));
        }
        public Credential CreateApiKey(TimeSpan?expiration, out string plaintextApiKey)
        {
            var apiKey = ApiKeyV4.Create();

            plaintextApiKey = apiKey.PlaintextApiKey;

            return(new Credential(
                       CredentialTypes.ApiKey.V4,
                       apiKey.HashedApiKey,
                       expiration: expiration));
        }
        public void TryParseSucceedsForValidApiKeys(string inputApiKey, string idPart, string passwordPart)
        {
            // Act
            bool result = ApiKeyV4.TryParse(inputApiKey, out var apiKey);

            // Assert
            Assert.True(result);
            Assert.Equal(inputApiKey, apiKey.PlaintextApiKey);
            Assert.Equal(idPart, apiKey.IdPart);
            Assert.Equal(passwordPart, apiKey.PasswordPart);
            Assert.Null(apiKey.HashedApiKey);
        }
Example #9
0
        public IList <Credential> GetValidCredentialsForApiKey(IQueryable <Credential> allCredentials, string providedApiKey)
        {
            var results = new List <Credential>();

            if (ApiKeyV4.TryParse(providedApiKey, out ApiKeyV4 apiKeyV4))
            {
                var foundApiKeys = allCredentials.Where(c => c.Type == CredentialTypes.ApiKey.V4 &&
                                                        c.Value.StartsWith(apiKeyV4.IdPart)).ToList();

                // There shouldn't be duplications in the id part because it's long enough, but we shouldn't assume that.
                results = foundApiKeys.Where(c => apiKeyV4.Verify(c.Value)).ToList();
            }
            else
            {
                // Try to authenticate as APIKey V1/V2/V3/Verify
                if (ApiKeyV3.TryParse(providedApiKey, out var v3ApiKey))
                {
                    results = allCredentials.Where(c => c.Type.StartsWith(CredentialTypes.ApiKey.Prefix) &&
                                                   (c.Value == providedApiKey || c.Value.StartsWith(v3ApiKey.IdPart))).ToList();

                    results = results.Where(credential =>
                    {
                        switch (credential.Type)
                        {
                        case CredentialTypes.ApiKey.V1:
                        case CredentialTypes.ApiKey.V2:
                        case CredentialTypes.ApiKey.VerifyV1:
                            {
                                return(credential.Value == providedApiKey);
                            }

                        case CredentialTypes.ApiKey.V3:
                            {
                                return(v3ApiKey.Verify(credential.Value));
                            }

                        default:
                            {
                                return(false);
                            }
                        }
                    }).ToList();
                }
            }

            return(results);
        }
Example #10
0
        public IList <Credential> GetValidCredentialsForApiKey(IQueryable <Credential> allCredentials, string providedApiKey)
        {
            List <Credential> results;

            if (ApiKeyV4.TryParse(providedApiKey, out ApiKeyV4 apiKeyV4))
            {
                var foundApiKeys = allCredentials.Where(c => c.Type == CredentialTypes.ApiKey.V4 &&
                                                        c.Value.StartsWith(apiKeyV4.IdPart)).ToList();

                // There shouldn't be duplications in the id part because it's long enough, but we shouldn't assume that.
                results = foundApiKeys.Where(c => apiKeyV4.Verify(c.Value)).ToList();
            }
            else
            {
                results = allCredentials.Where(c => c.Type.StartsWith(CredentialTypes.ApiKey.Prefix) &&
                                               c.Value == providedApiKey).ToList();
            }

            return(results);
        }
        public void CreatesAValidApiKey()
        {
            // Act
            var apiKey = ApiKeyV4.Create();

            // Assert
            Assert.NotNull(apiKey);
            Assert.NotNull(apiKey.HashedApiKey);
            Assert.Equal(ApiKeyV4.IdAndPasswordHashedLength, apiKey.HashedApiKey.Length);

            Assert.NotNull(apiKey.PlaintextApiKey);
            Assert.Equal(ApiKeyV4.IdAndPasswordLength, apiKey.PlaintextApiKey.Length);
            Assert.Equal(apiKey.PlaintextApiKey.ToLower(), apiKey.PlaintextApiKey);

            Assert.NotNull(apiKey.IdPart);
            Assert.Equal(ApiKeyV4.IdPartBase32Length, apiKey.IdPart.Length);

            Assert.NotNull(apiKey.PasswordPart);
            Assert.Equal(ApiKeyV4.IdAndPasswordLength - ApiKeyV4.IdPartBase32Length, apiKey.PasswordPart.Length);
        }
Example #12
0
 /// <summary>
 /// Parses the provided string and creates an ApiKeyV4 if it's successful.
 /// </summary>
 public static bool TryParse(string plaintextApiKey, out ApiKeyV4 apiKey)
 {
     apiKey = new ApiKeyV4();
     return(apiKey.TryParseInternal(plaintextApiKey));
 }