There is a good example of this in XML::Atom, Ben Trott's Perl implementation of the Atom API. It includes a skeleton server that handles the API (including authentication), with hooks to build the rest of your application on top. It can run as a CGI in a non-.htaccess environment. Check it out.
But it does mean the authentication happens at the CGI level, right?
I think I was confused because you said "the server will return 401" and I guess I assumed the web server but presumably you were meaning the CGI based Atom server right?
I just wanted to get this clear because obviously that would mean no other files in that specific directory are protected.
I don't think this tries to protect any files on the web server; I think this authentication only keeps the CGI script from doing things that someone other than Bob ask it to do.
I believe the CGI script actually sends the 401 header.
When the Atom client responds, the meat of its response is in the X-WSSE header which Apache passes to the CGI script.