warden-github-rails-thinknear-fork 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +20 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +39 -0
  5. data/CHANGELOG.md +20 -0
  6. data/Gemfile +27 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +250 -0
  9. data/Rakefile +6 -0
  10. data/VERSION +1 -0
  11. data/lib/warden/github/rails.rb +39 -0
  12. data/lib/warden/github/rails/config.rb +49 -0
  13. data/lib/warden/github/rails/controller_helpers.rb +39 -0
  14. data/lib/warden/github/rails/railtie.rb +42 -0
  15. data/lib/warden/github/rails/routes.rb +83 -0
  16. data/lib/warden/github/rails/test_helpers.rb +28 -0
  17. data/lib/warden/github/rails/test_helpers/mock_user.rb +30 -0
  18. data/lib/warden/github/rails/version.rb +9 -0
  19. data/spec/integration/controller_helpers_spec.rb +95 -0
  20. data/spec/integration/membership_spec.rb +183 -0
  21. data/spec/integration/route_spec.rb +82 -0
  22. data/spec/integration/scope_spec.rb +33 -0
  23. data/spec/integration/view_helpers_spec.rb +19 -0
  24. data/spec/rails_app/app/controllers/scoped_controller.rb +28 -0
  25. data/spec/rails_app/app/controllers/unscoped_controller.rb +28 -0
  26. data/spec/rails_app/app/controllers/view_tests_controller.rb +2 -0
  27. data/spec/rails_app/app/views/view_tests/authenticated.html.erb +1 -0
  28. data/spec/rails_app/app/views/view_tests/user.html.erb +1 -0
  29. data/spec/rails_app/config.ru +2 -0
  30. data/spec/rails_app/config/application.rb +20 -0
  31. data/spec/rails_app/config/boot.rb +3 -0
  32. data/spec/rails_app/config/environment.rb +3 -0
  33. data/spec/rails_app/config/environments/development.rb +8 -0
  34. data/spec/rails_app/config/environments/production.rb +8 -0
  35. data/spec/rails_app/config/environments/test.rb +11 -0
  36. data/spec/rails_app/config/initializers/secret_token.rb +1 -0
  37. data/spec/rails_app/config/initializers/session_store.rb +1 -0
  38. data/spec/rails_app/config/initializers/warden_github_rails.rb +12 -0
  39. data/spec/rails_app/config/initializers/wrap_parameters.rb +4 -0
  40. data/spec/rails_app/config/routes.rb +53 -0
  41. data/spec/rails_app/script/rails +6 -0
  42. data/spec/spec_helper.rb +40 -0
  43. data/spec/unit/config_spec.rb +67 -0
  44. data/spec/unit/mock_user_spec.rb +21 -0
  45. data/spec/unit/rails_spec.rb +11 -0
  46. data/spec/unit/test_helpers_spec.rb +39 -0
  47. data/warden-github-rails.gemspec +25 -0
  48. metadata +215 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmMzZWMzODZlYjE3OTJkNjJjZmY2NTlhMTg3OWM2YzNiZWQ5NzNhZg==
