-
Notifications
You must be signed in to change notification settings - Fork 324
How to: Add authentication
pushmatrix edited this page Nov 29, 2012
·
14 revisions
Each time you visit a dashboard, a method called protected!
gets called. For all new Dashing projects, this method does nothing. You can override this behaviour in the config.ru
file.
helpers do
def protected!
# Put any authentication code you want in here.
# This method is run before accessing any resource.
end
end
Add the following to your config.ru
file
helpers do
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Restricted Area")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'admin']
end
end
Make sure to add the following to your Gemfile.
gem 'omniauth-google-apps'
Here is a sample config.ru
file that enables Google Apps auth.
require 'openid/store/filesystem'
require 'omniauth/strategies/google_apps'
require 'dashing'
configure do
set :auth_token, 'YOUR_AUTH_TOKEN'
helpers do
def protected!
redirect '/auth/g' unless session[:user_id]
end
end
use Rack::Session::Cookie
use OmniAuth::Builder do
provider :google_apps, :store => OpenID::Store::Filesystem.new('./tmp'), :name => 'g', :domain => 'YOURDOMAIN.com'
end
post '/auth/g/callback' do
if auth = request.env['omniauth.auth']
session[:user_id] = auth['info']['email']
redirect '/'
else
redirect '/auth/failure'
end
end
get '/auth/failure' do
'Nope.'
end
end
map Sinatra::Application.assets_prefix do
run Sinatra::Application.sprockets
end
run Sinatra::Application
- Home
- Dashing Workshop
- Installation
- Widgets
- Configuration
- Security
- Troubleshooting
- Deploying dashboards
- How Tos
- How to: post data to your dashboard and widgets
- How to: Define a data model and store history data to database
- How to: Prevent a job from overlapping with itself
- How to: Send HTML data to your widgets
- How to: Send mysql data to your widgets
- How to: Setup a Graph
- How to: Store data to and display from database
- How to: Update a Dashboard using a spreadsheet
- How to: update dashboard in Django
- How to: Update font awesome fonts from version 3 to 4
- How to: Use New Relic with Dashing
- How to: precompile assets
- Development