Module: Sinatra::BrowserID

Defined in:
lib/sinatra/browserid.rb

Defined Under Namespace

Modules: Helpers, Templates

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

module Helpers



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sinatra/browserid.rb', line 65

def self.registered(app)
  app.helpers BrowserID::Helpers

  app.set :browserid_url, "https://browserid.org"
  app.set :browserid_login_button, :red
  app.set :browserid_login_url, "/_browserid_login"

  app.get '/_browserid_login' do
    # TODO(petef): render a page that initiates login without
    # waiting for a user click.
    
  end

  app.post '/_browserid_assert' do
    # TODO(petef): do verification locally, without a callback
    audience = request.host_with_port
    bid_uri = URI.parse(settings.browserid_url)
    http = Net::HTTP.new(bid_uri.host, bid_uri.port)
    http.use_ssl = true
    data = {
      "assertion" => params[:assertion],
      "audience" => audience,
    }
    data_str = data.collect { |k, v| "#{k}=#{v}" }.join("&")
    res, body = http.post("/verify", data_str)

    # TODO: check res is a 200
    verify = JSON.parse(body) || nil
    if verify.nil?
      # JSON parsing error
      return
    end

    if verify["status"] != "okay"
      $stderr.puts "status was not OK. #{verify.inspect}"
      return
    end

    session[:browserid_email] = verify["email"]
    session[:browserid_expires] = verify["expires"].to_f / 1000

    redirect params[:redirect] || "/"
  end
end