Move most of basic_auth handling to a function