Search code examples
securityauthenticationhttp-redirectcouchdbaccess-denied

CouchDB Access Denied Redirect


When I punch in the URL for a secured database it displays the following message on the page:

{"error":"unauthorized","reason":"You are not authorized to access this db."}

Although this message certainly gets the point across I would prefer to redirect the user to a different page, like a login page. I've tried changing the authentication_redirect option in the couch config but no success. How would I go about doing this?


Solution

  • Authentication redirect is only works if client explicitly accepts text/html content type (e.g. sends Accept: text/html header):

    GET /db HTTP/1.1
    Accept: text/html
    Host: localhost:5984
    

    In this case, CouchDB will send HTTP 302 response instead of HTTP 401 which redirects on your authentication form, specified with authentication_redirect configuration option:

    HTTP/1.1 302 Moved Temporarily
    Cache-Control: must-revalidate
    Content-Length: 78
    Content-Type: text/plain; charset=utf-8
    Date: Tue, 24 Sep 2013 01:32:40 GMT
    Location: http://localhost:5984/_utils/session.html?return=%2Fdb&reason=You%20are%20not%20authorized%20to%20access%20this%20db.
    Server: CouchDB/1.4.0 (Erlang OTP/R16B01)
    
    {"error":"unauthorized","reason":"You are not authorized to access this db."}
    

    Othewise CouchDB doesn't know was request send by human from browser or by application library. In the last case redirecting to the HTML form instead of raising HTTP error isn't suitable solution.