Sinatra::GAuth
Quickly add Google Apps authentication to any Sintra app.
Installation
Add this line to your application's Gemfile:
gem 'sinatra-g_auth', require: 'sinatra/g_auth'
And then execute:
$ bundle
Usage
Configure the settings and register the extension to get up and running.
class App < Sinatra::Base
register Sinatra::GAuth # add the sinatra extension to your stack
set :gauth_domain, 'example.org' # set this to your google apps domain
set :gauth_tmp_dir, './tmp' # path to a directory that's writable by your web process
set :gauth_redirect, '/' # where to redirect users after they've authenticated
set :gauth_openid_store, OpenID::Store::Memcache.new(Dalli::Client.new) # Pass an instance of OpenID::Store, defaults to Filestore in gauth_tmp_dir
get '/protected' do
protect_with_gauth! # add this to any route you want protected
# ...
end
end
Once the user has authenticated with google, some basic information is added to the session:
session[:_gauth][:id] # => google ID
session[:_gauth][:name] # => user's full name
session[:_gauth][:email] # => google apps email address
If attempting to add protect_with_gauth!
to a before filter, be sure to skip paths that begin with /auth
:
before(%r{^(?!(\/auth))}) do
protect_with_gauth!
end
This extension will enable sessions, but does not configure a session store. Do that with the middleware of your choice (e.g. Rack::Session::Cookie
, Rack::Session::Dalli
, etc).
Authentication is automatic for any routes that call protect_with_gauth!
. You can add a more user friendly login page, like this:
get '/login' do
'<a href="/auth/g">Login with Google Apps</a>'
end
This extension does not provide a logout mechanism, but one can be added easily if you like:
get '/logout' do
session.clear
# redirect or whatever...
end
Project Status
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request