anchor.diff:                   _ ->
                        {done, Req:respond({501, [], []})}
                end,
  -    CheckAuth = fun (F, Args) ->
  -                        case check_auth(extract_basic_auth(Req)) of
  -                            true -> apply(F, Args ++ [Req]);
  -                            _ -> Req:respond({401, [{"WWW-Authenticate",
  -                                                     "Basic realm=\"api\""}], []})
  -                        end
  -                end,
  +    CheckAuth =
  +        fun (F, Args) ->
  +                UserPassword = extract_basic_auth(Req),
  +                case check_auth(UserPassword) of
  +                    true -> apply(F, Args ++ [Req]);
  +                    _ -> Req:respond({401, [{"WWW-Authenticate",
  +                                             "Basic realm=\"api\""}],
  +                                      []})
  +                end
  +        end,
  +    CheckAuthBucket =
  +        fun (F, Args) ->
  +                UserPassword = extract_basic_auth(Req),
  +                case check_bucket_auth(UserPassword) of
  +                    true -> apply(F, Args ++ [Req]);
  +                    _    -> CheckAuth(F, Args)
  +                end
  +        end,
       case Action of
           {done, RV} -> RV;
           {need_auth, F} -> CheckAuth(F, []);
  -        {need_auth, F, Args} -> CheckAuth(F, Args)
  +        {need_auth, F, Args} -> CheckAuth(F, Args);
  +        {need_auth_bucket, F} -> CheckAuthBucket(F, []);
  +        {need_auth_bucket, F, Args} -> CheckAuthBucket(F, Args)
       end.
   
   %% Internal API
  
positive.code: %   ]}
  % ]}
  
  build_pool_info(Id) ->
      MyPool = find_pool_by_id(Id),
      Buckets = expect_prop_value(buckets, MyPool),
      Nodes = build_nodes_info(MyPool, true),
      BucketsInfo = [{struct, [{uri, list_to_binary("/pools/" ++ Id ++ "/buckets/" ++ Name)},
                               {name, list_to_binary(Name)}]}
                     || Name <- proplists:get_keys(Buckets)],
      {struct, [{name, list_to_binary(Id)},
  
negative.code:                      case PathTokens of
                           [] -> {done, redirect_permanently("/index.html", Req)};
                           ["pools"] ->
                               {need_auth_bucket, fun handle_pools/1};
                           ["pools", Id] ->
                               {need_auth_bucket, fun handle_pool_info/2, [Id]};
                           ["pools", Id, "stats"] ->
                               {need_auth, fun handle_bucket_stats/3, ["asd", Id]};
                           ["poolsStreaming", Id] ->
                               {need_auth, fun handle_pool_info_streaming/2, [Id]};
                           ["pools", PoolId, "buckets", Id] ->
                               {need_auth_bucket, fun handle_bucket_info/3, [PoolId, Id]};
                           ["pools", PoolId, "buckets", Id, "stats"] ->
                               {need_auth, fun handle_bucket_stats/3, [PoolId, Id]};
                           ["alerts"] ->
  