Skip to content

Commit

Permalink
authenticate to gmail in sinatra
Browse files Browse the repository at this point in the history
* available via 'register Sinatra::Auth::Gmail' in your app
  • Loading branch information
atmos committed Feb 10, 2010
0 parents commit 788a26c
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
coverage
24 changes: 24 additions & 0 deletions Gemfile
@@ -0,0 +1,24 @@
source :gemcutter

group :runtime do
gem 'haml', '~>2.2.0'
gem 'sinatra', '~>0.9.4'
gem 'rack', '~>1.0.1'
gem 'warden-googleapps', '~>0.0.3'
end

group :development do
gem 'shotgun'
end

group :test do
gem 'rake'
gem 'rcov'
gem 'rspec', '~>1.2.9', :require => 'spec'
gem 'bundler', '~>0.9.3'
gem 'randexp', '>=0.1.4'
gem 'rack-test', '~>0.5.3', :require => 'rack/test'
gem 'ParseTree', '>=3.0.4', :require => nil
end

# vim:ft=ruby
20 changes: 20 additions & 0 deletions LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2010 Corey Donohoe

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
@@ -0,0 +1,28 @@
sinatra_gmail_auth
==================

A sinatra app the provides a gem that...

Developing
==========
% gem install bundler
% bundle install
% bundle exec rake

Gemfile
-------
gem "sinatra_auth_gmail"
# vim:ft=ruby

Example
-------
module Fooby
class App < Sinatra::Base
register Sinatra::Auth::Gmail
get '/' do
authorize!
haml "%h2= 'Hello There, #{gmail_user.full_name}!'"
end
end
end
end
52 changes: 52 additions & 0 deletions Rakefile
@@ -0,0 +1,52 @@
require 'rake/gempackagetask'
require 'rubygems/specification'
require 'date'
require 'bundler'

task :default => [:spec]

require 'spec/rake/spectask'
desc "Run specs"
Spec::Rake::SpecTask.new do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = %w(-fs --color)
t.spec_opts << '--loadby' << 'random'

t.rcov_opts << '--exclude' << 'spec,.bundle'
t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
t.rcov_opts << '--text-summary'
t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
end

GEM = "sinatra_auth_gmail"
GEM_VERSION = "0.0.1"
AUTHOR = "Corey Donohoe"
EMAIL = "atmos@atmos.org"
HOMEPAGE = "http://github.com/atmos/sinatra_auth_gmail"
SUMMARY = "A sinatra extension that provides authentication for GMail authentication"

spec = Gem::Specification.new do |s|
s.name = GEM
s.version = GEM_VERSION
s.platform = Gem::Platform::RUBY
s.has_rdoc = true
s.extra_rdoc_files = ["LICENSE", 'TODO']
s.summary = SUMMARY
s.description = s.summary
s.author = AUTHOR
s.email = EMAIL
s.homepage = HOMEPAGE

bundle = Bundler::Definition.from_gemfile('Gemfile')
bundle.dependencies.each do |dep|
next unless dep.groups.include?(:runtime)
s.add_dependency(dep.name, dep.version_requirements.to_s)
end

s.require_path = 'lib'
s.files = %w(LICENSE README.md Rakefile TODO) + Dir.glob("{lib}/**/*")
end

Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
end
61 changes: 61 additions & 0 deletions lib/sinatra/auth/gmail.rb
@@ -0,0 +1,61 @@
module Sinatra
module Auth
module Gmail
class BadAuthentication < Sinatra::Default
get '/unauthenticated' do
status 403
haml "%h3= 'Unable to authenticate, sorry bud.'"
end
end

module Helpers
def authorized?
env['warden'].authenticate!
end

def authorize!
throw(:warden) unless authorized?
end

def logout!
env['warden'].logout
end

def gmail_user
env['warden'].user
end
end

def self.registered(app)
app.use Warden::Manager do |manager|
manager.default_strategies :google_apps
manager.failure_app = BadAuthentication

manager[:google_apps_endpoint] = 'http://www.google.com/accounts/o8/id'
end
app.helpers Helpers

app.get '/logout' do
logout!
haml "%h2= 'Peace!'"
end
end

class ExampleApp < Sinatra::Base
register Sinatra::Auth::Gmail

before do
authorize!
end

get '/' do
haml "%h2= 'Hello There, #{gmail_user.full_name}!'"
end

get '/another_route' do
haml "%h2= 'Hello There, #{gmail_user.full_name}!'"
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/sinatra_auth_gmail.rb
@@ -0,0 +1,13 @@
require 'sinatra/base'
require 'haml/util'
require 'haml/engine'
require 'warden-googleapps'

module Sinatra
module Auth
module Gmail
end
end
end

require File.join(File.dirname(__FILE__), 'sinatra', 'auth', 'gmail')
10 changes: 10 additions & 0 deletions spec/sinatra_auth_gmail_spec.rb
@@ -0,0 +1,10 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "Sinatra::Auth::Gmail" do
it "validates via something other than a browser" do
pending do
get '/'
last_response.should be_successful
end
end
end
12 changes: 12 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,12 @@
Bundler.setup(:runtime, :test)
Bundler.require(:runtime, :test)
require File.join(File.dirname(__FILE__), '..', 'lib', 'sinatra_auth_gmail')
require 'pp'

Spec::Runner.configure do |config|
config.include(Rack::Test::Methods)

def app
Sinatra::Auth::Gmail::ExampleApp
end
end

0 comments on commit 788a26c

Please sign in to comment.