Refactoring of API authorize method