Module Capcode::Helpers::Authorization
In: lib/capcode/helpers/auth.rb

Because this helper was trully inspired by this post : www.gittr.com/index.php/archive/sinatra-basic-authentication-selectively-applied/ and because the code in this post was extracted out of Wink, this file follow the Wink license :

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Methods

Public Instance methods

Allow you to add and HTTP Authentication (Basic or Digest) to a controller

Options :

  • :type : Authentication type (:basic or :digest) - default : :basic
  • :realm : realm ;) - default : "Capcode.app"
  • :opaque : Your secret passphrase. You MUST set it if you use Digest Auth - default : "opaque"

The block must return a Hash of username => password like that :

  {
    "user1" => "pass1",
    "user2" => "pass2",
    # ...
  }

[Source]

     # File lib/capcode/helpers/auth.rb, line 105
105:       def http_authentication( opts = {}, &b )
106:         @auth = nil
107:         
108:         @auth_type = opts[:type]||:basic
109:         realm = opts[:realm]||"Capcode.app"
110:         opaque = opts[:opaque]||"opaque"
111:         @authorizations = b
112:         
113:         return if authorized?
114:         
115:         if @auth_type == :basic
116:           basic_unauthorized!(realm) unless auth.provided?
117:           bad_request! unless auth.basic?
118:           basic_unauthorized!(realm) unless basic_authorize(*auth.credentials)
119:         else
120:           digest_unauthorized!(realm, opaque) unless auth.provided?
121:           bad_request! unless auth.digest?
122:           digest_unauthorized!(realm, opaque) unless digest_authorize
123:         end        
124:         
125:         request.env['REMOTE_USER'] = auth.username
126:       end

[Validate]