/// <summary> /// Attempts to authenticate a IoT application using an API key. /// </summary> /// <param name="data">The <see cref="ApiKeyData"/> needed to authentication as a JSON string.</param> /// <param name="subject">The subject, which is the API key ID.</param> /// <param name="roles">No roles will be set using this authentication method.</param> /// <param name="rights">The rights associated to the API key.</param> /// <returns>Returns whether authentication was successful.</returns> /// <exception cref="MalformedAuthenticationDataException">Thrown if the passed data doesn't match the expected model.</exception> public bool TryAuthenticate(string data, out string subject, out IEnumerable <Role> roles, out IEnumerable <Right> rights) { ApiKeyData apiKeyData = ParseData <ApiKeyData>(data); // Initialize out-parameters subject = null; roles = new List <Role>(); rights = null; // Look up key ApiKey key = ApiKeyRepository.GetApiKey(apiKeyData.ApiKey); if (key == null) { return(false); } subject = key.Id.ToString(); rights = key.Rights; // Check for status if (!key.Enabled) { return(false); } // All checks passed! return(true); }
/// <summary> /// Gets an API key by it's ID. /// </summary> /// <param name="id">ID of the API key to get.</param> /// <returns>Returns the API key if found.</returns> /// <exception cref="EntityNotFoundException">Thrown if no matching API key could be found.</exception> public async Task <ApiKey> GetApiKey(Guid id) { ApiKey key = await ApiKeyRepository.GetApiKey(id); if (key == null) { throw new EntityNotFoundException("ApiKey", id); } return(key); }
/// <summary> /// Attempts to get an API key from the underlying repository and throws a <see cref="ApiKeyNotFoundException"/> if no matching key could be found. /// </summary> /// <param name="id">ID of the key to get.</param> /// <exception cref="ApiKeyNotFoundException">Thrown if no matching key could be found.</exception> /// <returns>Returns the key, if found.</returns> private ApiKey GetApiKeyOrThrowNotFoundException(Guid id) { ApiKey key = ApiKeyRepository.GetApiKey(id); // Check for key existence if (key == null) { throw new ApiKeyNotFoundException(id); } return(key); }