sinatra_auth_github 0.0.13 → 0.0.14

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.
data/README.md CHANGED
@@ -13,4 +13,48 @@ The Extension in Action
13
13
  =======================
14
14
  % gem install bundler
15
15
  % bundle install
16
- % GH_CLIENT_ID="<from GH>" GH_SECRET="<from GH>" bundle exec rackup
16
+ % GITHUB_CLIENT_ID="<from GH>" GITHUB_CLIENT_SECRET="<from GH>" bundle exec shotgun
17
+
18
+ ```ruby
19
+ module Example
20
+ class App < Sinatra::Base
21
+ enable :sessions
22
+
23
+ set :github_options, {
24
+ # GitHub Provided secrets
25
+ :secret => ENV['GITHUB_CLIENT_SECRET'],
26
+ :client_id => ENV['GITHUB_CLIENT_ID'],
27
+
28
+ # How much info you need about the user
29
+ :scopes => 'user,offline_access',
30
+
31
+ # restrict access to a members of organization named
32
+ :organization => "github",
33
+
34
+ # restrict access to specific team on an organization
35
+ :team => nil # || 42
36
+ }
37
+
38
+ register Sinatra::Auth::Github
39
+
40
+ before do
41
+ authenticate!
42
+ end
43
+
44
+ helpers do
45
+ def repos
46
+ github_request("repos/show/#{github_user.login}")
47
+ end
48
+ end
49
+
50
+ get '/' do
51
+ "Hello There, #{github_user.name}!#{github_user.token}\n#{repos.inspect}"
52
+ end
53
+
54
+ get '/logout' do
55
+ logout!
56
+ redirect '/'
57
+ end
58
+ end
59
+ end
60
+ ```
@@ -5,7 +5,7 @@ require 'rest_client'
5
5
  module Sinatra
6
6
  module Auth
7
7
  module Github
8
- VERSION = "0.0.13"
8
+ VERSION = "0.0.14"
9
9
 
10
10
  class BadAuthentication < Sinatra::Base
11
11
  get '/unauthenticated' do
@@ -36,10 +36,24 @@ module Sinatra
36
36
  end
37
37
 
38
38
  def github_request(path)
39
- response = RestClient.get("https://github.com/api/v2/json/#{path}", {:accept => :json, :params => {:access_token => github_user.token}})
39
+ response = RestClient.get "https://github.com/api/v2/json/#{path}", :params => { :access_token => github_user.token }, :accept => :json
40
40
  JSON.parse(response.body)
41
41
  end
42
42
 
43
+ def github_organization_authenticate!(name)
44
+ authenticate!
45
+ orgs = github_request("user/show/#{github_user.login}/organizations")["organizations"]
46
+ halt([401, "Unauthorized User"]) unless orgs.map { |org| org["login"] }.include?(name)
47
+ end
48
+
49
+ def github_organization_team_authenticate!(name, team)
50
+ authenticate!
51
+ members = github_request("teams/#{team}/members")["users"]
52
+ halt([401, "Unauthorized User"]) unless members.map { |user| user["login"] }.include?(github_user.login)
53
+ rescue RestClient::Unauthorized => e
54
+ halt([401, "Unauthorized User"])
55
+ end
56
+
43
57
  def _relative_url_for(path)
44
58
  request.script_name + path
45
59
  end
@@ -54,6 +68,7 @@ module Sinatra
54
68
  manager[:github_secret] = app.github_options[:secret]
55
69
  manager[:github_scopes] = app.github_options[:scopes] || 'email,offline_access'
56
70
  manager[:github_client_id] = app.github_options[:client_id]
71
+ manager[:github_organization] = app.github_options[:organization] || nil
57
72
  manager[:github_callback_url] = app.github_options[:callback_url] || '/auth/github/callback'
58
73
  end
59
74
 
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "sinatra_auth_github"
6
- s.version = "0.0.13"
6
+ s.version = "0.0.14"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Corey Donohoe"]
9
9
  s.email = ["atmos@atmos.org"]
@@ -14,11 +14,12 @@ Gem::Specification.new do |s|
14
14
  s.rubyforge_project = "sinatra_auth_github"
15
15
 
16
16
  s.add_dependency "sinatra", "~>1.0"
17
- s.add_dependency "rest-client", "~>1.5.1"
18
- s.add_dependency "warden-github", "~>0.0.7"
17
+ s.add_dependency "rest-client", "~>1.6.1"
18
+ s.add_dependency "warden-github", "~>0.0.8"
19
19
 
20
20
  s.add_development_dependency "rake"
21
21
  s.add_development_dependency "rspec", "~>1.3.0"
22
+ s.add_development_dependency "shotgun"
22
23
  s.add_development_dependency "bundler", "~>1.0"
23
24
  s.add_development_dependency "randexp", "~>0.1.5"
24
25
  s.add_development_dependency "rack-test", "~>0.5.3"
data/spec/app.rb CHANGED
@@ -4,14 +4,26 @@ module Example
4
4
  class App < Sinatra::Base
5
5
  enable :sessions
6
6
 
7
- set :github_options, { :client_id => ENV['GH_CLIENT_ID'],
8
- :secret => ENV['GH_SECRET'],
9
- :scopes => 'user,offline_access,repo' }
7
+ set :github_options, {
8
+ # GitHub Provided secrets
9
+ :secret => ENV['GITHUB_CLIENT_SECRET'],
10
+ :client_id => ENV['GITHUB_CLIENT_ID'],
11
+
12
+ # How much info you need about the user
13
+ :scopes => 'user,offline_access,repo',
14
+
15
+ # restrict access to a members of organization named
16
+ :organization => "github",
17
+
18
+ # restrict access to specific team on an organization
19
+ :team => nil # || 42
20
+ }
10
21
 
