Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit b454fa4413ada955d890c43e9cd8cd6c109ca66d @grk committed Sep 22, 2012
Showing with 1,106 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +17 −0 Gemfile
  3. +124 −0 Gemfile.lock
  4. +20 −0 MIT-LICENSE
  5. +64 −0 README.md
  6. +38 −0 Rakefile
  7. 0 app/assets/images/rails-auth-github/.gitkeep
  8. BIN app/assets/images/rails-auth-github/securocat.png
  9. 0 app/controllers/.gitkeep
  10. +11 −0 app/controllers/github_auth_controller.rb
  11. +4 −0 config/routes.rb
  12. +6 −0 lib/rails-auth-github.rb
  13. +26 −0 lib/rails-auth-github/engine.rb
  14. +71 −0 lib/rails-auth-github/helpers.rb
  15. +3 −0 lib/rails-auth-github/version.rb
  16. +4 −0 lib/tasks/rails-auth-github_tasks.rake
  17. +18 −0 rails-auth-github.gemspec
  18. +8 −0 script/rails
  19. +261 −0 test/dummy/README.rdoc
  20. +7 −0 test/dummy/Rakefile
  21. +15 −0 test/dummy/app/assets/javascripts/application.js
  22. +13 −0 test/dummy/app/assets/stylesheets/application.css
  23. +3 −0 test/dummy/app/controllers/application_controller.rb
  24. +14 −0 test/dummy/app/controllers/home_controller.rb
  25. +2 −0 test/dummy/app/helpers/application_helper.rb
  26. 0 test/dummy/app/mailers/.gitkeep
  27. 0 test/dummy/app/models/.gitkeep
  28. +1 −0 test/dummy/app/views/home/index.html.erb
  29. +4 −0 test/dummy/app/views/home/user_info.html.erb
  30. +14 −0 test/dummy/app/views/layouts/application.html.erb
  31. +4 −0 test/dummy/config.ru
  32. +26 −0 test/dummy/config/application.rb
  33. +10 −0 test/dummy/config/boot.rb
  34. +5 −0 test/dummy/config/environment.rb
  35. +31 −0 test/dummy/config/environments/development.rb
  36. +64 −0 test/dummy/config/environments/production.rb
  37. +35 −0 test/dummy/config/environments/test.rb
  38. +7 −0 test/dummy/config/initializers/backtrace_silencers.rb
  39. +15 −0 test/dummy/config/initializers/inflections.rb
  40. +5 −0 test/dummy/config/initializers/mime_types.rb
  41. +7 −0 test/dummy/config/initializers/secret_token.rb
  42. +8 −0 test/dummy/config/initializers/session_store.rb
  43. +10 −0 test/dummy/config/initializers/wrap_parameters.rb
  44. +5 −0 test/dummy/config/locales/en.yml
  45. +5 −0 test/dummy/config/routes.rb
  46. 0 test/dummy/lib/assets/.gitkeep
  47. 0 test/dummy/log/.gitkeep
  48. +26 −0 test/dummy/public/404.html
  49. +26 −0 test/dummy/public/422.html
  50. +25 −0 test/dummy/public/500.html
  51. 0 test/dummy/public/favicon.ico
  52. +6 −0 test/dummy/script/rails
  53. +9 −0 test/integration/navigation_test.rb
  54. +7 −0 test/rails-auth-github_test.rb
  55. +15 −0 test/test_helper.rb
