【问题标题】:Thin, Sinatra, and intercepting static file request to do CAS authenticationThin、Sinatra、拦截静态文件请求做CAS认证
【发布时间】:2010-04-24 03:57:23
【问题描述】:

我正在使用 casrack-the-authenticator gem 进行 CAS 身份验证。我的服务器在 Sinatra 之上运行 Thin。我已经让 CAS 身份验证位工作了,但我不确定如何告诉 Rack 拦截“/index.html”请求以确认 CAS 登录,如果不允许用户查看页面,则返回 HTTP 403 响应而不是提供实际页面。这个事情谁有经验?谢谢。

我的应用:

class Foo < Sinatra::Base
    enable :sessions
    set :public, "public"
    use CasrackTheAuthenticator::Simple, :cas_server => "https://my.cas_server.com"
    use CasrackTheAuthenticator::RequireCAS

    get '/' do
        puts "Hello World"
    end
end

我的机架文件:

require 'foo'

use Rack::CommonLogger
use Rack::Lint

run Foo

最初尝试让 Rack 了解其文件服务中的身份验证(欢迎 cmets 和想法):

builder = Rack::Builder.new do
    map '/foo/index.html' do
        run Proc.new { |env|
            user = Rack::Request.new(env).session[CasrackTheAuthenticator::USERNAME_PARAM]
            [401, { "Content-Type" => "text/html" }, "CAS Authentication Required"] unless user
            # Serve index.html because we detected user
         }
    end

    map '/foo' do
        run Foo
    end
end

run builder

【问题讨论】:

    标签: ruby sinatra rack cas


    【解决方案1】:

    Casrack-the-Authenticator 会将 CAS 信息放入 Rack 会话中。您可以在另一个 Rack 中间件或您的 Sinatra 应用程序中将其提取出来。

    以下是 Rails 应用程序,但概念与 Sinatra 或 Rack 中间件类似:

    # in app/controllers/application_controller.rb:
    protected
    
    def require_sign_in!
      render :nothing => true, :status => 403 unless signed_in?
    end
    
    def signed_in?
      current_user.present?
    end
    
    def current_user
      @current_user ||= Person.find_by_username(session[CasrackTheAuthenticator::USERNAME_PARAM])
    end
    

    【讨论】:

    • 感谢您的信息,这段代码很有意义。我不确定的是挂接到静态内容服务器的 Rack 等价物。例如,来自浏览器的 JSON 请求被正确拒绝,但静态文件服务似乎绕过了我的 Sinatra 应用程序并提供了 index.html。
    • 你在使用Rack::Static吗?如果是这样,它是在 Casrack 中间件背后吗?也许显示你的机架文件会有所帮助。
    • 我之前没有使用过Rack::Static。实际上,我将 casrack 放入了扩展 Sinatra::Base 的类中,因为出于某种原因,我无法将它放在我的机架中并让它与 enable :sessions 一起使用(不过,我可能错过了一个简单的语法或配置选项)。我基本上发布了我的 rackup 和 Sinatra 应用程序目前的样子。
    • 我认为会话的问题是 Casrack 需要在运行之前启用会话,所以你不能把它放在 Sinatra 之外。是否可以使用 Rack::Session::Cookie 代替 Sinatra 的版本?
    • 当然,我可以试试。会话管理部分不如 403 响应的静态内容重定向重要。我将我在主要部分中的内容发布为挂钩到 Rack 服务器的初步尝试。
    猜你喜欢
    • 2021-06-16
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多