0
我已經閱讀了關於創建一個OpenSSL證書的教程和文檔,並獲得了與它和Sinatra一起運行的Webbrick服務器。這是所有工作 - 並感謝此前的職位。但是,現在我試圖將其與我的應用程序集成,我似乎失去了解析請求的'before do'代碼,並且允許我將驗證的用戶名從憑據中提取出來。所以,我的基本問題是 - 如何在WebBrick與Sinatra一起運行的同時使用Rack :: Auth :: Basic和HTTPS。任何幫助,將不勝感激。Sinatra與WebBrick和OpenSSL/HTTPS Rack :: Auth :: Basic
#!/usr/local/bin/ruby
require 'sinatra'
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'yaml'
# basic authentication provided through Rack:Auth
configure do
puts "configure do ran"
# load password file - might move to DB at some point
@@config = YAML.load_file(File.join(Dir.pwd, 'config', 'users.yml'))
use Rack::Auth::Basic, "Restricted Area" do |u, p|
puts "use Rack::Auth::Basic"
[u, p] == [u, @@config[:users][u][:password]]
end
end
before do
puts "before do ran"
@auth ||= Rack::Auth::Basic::Request.new(request.env)
puts "auth username: " + @auth.username.to_s
# set the user name for processing in the post or get
@myuser = @auth.username.to_s
end
class MyServer < Sinatra::Base
get '/' do
# code would do something with @myuser here
"Hello, world!"
end
end
pkey = cert = cert_name = nil
begin
pkey = OpenSSL::PKey::RSA.new(File.open("private_key.pem").read)
cert = OpenSSL::X509::Certificate.new(File.open("certificate.pem").read)
end
webrick_options = {
:Port => 8443,
:Logger => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
:DocumentRoot => "/ruby/htdocs",
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLCertificate => cert,
:SSLPrivateKey => pkey,
:SSLCertName => [ [ "CN",WEBrick::Utils::getservername ] ],
:app => MyServer
}
Rack::Server.start webrick_options
再次,任何想法都非常感激。
進一步玩這個,事實證明移動'配置做'和'之前做'到服務器類似乎工作。此外,我已經將Rack :: SslEnforcer添加到代碼中 - 不確定這是否需要,儘管Web服務器僅運行https。 – SBG