Sha256: d3444651738fc0b0976e71934ee7d1085639b8f41c7dd4d19aa96162a3af3b54

Contents?: true

Size: 952 Bytes

Versions: 5

Compression:

Stored size: 952 Bytes

Contents

module Merb
  
  module Authentication
    require 'base64'
    
    def credentials
      if d = %w{REDIRECT_X_HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION
             X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION}.
             inject([]) { |d,h| @env.has_key?(h) ? @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::Server.basic_auth[:username] and password == Merb::Server.basic_auth[:password]
    end
    
    def basic_authentication
      if !authenticated?
        throw :halt, :access_denied
      end
    end
      
    def access_denied
      @status = 401
      @headers['Content-type'] = 'text/plain'
      @headers['Status'] = 'Unauthorized'
      @headers['WWW-Authenticate'] = "Basic realm=\"#{Merb::Server.basic_auth[:domain]}\""
      return 'Unauthorized'
    end  
      
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
merb-0.3.3 lib/merb/mixins/basic_authentication_mixin.rb
merb-0.2.0 lib/merb/mixins/basic_authentication_mixin.rb
merb-0.3.0 lib/merb/mixins/basic_authentication_mixin.rb
merb-0.3.1 lib/merb/mixins/basic_authentication_mixin.rb
merb-0.3.4 lib/merb/mixins/basic_authentication_mixin.rb