Sha256: 2e4b6dc65d1b69e2f78073c4ee7e925e106a5f4739c87f3f9c285147f0d2868e

Contents?: true

Size: 976 Bytes

Versions: 4

Compression:

Stored size: 976 Bytes

Contents

module Merb
  
  module AuthenticationMixin
    require 'base64'
    
    def credentials
      if d = %w{REDIRECT_X_HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION
             X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION}.
             inject([]) { |d,h| request.env.has_key?(h) ? request.env[h].to_s.split : d }
        return Base64.decode64(d[1]).split(':')[0..1] if d[0] == 'Basic'
      end
    end
    
    def authenticated?
      username, password = *credentials
      username == Merb::Config[:basic_auth][:username] and password == Merb::Config[:basic_auth][:password]
    end
    
    def basic_authentication
      if !authenticated?
        throw :halt, :access_denied
      end
    end
      
    def access_denied
      set_status(401)
      headers['Content-type'] = 'text/plain'
      headers['Status'] = 'Unauthorized'
      headers['WWW-Authenticate'] = "Basic realm=\"#{Merb::Config[:basic_auth][:domain]}\""
      return 'Unauthorized'
    end  
      
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
merb-0.5.0 lib/merb/mixins/basic_authentication.rb
merb-0.5.1 lib/merb/mixins/basic_authentication.rb
merb-0.5.2 lib/merb/mixins/basic_authentication.rb
merb-0.5.3 lib/merb/mixins/basic_authentication.rb