Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial import of the sinatra extension to use github auth

  • Loading branch information...
commit 56ebb74c9f1fa4f88408203fb0e7a439061442a1 0 parents
@atmos authored
4 .gitignore
@@ -0,0 +1,4 @@
+coverage
+.bundle
+pkg
+.DS_Store
19 Gemfile
@@ -0,0 +1,19 @@
+source :gemcutter
+
+group :runtime do
+ gem 'sinatra', '~>1.0'
+ gem 'warden-github', '~>0.0.3'
+end
+
+group :test do
+ gem 'rake'
+ gem 'rspec', '~>1.3.0', :require => 'spec'
+ gem 'rcov'
+ gem 'webrat', '~>0.7.0'
+ gem 'bundler', '~>0.9.25'
+ gem 'fakeweb', '>=1.2.8'
+ gem 'randexp', '>=0.1.4'
+ gem 'rack-test', '~>0.5.3', :require => 'rack/test'
+end
+
+# vim:ft=ruby
20 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.
10 README.md
@@ -0,0 +1,10 @@
+sinatra_auth_github
+===================
+
+A sinatra app the provides a gem that...
+
+Developing
+==========
+ % gem install bundler
+ % bundle install
+ % bundle exec rake
59 Rakefile
@@ -0,0 +1,59 @@
+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_github"
+GEM_VERSION = "0.0.1"
+AUTHOR = "Corey Donohoe"
+EMAIL = "atmos@atmos.org"
+HOMEPAGE = "http://www.github.com/atmos/sinatra_auth_github"
+SUMMARY = "A sinatra extension for easy oauth integration with github"
+
+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"]
+ 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) + Dir.glob("{lib,features}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+desc "create a gemspec file"
+task :make_spec do
+ File.open("#{GEM}.gemspec", "w") do |file|
+ file.puts spec.to_ruby
+ end
+end
20 config.ru
@@ -0,0 +1,20 @@
+ENV['RACK_ENV'] ||= 'development'
+begin
+ require File.expand_path('../.bundle/environment', __FILE__)
+rescue LoadError
+ require "rubygems"
+ require "bundler"
+ Bundler.setup
+end
+
+Bundler.require(:runtime)
+
+$LOAD_PATH << File.dirname(__FILE__) + '/lib'
+require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'sinatra_auth_github'))
+require File.expand_path(File.join(File.dirname(__FILE__), 'spec', 'app'))
+
+use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public"
+
+run Example::App
+
+# vim:ft=ruby
53 lib/sinatra/auth/github.rb
@@ -0,0 +1,53 @@
+module Sinatra
+ module Auth
+ module Github
+ class BadAuthentication < Sinatra::Base
+ get '/unauthenticated' do
+ status 403
+ "Unable to authenticate, sorry bud."
+ end
+ end
+
+ module Helpers
+ def warden
+ env['warden']
+ end
+
+ def authenticate!(*args)
+ warden.authenticate!(*args)
+ end
+
+ def authenticated?(*args)
+ warden.authenticated?(*args)
+ end
+
+ def logout!
+ warden.logout
+ end
+
+ def github_user
+ warden.user
+ end
+ end
+
+ def self.registered(app)
+ app.use Warden::Manager do |manager|
+ manager.default_strategies :github
+
+ manager.failure_app = app.github_options[:failure_app] || BadAuthentication
+
+ manager[:github_secret] = app.github_options[:secret]
+ manager[:github_client_id] = app.github_options[:client_id]
+ manager[:github_callback_url] = app.github_options[:callback_url] || '/auth/github/callback'
+ end
+
+ app.helpers Helpers
+
+ app.get '/auth/github/callback' do
+ authenticate!
+ redirect "/"
+ end
+ end
+ end
+ end
+end
2  lib/sinatra_auth_github.rb
@@ -0,0 +1,2 @@
+require 'warden-github'
+require 'sinatra/auth/github'
27 spec/app.rb
@@ -0,0 +1,27 @@
+module Example
+ class App < Sinatra::Base
+ enable :sessions
+
+ set :github_options, {:client_id => ENV['GH_CLIENT_ID'],
+ :secret => ENV['GH_SECRET'] }
+
+ register Sinatra::Auth::Github
+
+ before do
+ authenticate!
+ end
+
+ get '/' do
+ "Hello There, #{github_user.name}!"
+ end
+
+ get '/another_route' do
+ "Hello There, #{github_user.name}!"
+ end
+
+ get '/logout' do
+ logout!
+ redirect '/'
+ end
+ end
+end
20 spec/spec_helper.rb
@@ -0,0 +1,20 @@
+Bundler.require(:default, :runtime, :test)
+
+require File.join(File.dirname(__FILE__), '..', 'lib', 'sinatra', 'auth', 'github')
+
+require 'pp'
+
+Webrat.configure do |config|
+ config.mode = :rack
+ config.application_port = 4567
+end
+
+Spec::Runner.configure do |config|
+ config.include(Rack::Test::Methods)
+ config.include(Webrat::Methods)
+ config.include(Webrat::Matchers)
+
+ def app
+ Example.app
+ end
+end

0 comments on commit 56ebb74

Please sign in to comment.
Something went wrong with that request. Please try again.