11
22
  register Sinatra::Auth::Github
12
23
 
13
24
  before do
14
- authenticate!
25
+ # authenticate!
26
+ # halt([401, "Unauthorized User"]) unless github_organization_member?
15
27
  end
16
28
 
17
29
  helpers do
@@ -21,9 +33,20 @@ module Example
21
33
  end
22
34
 
23
35
  get '/' do
36
+ authenticate!
24
37
  "Hello There, #{github_user.name}!#{github_user.token}\n#{repos.inspect}"
25
38
  end
26
39
 
40
+ get '/orgs/:id' do
41
+ github_organization_authenticate!(params['id'])
42
+ "Hello There, #{github_user.name}! You have access to the #{params['id']} organization."
43
+ end
44
+
45
+ get '/orgs/:org_id/team/:id' do
46
+ github_organization_team_authenticate!(params['org_id'], params['id'])
47
+ "Hello There, #{github_user.name}! You have access to the #{params['id']} team under the #{params['org_id']} organization."
48
+ end
49
+
27
50
  get '/logout' do
28
51
  logout!
29
52
  redirect '/'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_auth_github
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 13
10
- version: 0.0.13
9
+ - 14
10
+ version: 0.0.14
11
11
  platform: ruby
12
12
  authors:
13
13
  - Corey Donohoe
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-08 00:00:00 -07:00
18
+ date: 2011-05-16 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -41,12 +41,12 @@ dependencies:
41
41
  requirements:
42
42
  - - ~>
43
43
  - !ruby/object:Gem::Version
44
- hash: 1
44
+ hash: 13
45
45
  segments:
46
46
  - 1
47
- - 5
47
+ - 6
48
48
  - 1
49
- version: 1.5.1
49
+ version: 1.6.1
50
50
  type: :runtime
51
51
  version_requirements: *id002
52
52
  - !ruby/object:Gem::Dependency
@@ -57,12 +57,12 @@ dependencies:
57
57
  requirements:
58
58
  - - ~>
59
59
  - !ruby/object:Gem::Version
60
- hash: 17
60
+ hash: 15
61
61
  segments:
62
62
  - 0
63
63
  - 0
64
- - 7
65
- version: 0.0.7
64
+ - 8
65
+ version: 0.0.8
66
66
  type: :runtime
67
67
  version_requirements: *id003
68
68
  - !ruby/object:Gem::Dependency
@@ -96,9 +96,23 @@ dependencies:
96
96
  type: :development
97
97
  version_requirements: *id005
98
98
  - !ruby/object:Gem::Dependency
99
- name: bundler
99
+ name: shotgun
100
100
  prerelease: false
101
101
  requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ type: :development
111
+ version_requirements: *id006
112
+ - !ruby/object:Gem::Dependency
113
+ name: bundler
114
+ prerelease: false
115
+ requirement: &id007 !ruby/object:Gem::Requirement
102
116
  none: false
103
117
  requirements:
104
118
  - - ~>
@@ -109,11 +123,11 @@ dependencies:
109
123
  - 0
110
124
  version: "1.0"
111
125
  type: :development
112
- version_requirements: *id006
126
+ version_requirements: *id007
113
127
  - !ruby/object:Gem::Dependency
114
128
  name: randexp
115
129
  prerelease: false
116
- requirement: &id007 !ruby/object:Gem::Requirement
130
+ requirement: &id008 !ruby/object:Gem::Requirement
117
131
  none: false
118
132
  requirements:
119
133
  - - ~>
@@ -125,11 +139,11 @@ dependencies:
125
139
  - 5
126
140
  version: 0.1.5
127
141
  type: :development
128
- version_requirements: *id007
142
+ version_requirements: *id008
129
143
  - !ruby/object:Gem::Dependency
130
144
  name: rack-test
131
145
  prerelease: false
132
- requirement: &id008 !ruby/object:Gem::Requirement
146
+ requirement: &id009 !ruby/object:Gem::Requirement
133
147
  none: false
134
148
  requirements:
135
149
  - - ~>
@@ -141,11 +155,11 @@ dependencies:
141
155
  - 3
142
156
  version: 0.5.3
143
157
  type: :development
144
- version_requirements: *id008
158
+ version_requirements: *id009
145
159
  - !ruby/object:Gem::Dependency
146
160
  name: ruby-debug
147
161
  prerelease: false
148
- requirement: &id009 !ruby/object:Gem::Requirement
162
+ requirement: &id010 !ruby/object:Gem::Requirement
149
163
  none: false
150
164
  requirements:
151
165
  - - ">="
@@ -155,7 +169,7 @@ dependencies:
155
169
  - 0
156
170
  version: "0"
157
171
  type: :development
158
- version_requirements: *id009
172
+ version_requirements: *id010
159
173
  description: A sinatra extension for easy oauth integration with github
160
174
  email:
161
175
  - atmos@atmos.org
@@ -168,7 +182,6 @@ extra_rdoc_files: []
168
182
  files:
169
183
  - .gitignore
170
184
  - Gemfile
171
- - Gemfile.lock
172
185
  - LICENSE
173
186
  - README.md
174
187
  - Rakefile