Refactoring API key authentication