【问题标题】:Is Rack::Auth identical to basic HTTP authentication?Rack::Auth 与基本 HTTP 身份验证相同吗?
【发布时间】:2011-11-04 09:11:42
【问题描述】:

我正在使用来自Sinatra docs 的以下代码限制对我的 Sinatra 应用程序设置页面的访问。

helpers do 
  def protected!
    unless authorized?
      response['WWW-Authenticate'] = %(Basic realm="Access restricted")
      throw(:halt, [401, "Login incorrect\n"])
    end
  end

  def authorized?
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'admin']
  end
end

before "/admin" do
  protected!
end

Rack::Auth 是否与 .htaccess 基本身份验证相同?

还有什么我可以或应该做的来保护它吗?

【问题讨论】:

    标签: ruby http sinatra rack


    【解决方案1】:

    是的,它是一样的。您可以使用 Digest auth,或者如果您想坚持使用 Basic,您可以确保它使用 SSL。

    基本和摘要示例:

    https://github.com/sinatra/sinatra-book-contrib/blob/master/middleware/rack_auth_basic_and_digest.md

    带有基本示例应用的 HTTPS:

    ./config.ru

    require 'rubygems'
    require 'sinatra'
    require 'haml'
    
    require './app'
    
    run App
    

    ./app.rb

    class App < Sinatra::Application
    
      configure do
        set :haml, :format => :html5
        set :root, File.dirname(__FILE__)
        # more config stuff, db, mailers, file storage etc...
      end
    
    end
    
    # HELPERS
    require 'helpers/helpers'
    
    # CONTROLLER
    require 'controller/admin'
    

    ./helpers/helpers.rb

    module Sinatra
      module RegexpRouteFilter
        def before_with_regexp(pattern, &blk)
          before do
            instance_eval(&blk) if request.path =~ pattern
          end
        end
      end
    
      register RegexpRouteFilter
    end
    
    class App < Sinatra::Application
      helpers do
        def protected!
          unless authorized?
            response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
            throw(:halt, [401, "Not authorized\n"])
          end
        end
    
        def authorized?
          @auth ||=  Rack::Auth::Basic::Request.new(request.env)
          @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['user', 'pass']
        end
      end
    
      before_with_regexp(/^\/admin/) do
        if settings.environment == :production
          unless (@env['HTTP_X_FORWARDED_PROTO'] || @env['rack.url_scheme']) == 'https'
            redirect "https://#{request.env['HTTP_HOST']}#{request.env["REQUEST_PATH"]}"
          end
        end
        protected!
      end
    end
    

    ./controller/admin.rb

    class App < Sinatra::Application
    
      get '/admin' do
        haml :"admin/index"
      end
    
    end
    

    ./views/admin/index.haml

    %h1 Admin
    %p Welcome!
    

    然后使用 shotgun gem shotgun config.ru -p 4567 运行应用程序

    【讨论】:

    • 非常有趣。谢谢。也感谢对​​“before_with_regexp”的介绍。为什么要将整个东西包装在“App”类中并添加 Sinatra::Partials?
    • 糟糕,从我的一个应用程序中复制了代码,我将进行编辑以删除 helpers Sinatra::Partials 并显示 before_with_regexp 模块。将代码包装在 app 类中,可以让 app 跨不同的文件进行拆分,类似于 stackoverflow.com/questions/5015471/…
    猜你喜欢
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2021-03-21
    • 2011-11-12
    • 2011-05-05
    • 1970-01-01
    相关资源
    最近更新 更多