Skip to content

Instantly share code, notes, and snippets.

@zerobearing2
Created April 20, 2012 19:40
Show Gist options
  • Select an option

  • Save zerobearing2/2431247 to your computer and use it in GitHub Desktop.

Select an option

Save zerobearing2/2431247 to your computer and use it in GitHub Desktop.

Revisions

  1. zerobearing2 revised this gist Apr 20, 2012. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions 401_json.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@
    # Overload rails 3.2.x default HTTP Baisc 401 response
    # to JSON instead of plain text/html.
    #
    # Examples
    #
    # require 'json_401'
    # class ApiController < ActionController::Metal
    # ...
    # end
    #
    module ActionController::HttpAuthentication::Basic

    def authentication_request(controller, realm)
  2. zerobearing2 renamed this gist Apr 20, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. zerobearing2 created this gist Apr 20, 2012.
    10 changes: 10 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    module ActionController::HttpAuthentication::Basic

    def authentication_request(controller, realm)
    controller.headers["WWW-Authenticate"] = %(Basic realm="#{realm.gsub(/"/, "")}")
    controller.response.headers["Content-Type"] = "application/json"
    controller.response_body = {error: "Access denied."}.to_json
    controller.status = 401
    end

    end