Class: Warden::BrowserId::Strategy

Inherits:
Strategies::Base
  • Object
show all
Defined in:
lib/warden-browserid/strategy.rb

Overview

A Warden Strategy to authenticate with BrowserId from Mozilla

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.browserid_urlObject

Select BrowserID verifier instance from options



38
39
40
41
42
43
44
# File 'lib/warden-browserid/strategy.rb', line 38

def self.browserid_url
  begin
    request.env['warden'].config.browserid_url
  rescue NameError
    "dev.diresworb.org"
  end
end

Instance Method Details

#authenticate!Object

Authenticate user if email was verified



32
33
34
35
# File 'lib/warden-browserid/strategy.rb', line 32

def authenticate!
  u = User.find_by_email(@asserted["email"])
  u.nil? ? fail!("The given email is not registered for this system.") : success!(u)
end

#valid?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/warden-browserid/strategy.rb', line 10

def valid?
  # Not valid when the assertion parameter is missing
  return false unless params[:assertion]

  # Prepare the HTTP request
  http = Net::HTTP.new(Strategy.browserid_url, 443)
  http.use_ssl = true
  req = Net::HTTP::Post.new("/verify")
  req.set_form_data( { assertion: params[:assertion], audience: request.host_with_port } )

  # POST args to verifier and get response
  response = http.request(req)

  json = JSON.parse response.body
  @asserted = json
  puts @asserted

  # Return true if asserted email and audience is right
  json["status"] == "okay" and json["audience"] == request.host_with_port
end