@@ -0,0 +1,7 @@
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
+test/dummy/.sass-cache
17 Gemfile
@@ -0,0 +1,17 @@
+source "http://rubygems.org"
+
+# Declare your gem's dependencies in rails-auth-github.gemspec.
+# Bundler will treat runtime dependencies like base dependencies, and
+# development dependencies will be added by default to the :development group.
+gemspec
+
+# jquery-rails is used by the dummy application
+gem "jquery-rails"
+
+# Declare any dependencies that are still in development here instead of in
+# your gemspec. These might include edge Rails or gems from your path or
+# Git. Remember to move these dependencies to your gemspec before releasing
+# your gem to rubygems.org.
+
+# To use debugger
+# gem 'debugger'
@@ -0,0 +1,124 @@
+PATH
+ remote: .
+ specs:
+ rails-auth-github (0.0.1)
+ rails (~> 3.2.8)
+ warden-github (~> 0.11.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.2.8)
+ actionpack (= 3.2.8)
+ mail (~> 2.4.4)
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.4)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ activerecord (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ activesupport (3.2.8)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ addressable (2.3.2)
+ arel (3.0.2)
+ builder (3.0.3)
+ erubis (2.7.0)
+ faraday (0.8.4)
+ multipart-post (~> 1.1)
+ faraday_middleware (0.8.8)
+ faraday (>= 0.7.4, < 0.9)
+ hashie (1.2.0)
+ hike (1.2.1)
+ i18n (0.6.1)
+ journey (1.0.4)
+ jquery-rails (2.1.2)
+ railties (>= 3.1.0, < 5.0)
+ thor (~> 0.14)
+ json (1.7.5)
+ mail (2.4.4)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.19)
+ multi_json (1.3.6)
+ multipart-post (1.1.5)
+ oauth2 (0.5.2)
+ faraday (~> 0.7)
+ multi_json (~> 1.0)
+ octokit (1.13.0)
+ addressable (~> 2.2)
+ faraday (~> 0.8)
+ faraday_middleware (~> 0.8)
+ hashie (~> 1.2)
+ multi_json (~> 1.3)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.8)
+ actionmailer (= 3.2.8)
+ actionpack (= 3.2.8)
+ activerecord (= 3.2.8)
+ activeresource (= 3.2.8)
+ activesupport (= 3.2.8)
+ bundler (~> 1.0)
+ railties (= 3.2.8)
+ railties (3.2.8)
+ actionpack (= 3.2.8)
+ activesupport (= 3.2.8)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (>= 0.14.6, < 2.0)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rest-client (1.6.7)
+ mime-types (>= 1.16)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ thor (0.16.0)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.33)
+ warden (1.2.1)
+ rack (>= 1.0)
+ warden-github (0.11.0)
+ json (~> 1.5)
+ oauth2 (~> 0.5.2)
+ octokit (~> 1.13.0)
+ rest-client (~> 1.6.1)
+ warden (~> 1.0)
+ yajl-ruby (~> 1.1)
+ yajl-ruby (1.1.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ jquery-rails
+ rails-auth-github!
@@ -0,0 +1,20 @@
+Copyright 2012 Grzesiek Kołodziejczyk
+
+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.
@@ -0,0 +1,64 @@
+# rails-auth-github
+
+A simple Rails plugin that provides authentication with github oauth.
+
+It's basically a port of [sinatra_auth_github](https://github.com/atmos/sinatra_auth_github) to Rails.
+
+**Note:** this is an initial release, so it may be buggy. Use at your own risk.
+
+## Install
+
+Add the gem to your Gemfile:
+
+```ruby
+gem "rails-auth-github", "~> 0.0.1"
+```
+
+## Configure
+
+You can configure rails-auth-github using `config.github_auth` in either
+config/application.rb or environment-specific config files, with following
+options:
+
+ * `config.github_auth.client_id` (required)
+ * `config.github_auth.client_secret` (required)
+ * `config.github_auth.scopes`
+ * `config.github_auth.oauth_domain`
+ * `config.github_auth.callback_url`
+
+## Usage
+
+This engine provides following helpers for use in controllers:
+
+`authenticate!`
+
+`authenticated?`
+
+`logout!`
+
+`github_user`
+
+`github_raw_request(path)`
+
+`github_request(path)`
+
+`github_public_organization_access?(name)`
+
+`github_organization_access?(name)`
+
+`github_team_access?(team_id)`
+
+`github_public_organization_authenticate!(name)`
+
+`github_organization_authenticate!(name)`
+
+`github_team_authenticate!(team_id)`
+
+Also, `github_user`, `github_public_organization_access?`,
+`github_organization_access?`, and `github_team_access?` are available as
+view helpers.
+
+## TODO
+
+ * tests
+
@@ -0,0 +1,38 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'RailsAuthGithub'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+
+
+Bundler::GemHelper.install_tasks
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
No changes.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
No changes.
@@ -0,0 +1,11 @@
+class GithubAuthController < ApplicationController
+ def callback
+ authenticate!
+ redirect_to session['return_to'] || root_url
+ end
+
+ def failed
+ render :inline =>
+ "<center><%= image_tag 'rails-auth-github/securocat.png' %></center>"
+ end
+end
@@ -0,0 +1,4 @@
+Rails.application.routes.draw do
+ match "auth/github/callback", :to => "github_auth#callback"
+ match "auth/github/failed", :to => "github_auth#failed"
+end
@@ -0,0 +1,6 @@
+require "rails-auth-github/engine"
+require "rails-auth-github/helpers"
+
+module RailsAuthGithub
+ Unauthorized = Class.new(StandardError)
+end
@@ -0,0 +1,26 @@
+require 'warden-github'
+require 'ostruct'
+
+module RailsAuthGithub
+ class Engine < ::Rails::Engine
+ config.github_auth = ActiveSupport::OrderedOptions.new
+
+ initializer "rails_auth_github.add_middleware" do |app|
+ app.middleware.use Warden::Manager do |manager|
+ manager.default_strategies :github
+ manager.failure_app = lambda{ |env| [301, {'Location' => '/auth/github/failed'}, ['Redirecting']] }
+ manager[:github_client_id] = app.config.github_auth.client_id
+ manager[:github_secret] = app.config.github_auth.client_secret
+ manager[:github_scopes] = app.config.github_auth.scopes.to_s
+ manager[:github_oauth_domain] = app.config.github_auth.oauth_domain || 'https://github.com'
+ manager[:github_callback_url] = app.config.github_auth.callback_url || '/auth/github/callback'
+ end
+ end
+
+ initializer "rails_auth_github.add_helpers" do |app|
+ ActiveSupport.on_load(:action_controller) do
+ include RailsAuthGithub::Helpers
+ end
+ end
+ end
+end
@@ -0,0 +1,71 @@
+module RailsAuthGithub
+ module Helpers
+ extend ActiveSupport::Concern
+
+ included do
+ helper_method :github_user, :github_public_organization_access?,
+ :github_organization_access?, :github_team_access?
+
+ rescue_from RailsAuthGithub::Unauthorized, :with => :github_unauthorized_user
+ end
+
+ def warden
+ request.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
+
+ def github_raw_request(path)
+ github_user.github_raw_request(path)
+ end
+
+ def github_request(path)
+ github_user.github_request(path)
+ end
+
+ def github_public_organization_access?(name)
+ github_user.publicized_organization_member?(name)
+ end
+
+ def github_organization_access?(name)
+ github_user.organization_member?(name)
+ end
+
+ def github_team_access?(team_id)
+ github_user.team_member?(team_id)
+ end
+
+ def github_public_organization_authenticate!(name)
+ authenticate!
+ raise RailsAuthGithub::Unauthorized unless github_public_organization_access?(name)
+ end
+
+ def github_organization_authenticate!(name)
+ authenticate!
+ raise RailsAuthGithub::Unauthorized unless github_organization_access?(name)
+ end
+
+ def github_team_authenticate!(team_id)
+ authenticate!
+ raise RailsAuthGithub::Unauthorized unless github_team_access?(team_id)
+ end
+
+ def github_unauthorized_user
+ head 401
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit b454fa4

Please sign in to comment.