5
+ data.tar.gz: !binary |-
6
+ NTk3OTQ2ZWNkZWQ4YzFhZTg3ZDdjZmEwZGMzMTk2MDQzMTIyZWFmMA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MWRmMzM2ZTUwNTc0OGI2YWMxODQwMTJhZGQ3MzJlNjUzZWMyYThlNTQ3YTBj
10
+ MDAxOGJhMTZlN2FiYzY1ZjRhMTE5MWRhZGM0Mzc0ZjQ4NmQyYmZkMjJhMjhh
11
+ ZDM0MWIzMzNjYjM5OTMxNjI0ODZmZDlkOGU1NDUxY2Q3YTNkYTA=
12
+ data.tar.gz: !binary |-
13
+ NmEyYTc0YmE2NjdlNGJiMDE5MjljM2Y2MzU1NjY3YzRiMTFkYzBjNjZkYWRi
14
+ MzUxNzdlNDgwZGQzOWJkN2JlZTIwNDdmYThkNGMxMjkyMmY5MjQ4ZWZlNDI5
15
+ MWU2MmEwZThmNGE1ZDBmNGM3OGYxOGYwMDNjMzZjMGJkNTdiZjE=
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tags
19
+ bin/
20
+ log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,39 @@
1
+ language: ruby
2
+ script: "bundle exec rspec"
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.2
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - ruby-head
9
+ - jruby-18mode # JRuby in 1.8 mode
10
+ - jruby-19mode # JRuby in 1.9 mode
11
+ - rbx-18mode
12
+ - rbx-19mode
13
+ env:
14
+ - "RAILS_VERSION=3.1.0"
15
+ - "RAILS_VERSION=3.2.0"
16
+ - "RAILS_VERSION=4.0.0.pre"
17
+ - "RAILS_VERSION=master"
18
+ matrix:
19
+ allow_failures:
20
+ - env: "RAILS_VERSION=master"
21
+ - rvm: ruby-head
22
+ exclude:
23
+ - rvm: 1.8.7
24
+ env: "RAILS_VERSION=4.0.0.pre"
25
+ - rvm: 1.9.2
26
+ env: "RAILS_VERSION=4.0.0.pre"
27
+ - rvm: jruby-18mode
28
+ env: "RAILS_VERSION=4.0.0.pre"
29
+ - rvm: rbx-18mode
30
+ env: "RAILS_VERSION=4.0.0.pre"
31
+ - rvm: 1.8.7
32
+ env: "RAILS_VERSION=master"
33
+ - rvm: 1.9.2
34
+ env: "RAILS_VERSION=master"
35
+ - rvm: jruby-18mode
36
+ env: "RAILS_VERSION=master"
37
+ - rvm: rbx-18mode
38
+ env: "RAILS_VERSION=master"
39
+
@@ -0,0 +1,20 @@
1
+ # Changelog
2
+
3
+ ## v1.1.0
4
+
5
+ - Upgrade to octokit.rb version 2
6
+ - Require ruby 1.9 or higher
7
+
8
+ ## v1.0.1
9
+
10
+ - Fully test on Rails 4
11
+ - Improve mock user membership stubbing
12
+ - Add testing instructions to README
13
+
14
+ ## v1.0.0
15
+
16
+ - Add Devise compatibility
17
+
18
+ ## v0.0.1
19
+
20
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,27 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ if ENV['EDGE']
6
+ gem 'warden-github', :github => 'atmos/warden-github'
7
+ end
8
+
9
+ rails_version = ENV['RAILS_VERSION']
10
+
11
+ rails_opts = case rails_version
12
+ when 'master'
13
+ { :github => 'rails/rails' }
14
+ when nil
15
+ {}
16
+ else
17
+ "~> #{rails_version}"
18
+ end
19
+
20
+ gem "rails", rails_opts
21
+
22
+ group :development do
23
+ unless ENV['CI']
24
+ gem 'debugger', :platforms => :ruby_19, :require => false
25
+ gem 'ruby-debug', :platforms => :ruby_18, :require => false
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Philipe Fatio
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,250 @@
1
+ # warden-github-rails
2
+
3
+ [![Build Status](https://travis-ci.org/fphilipe/warden-github-rails.png)](https://travis-ci.org/fphilipe/warden-github-rails)
4
+ [![Gem Version](https://badge.fury.io/rb/warden-github-rails.png)](http://badge.fury.io/rb/warden-github-rails)
5
+ [![Dependency Status](https://gemnasium.com/fphilipe/warden-github-rails.png)](https://gemnasium.com/fphilipe/warden-github-rails)
6
+ [![Code Climate](https://codeclimate.com/github/fphilipe/warden-github-rails.png)](https://codeclimate.com/github/fphilipe/warden-github-rails)
7
+
8
+ A gem for rails that provides easy GitHub OAuth integration.
9
+ It is built on top of [warden-github](https://github.com/atmos/warden-github), which gives you an easy to use [warden](https://github.com/hassox/warden) strategy to authenticate GitHub users.
10
+
11
+ ## Motivation
12
+
13
+ **Wouldn't it be nice to**
14
+
15
+ - use your organization and its teams for user access control?
16
+ - add a new employee to your GitHub organization or team in order to grant them access to your app's admin area?
17
+
18
+ The motivation for this gem was to provide a very easy authorization (not authentication) mechanism to existing rails apps for admins, especially in combination with organization and team memberships.
19
+ The provided routing helpers do exactly that.
20
+ They allow you to restrict access to members of your organization or a certain team.
21
+
22
+ This is how your rails `routes.rb` could look like:
23
+
24
+ ```ruby
25
+ constraints(:subdomain => 'admin') do
26
+ github_authenticate(:org => 'my_company_inc') do
27
+ resources :users
28
+ resources :projects
29
+
30
+ github_authenticated(:team => 'sysadmins') do
31
+ resource :infrastructure
32
+ end
33
+ end
34
+ end
35
+ ```
36
+
37
+ Of course, this gem can also be used for user registration and authentication.
38
+ Several helper methods are available in the controller to accomplish this:
39
+
40
+ ```ruby
41
+ class UsersController < ApplicationController
42
+ # ...
43
+
44
+ def new
45
+ github_authenticate! # Performs OAuth flow when not logged in.
46
+ @user = User.new(:name => github_user.name, :email => github_user.email)
47
+ end
48
+
49
+ def create
50
+ attrs = params.require(:user).permit(:name, :email).merge(:github_id => github_user.id)
51
+ @user = User.create(attrs)
52
+
53
+ if @user
54
+ redirect_to :show
55
+ else
56
+ render :new
57
+ end
58
+ end
59
+
60
+ # ...
61
+ end
62
+ ```
63
+
64
+ ## Installation
65
+
66
+ To use this gem, add it to your `Gemfile`:
67
+
68
+ ```ruby
69
+ gem 'warden-github-rails', '~> 1.0'
70
+ ```
71
+
72
+ If you're using devise, make sure to use version 2.2.4 or newer.
73
+ Previous versions are not compatible with warden-github-rails and thus will not work.
74
+ See the note at [*Using alongside Devise and other Warden Gems*](#using-alongside-devise-and-other-warden-gems) for an explanation.
75
+
76
+ ## Usage
77
+
78
+ ### Configuration
79
+
80
+ First off, you might want to configure this gem by creating an initializer such as `config/initializers/warden_github_rails.rb`.
81
+ There you can define:
82
+
83
+ - various scopes and their configs (scopes are types of users with different configs)
84
+ - the default scope (which is `:user` by default)
85
+ - team aliases (GitHub teams are identified by a numerical ID; defining an alias for a team makes it easier to use)
86
+
87
+ Here's how such a config might look like:
88
+
89
+ ```ruby
90
+ Warden::GitHub::Rails.setup do |config|
91
+ config.add_scope :user, :client_id => 'foo',
92
+ :client_secret => 'bar',
93
+ :scope => 'user'
94
+
95
+ config.add_scope :admin, :client_id => 'abc',
96
+ :client_secret => 'xyz',
97
+ :redirect_uri => '/admin/login/callback',
98
+ :scope => 'repo'
99
+
100
+ config.default_scope = :admin
101
+
102
+ config.add_team :marketing, 456
103
+ end
104
+ ```
105
+
106
+ For a list of allowed config parameters to use in `#add_scope`, read the [warden-github documentation](https://github.com/atmos/warden-github#parameters).
107
+
108
+ ### Inside `routes.rb`
109
+
110
+ The available routing helpers are defined and documented in [lib/warden/github/rails/routes.rb](lib/warden/github/rails/routes.rb).
111
+ They all accept an optional scope that, when omitted, falls back to the default_scope configured in the initializer.
112
+
113
+ Examples:
114
+
115
+ ```ruby
116
+ # Performs login if not logged in already.
117
+ github_authenticate do
118
+ resource :profile
119
+ end
120
+
121
+ # Does not perform login when not logged in.
122
+ github_authenticated do
123
+ delete '/logout' => 'sessions#delete'
124
+ end
125
+
126
+ # Only matches when not logged in. Does not perform login.
127
+ github_unauthenticated do
128
+ resource :registration
129
+ end
130
+
131
+ # Only matches when member of the organization. Initiates login if not logged in.
132
+ github_authenticate(:org => 'my_company') do
133
+ resource :admin
134
+ end
135
+
136
+ # Only matches when member of the team. Does not initiate login if not logged in.
137
+ github_authenticated(:team => 'markting') do
138
+ get '/dashboard' => 'dashboard#show'
139
+ end
140
+
141
+ # Using dynamic membership values:
142
+ github_authenticate(:org => lambda { |req| r.params[:id] }) do
143
+ get '/orgs/:id' => 'orgs#show'
144
+ end
145
+ ```
146
+
147
+ ### Inside a Controller
148
+
149
+ The available controller helpers are defined and documented in [lib/warden/github/rails/controller_helpers.rb](lib/warden/github/rails/controller_helpers.rb).
150
+ They all accept an optional scope that, when omitted, falls back to the default_scope configured in the initializer.
151
+
152
+ ```ruby
153
+ class SomeController < ActionController::Base
154
+ def show
155
+ @is_admin = github_authenticated?(:admin)
156
+ end
157
+
158
+ def delete
159
+ github_logout
160
+ redirect_to '/'
161
+ end
162
+
163
+ def settings
164
+ github_authenticate!
165
+ @settings = UserSettings.find_by_github_user_id(github_user.id)
166
+ end
167
+
168
+ def finish_wizard
169
+ github_session[:wizard_completed] = true
170
+ end
171
+
172
+ def followers
173
+ @followers = github_user.api.followers
174
+ end
175
+ end
176
+ ```
177
+
178
+ ### Communicating with the GitHub API
179
+
180
+ Once a user is logged in, you'll have access to it in the controller using `github_user`. It is an instance of `Warden::GitHub::User` which is defined in the [warden-github](https://github.com/atmos/warden-github/blob/master/lib/warden/github/user.rb) gem. The instance has several methods to access user information such as `#name`, `#id`, `#email`, etc. It also features a method `#api` which returns a preconfigured [Octokit](https://github.com/pengwynn/octokit) client for that user.
181
+
182
+ ### Test Helpers
183
+
184
+ This gems comes with a couple test helpers to make your life easier:
185
+
186
+ - A method is added to `Rack::Response` called `#github_oauth_redirect?` which
187
+ returns true if the response is a redirect to a url that starts with
188
+ `https://github.com/login/oauth/authorize`. You can use it in your request
189
+ tests to make sure the OAuth dance is initiated. In rspec you could verify
190
+ this as follows:
191
+
192
+ ```ruby
193
+ subject { get '/some-url-that-triggers-oauth' }
194
+ it { should be_github_oauth_redirect }
195
+ ```
196
+
197
+ - A mock user that allows you to stub team and organization memberships:
198
+
199
+ ```ruby
200
+ user = Warden::GitHub::Rails::TestHelpers::MockUser.new
201
+ user.stub_membership(team: [234, 987], org: 'some-inc')
202
+ user.team_member?(234) # => true
203
+ user.organization_member?('rails') # => false
204
+ ```
205
+
206
+ - A method that creates a mock user and logs it in. If desired, the scope can
207
+ be specified. The method returns the mock user so that you can manipulate it
208
+ further:
209
+
210
+ ```ruby
211
+ user = github_login(:admin)
212
+
213
+ get '/org/rails/admin'
214
+ expect(response).to be_not_found
215
+
216
+ user.stub_membership(org: 'rails')
217
+ get '/org/rails/admin'
218
+ expect(response).to be_ok
219
+ ```
220
+
221
+ In order to use the mock user and the `#github_login` method, make sure to
222
+ include `Warden::GitHub::Rails::TestHelpers` in your tests.
223
+
224
+ ## Using alongside Devise and other Warden Gems
225
+
226
+ Currently this gem does not play nicely with other gems that setup a warden middleware.
227
+ The reason is that warden simply does not have support for multiple middlewares.
228
+ The warden middleware configures a warden instance and adds it to the rack environment.
229
+ Any other warden middleware downstream checks for any existing warden instance in the environment and, if present, skips itself.
230
+ I've opened an [issue](https://github.com/hassox/warden/issues/67) on the warden repository to discuss possible workarounds.
231
+
232
+ Nevertheless, this gem is compatible with devise for version 2.2.4 and newer.
233
+ devise allows you to specify a block that will be invoked when the warden middleware is configured.
234
+ This functionality is used in this gem in order to setup the github strategy for warden instead of inserting our own middleware.
235
+
236
+ ## Additional Information
237
+
238
+ ### Dependencies
239
+
240
+ - [warden-github](https://github.com/atmos/warden-github)
241
+ - [warden](https://github.com/hassox/warden)
242
+ - [octokit](https://github.com/pengwynn/octokit)
243
+
244
+ ### Maintainers
245
+
246
+ - Philipe Fatio ([@fphilipe](https://github.com/fphilipe))
247
+
248
+ ### License
249
+
250
+ MIT License. Copyright 2013 Philipe Fatio
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.1.0
@@ -0,0 +1,39 @@
1
+ require 'warden/github'
2
+
3
+ require 'warden/github/rails/version'
4
+ require 'warden/github/rails/routes'
5
+ require 'warden/github/rails/railtie'
6
+ require 'warden/github/rails/config'
7
+ require 'warden/github/rails/controller_helpers'
8
+
9
+ require 'forwardable'
10
+
11
+ module Warden
12
+ module GitHub
13
+ module Rails
14
+ extend SingleForwardable
15
+
16
+ def_delegators :config,
17
+ :default_scope,
18
+ :scopes,
19
+ :team_id
20
+
21
+ @config = Config.new
22
+
23
+ def self.config
24
+ @config
25
+ end
26
+
27
+ # Use this method to setup this gem.
28
+ #
29
+ # @example
30
+ #
31
+ # Warden::GitHub::Rails.setup do |config|
32
+ # # ...
33
+ # end
34
+ def self.setup
35
+ yield config
36
+ end
37
+ end
38
+ end
39
+ end