Module: Sinatra::BrowserID::Helpers

Defined in:
lib/sinatra/browserid/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authorize!(redirect: nil) ⇒ Object

If the current user is not logged in, redirects to a login page. Override the login page by setting the Sinatra option :browserid_login_url.



13
14
15
16
17
18
# File 'lib/sinatra/browserid/helpers.rb', line 13

def authorize!(redirect: nil)
  session[:authorize_redirect_url] = request.url
  session[:authorize_redirect_url] = redirect if redirect
   = settings.
  redirect  unless authorized?
end

#authorized?Boolean

Returns true if the current user has logged in and presented a valid assertion.

Returns:

  • (Boolean)


6
7
8
# File 'lib/sinatra/browserid/helpers.rb', line 6

def authorized?
  ! session[:browserid_email].nil?
end

#authorized_emailObject

Returns the BrowserID verified email address, or nil if the user is not logged in.



27
28
29
# File 'lib/sinatra/browserid/helpers.rb', line 27

def authorized_email
  session[:browserid_email]
end

#logout!Object

Logs out the current user.



21
22
23
# File 'lib/sinatra/browserid/helpers.rb', line 21

def logout!
  session[:browserid_email] = nil
end

#normalize_email(email) ⇒ Object

Normalize the email like the broker will do it, see github.com/portier/portier.github.io/blob/master/specs/Email-Normalization.md



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sinatra/browserid/helpers.rb', line 33

def normalize_email(email)
  begin
      user, domain = email.split("@")
      if user == nil or user.empty?
          raise ArgumentError.new('user part must not be empty')  
      end 
      user = user.downcase
      domain = SimpleIDN.to_ascii(domain).downcase
      begin
          IPAddr.new(domain)
      rescue
          # if domain could not be parsed as IP we are good
          return user + "@" + domain
      end
      raise ArgumentError.new('domain must not be an IP')  
  rescue Exception => e
      raise ArgumentError, 'Not a valid email adress: ' + e.message
  end
end

#render_login_button(redirect_url = nil) ⇒ Object

Returns the HTML to render the Persona login form. Optionally takes a URL parameter for where the user should be redirected to after the assert POST back.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sinatra/browserid/helpers.rb', line 56

def (redirect_url = nil)
  if session[:authorize_redirect_url]
    redirect_url = session[:authorize_redirect_url]
    session[:authorize_redirect_url] = nil
  end
  redirect_url ||= request.url
  session['redirect_url'] = redirect_url

  if session[:nonce]
      nonce = session[:nonce]
      # Try to limit how many nonces are stored by keeping the session nonce alive
      Cachy.delete_key(nonce)
      Cachy.cache(nonce, expires_in: 600) { true }
  else
      session[:nonce] = nonce = SecureRandom.base64
      Cachy.cache(nonce, expires_in: 600) { true }
  end

  template = ERB.new(Templates::LOGIN_BUTTON)
  template.result(binding)
end