Module: Sinatra::Warden

Defined in:
lib/sinatra_warden/sinatra.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sinatra_warden/sinatra.rb', line 68

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

  # Enable Sessions
  unless defined?(Rack::Session::Cookie)
    app.set :sessions, true
  end

  app.set :auth_failure_path, '/'
  app.set :auth_success_path, '/'
  # Setting this to true will store last request URL
  # into a user's session so that to redirect back to it
  # upon successful authentication
  app.set :auth_use_referrer, false

  app.set :auth_error_message,   "Could not log you in."
  app.set :auth_success_message, "You have logged in successfully."
  app.set :auth_template_renderer, :haml
  app.set :auth_login_template, :login

  # OAuth Specific Settings
  app.set :auth_use_oauth, false

  app.post '/unauthenticated/?' do
    status 401
    warden.custom_failure! if warden.config.failure_app == self.class
    env['x-rack.flash'][:error] = settings.auth_error_message if defined?(Rack::Flash)
    self.send(settings.auth_template_renderer, settings.)
  end

  app.get '/login/?' do
    if settings.auth_use_oauth && !@auth_oauth_request_token.nil?
      session[:request_token] = @auth_oauth_request_token.token
      session[:request_token_secret] = @auth_oauth_request_token.secret
      redirect @auth_oauth_request_token.authorize_url
    else
      self.send(settings.auth_template_renderer, settings.)
    end
  end

  app.get '/oauth_callback/?' do
    if settings.auth_use_oauth
      authenticate
      env['x-rack.flash'][:success] = settings.auth_success_message if defined?(Rack::Flash)
      redirect settings.auth_success_path
    else
      redirect settings.auth_failure_path
    end
  end

  app.post '/login/?' do
    authenticate
    env['x-rack.flash'][:success] = settings.auth_success_message if defined?(Rack::Flash)
    redirect settings.auth_use_referrer && session[:return_to] ? session.delete(:return_to) : settings.auth_success_path
  end

  app.get '/logout/?' do
    authorize!
    logout
    env['x-rack.flash'][:success] = settings.auth_success_message if defined?(Rack::Flash)
    redirect settings.auth_success_path
  end
end