Sha256: 6103a46a8e141d252098778091135907c3d15f5ebd89ceb847aca918a7227008

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'
require 'sinatra/multi_route'

name = "/C=US/ST=SomeState/L=SomeCity/O=Organization/OU=Unit/CN=localhost"
ca   = OpenSSL::X509::Name.parse(name)
key = OpenSSL::PKey::RSA.new(1024)
crt = OpenSSL::X509::Certificate.new
crt.version = 2
crt.serial  = 1
crt.subject = ca
crt.issuer = ca
crt.public_key = key.public_key
crt.not_before = Time.now
crt.not_after  = Time.now + 1 * 365 * 24 * 60 * 60 # 1 year
crt.sign(key, OpenSSL::Digest::SHA1.new)
webrick_options = {
    :Port               => 8443,
    :SSLEnable          => true,
    :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
    :SSLCertificate     => crt,
    :SSLPrivateKey      => key,
    :SSLCertName        => [[ "CN", WEBrick::Utils::getservername ]],
}

class SslServer < Sinatra::Base
  register Sinatra::MultiRoute

  get '/ping' do
    "1"
  end

  route :get, :post, :put, :delete, "/bands/bodyjar" do
    %{{"name": "Bodyjar"}}
  end

  # FIXME: redundant with server.rb.
  helpers do
    def protected!
      return if authorized?
      headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
      halt 401, "Not authorized\n"
    end

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

  get "/protected" do
    protected!

    %{{"name": "Bodyjar"}}
  end
end
server = ::Rack::Handler::WEBrick

server.run(SslServer, webrick_options)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
roar-1.1.1 test/integration/ssl_server.rb
roar-1.1.0 test/integration/ssl_server.rb
roar-1.0.4 test/integration/ssl_server.rb
roar-1.0.3 test/integration/ssl_server.rb
roar-1.0.2 test/integration/ssl_server.rb