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 VerifySucceedsOnKeysCreatedByCreate() { // Arrange var apiKey = ApiKeyV4.Create(); ApiKeyV4.TryParse(apiKey.PlaintextApiKey, out var parsedApiKey); // Act & Assert Assert.True(parsedApiKey.Verify(apiKey.HashedApiKey)); }
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); }
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); }
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); }