Rewrite AuthMiddleware
and credential caching
#2984
Merged
AuthMiddleware
and credential caching
#2984