oauth2-provider-jonrowe 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/README.rdoc +314 -0
  2. data/example/README.rdoc +11 -0
  3. data/example/application.rb +151 -0
  4. data/example/config.ru +3 -0
  5. data/example/environment.rb +11 -0
  6. data/example/models/connection.rb +9 -0
  7. data/example/models/note.rb +4 -0
  8. data/example/models/user.rb +6 -0
  9. data/example/public/style.css +78 -0
  10. data/example/schema.rb +27 -0
  11. data/example/views/authorize.erb +28 -0
  12. data/example/views/create_user.erb +3 -0
  13. data/example/views/home.erb +25 -0
  14. data/example/views/layout.erb +25 -0
  15. data/example/views/login.erb +20 -0
  16. data/example/views/new_client.erb +25 -0
  17. data/example/views/new_user.erb +22 -0
  18. data/example/views/show_client.erb +15 -0
  19. data/lib/oauth2/model.rb +17 -0
  20. data/lib/oauth2/model/authorization.rb +113 -0
  21. data/lib/oauth2/model/client.rb +55 -0
  22. data/lib/oauth2/model/client_owner.rb +13 -0
  23. data/lib/oauth2/model/hashing.rb +27 -0
  24. data/lib/oauth2/model/resource_owner.rb +26 -0
  25. data/lib/oauth2/model/schema.rb +42 -0
  26. data/lib/oauth2/provider.rb +117 -0
  27. data/lib/oauth2/provider/access_token.rb +66 -0
  28. data/lib/oauth2/provider/authorization.rb +168 -0
  29. data/lib/oauth2/provider/error.rb +29 -0
  30. data/lib/oauth2/provider/exchange.rb +212 -0
  31. data/lib/oauth2/router.rb +60 -0
  32. data/spec/factories.rb +27 -0
  33. data/spec/oauth2/model/authorization_spec.rb +216 -0
  34. data/spec/oauth2/model/client_spec.rb +55 -0
  35. data/spec/oauth2/model/resource_owner_spec.rb +55 -0
  36. data/spec/oauth2/provider/access_token_spec.rb +125 -0
  37. data/spec/oauth2/provider/authorization_spec.rb +323 -0
  38. data/spec/oauth2/provider/exchange_spec.rb +330 -0
  39. data/spec/oauth2/provider_spec.rb +531 -0
  40. data/spec/request_helpers.rb +46 -0
  41. data/spec/spec_helper.rb +44 -0
  42. data/spec/test_app/helper.rb +33 -0
  43. data/spec/test_app/provider/application.rb +61 -0
  44. data/spec/test_app/provider/views/authorize.erb +19 -0
  45. metadata +220 -0
@@ -0,0 +1,46 @@
1
+ module RequestHelpers
2
+ require 'net/http'
3
+
4
+ def get(query_params)
5
+ qs = params.map { |k,v| "#{ CGI.escape k.to_s }=#{ CGI.escape v.to_s }" }.join('&')
6
+ uri = URI.parse('http://localhost:8000/authorize?' + qs)
7
+ Net::HTTP.get_response(uri)
8
+ end
9
+
10
+ def allow_or_deny(query_params)
11
+ Net::HTTP.post_form(URI.parse('http://localhost:8000/allow'), query_params)
12
+ end
13
+
14
+ def post_basic_auth(auth_params, query_params)
15
+ url = "http://#{ auth_params['client_id'] }:#{ auth_params['client_secret'] }@localhost:8000/authorize"
16
+ Net::HTTP.post_form(URI.parse(url), query_params)
17
+ end
18
+
19
+ def post(query_params)
20
+ Net::HTTP.post_form(URI.parse('http://localhost:8000/authorize'), query_params)
21
+ end
22
+
23
+ def validate_json_response(response, status, body)
24
+ response.code.to_i.should == status
25
+ JSON.parse(response.body).should == body
26
+ response['Content-Type'].should == 'application/json'
27
+ response['Cache-Control'].should == 'no-store'
28
+ end
29
+
30
+ def mock_request(request_class, stubs = {})
31
+ mock_request = mock(request_class)
32
+ method_stubs = {
33
+ :redirect? => false,
34
+ :response_body => nil,
35
+ :response_headers => {},
36
+ :response_status => 200
37
+ }.merge(stubs)
38
+
39
+ method_stubs.each do |method, value|
40
+ mock_request.should_receive(method).and_return(value)
41
+ end
42
+
43
+ mock_request
44
+ end
45
+ end
46
+
@@ -0,0 +1,44 @@
1
+ dir = File.expand_path(File.dirname(__FILE__))
2
+ $:.unshift(dir + '/../lib')
3
+ $:.unshift(dir)
4
+
5
+ require 'rubygems'
6
+ require 'bundler/setup'
7
+
8
+ require 'active_record'
9
+ require 'oauth2/provider'
10
+
11
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => 'test.sqlite3')
12
+
13
+ OAuth2::Model::Schema.up
14
+
15
+ ActiveRecord::Schema.define do |version|
16
+ create_table :users, :force => true do |t|
17
+ t.string :name
18
+ end
19
+ end
20
+
21
+ require 'test_app/helper'
22
+ require 'test_app/provider/application'
23
+
24
+ require 'request_helpers'
25
+
26
+ require 'thin'
27
+ Thin::Logging.silent = true
28
+
29
+ require 'factories'
30
+
31
+ RSpec.configure do |config|
32
+ config.before do
33
+ OAuth2::Provider.enforce_ssl = false
34
+ end
35
+
36
+ config.after do
37
+ [ OAuth2::Model::Client,
38
+ OAuth2::Model::Authorization,
39
+ TestApp::User
40
+
41
+ ].each { |k| k.delete_all }
42
+ end
43
+ end
44
+
@@ -0,0 +1,33 @@
1
+ module TestApp
2
+
3
+ class User < ActiveRecord::Base
4
+ set_table_name :users
5
+
6
+ include OAuth2::Model::ResourceOwner
7
+ include OAuth2::Model::ClientOwner
8
+
9
+ def self.[](name)
10
+ find_or_create_by_name(name)
11
+ end
12
+ end
13
+
14
+ module Helper
15
+ module RackRunner
16
+ def start(port)
17
+ handler = Rack::Handler.get('thin')
18
+ Thread.new do
19
+ handler.run(new, :Port => port) { |server| @server = server }
20
+ end
21
+ sleep 0.1 until @server
22
+ end
23
+
24
+ def stop
25
+ @server.stop if @server
26
+ @server = nil
27
+ sleep 0.1 while EM.reactor_running?
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+
@@ -0,0 +1,61 @@
1
+ require 'sinatra'
2
+
3
+ module TestApp
4
+ class Provider < Sinatra::Base
5
+
6
+ extend Helper::RackRunner
7
+
8
+ OAuth2::Provider.realm = 'Demo App'
9
+
10
+ set :views, File.dirname(__FILE__) + '/views'
11
+
12
+ def handle_authorize
13
+ @oauth2 = OAuth2::Provider.parse(User['Bob'], request)
14
+ redirect @oauth2.redirect_uri if @oauth2.redirect?
15
+
16
+ headers @oauth2.response_headers
17
+ status @oauth2.response_status
18
+
19
+ @oauth2.response_body || erb(:authorize)
20
+ end
21
+
22
+ def protect_resource_for(user = nil, scopes = [])
23
+ access_token = OAuth2::Provider.access_token(user, scopes, request)
24
+ headers access_token.response_headers
25
+ status access_token.response_status
26
+ yield access_token
27
+ end
28
+
29
+ def serve_protected_resource
30
+ @user = User['Bob']
31
+ protect_resource_for(@user, ['profile']) do |auth|
32
+ if auth.valid?
33
+ JSON.unparse('data' => 'Top secret')
34
+ else
35
+ JSON.unparse('data' => 'No soup for you')
36
+ end
37
+ end
38
+ end
39
+
40
+ [:get, :post].each do |method|
41
+ __send__(method, '/authorize') { handle_authorize }
42
+ end
43
+
44
+ post '/allow' do
45
+ @user = User['bob']
46
+ @oauth2 = OAuth2::Provider::Authorization.new(@user, params)
47
+ if params['allow'] == '1'
48
+ @oauth2.grant_access!
49
+ else
50
+ @oauth2.deny_access!
51
+ end
52
+ redirect @oauth2.redirect_uri
53
+ end
54
+
55
+ [:get, :post].each do |method|
56
+ __send__(method, '/user_profile') { serve_protected_resource }
57
+ end
58
+
59
+ end
60
+ end
61
+
@@ -0,0 +1,19 @@
1
+ <h1>Authorize OAuth client</h1>
2
+
3
+ <p>Do you want to allow <%= @oauth2.client.name %> to act on your behalf?</p>
4
+
5
+ <ul>
6
+ <% @oauth2.scopes.each do |scope| %><%= scope %><% end %>
7
+ </ul>
8
+
9
+ <form method="post" action="/allow">
10
+ <% @oauth2.params.each do |key, value| %>
11
+ <input type="hidden" name="<%= key %>" value="<%= value %>">
12
+ <% end %>
13
+
14
+ <input type="checkbox" name="allow" id="allow" value="1">
15
+ <label for="allow">Allow this application</label>
16
+
17
+ <input type="submit" value="Go!">
18
+ </form>
19
+
metadata ADDED
@@ -0,0 +1,220 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth2-provider-jonrowe
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - James Coglan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-10-17 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bcrypt-ruby
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activerecord
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: json
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: sqlite3-ruby
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :development
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: sinatra
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ type: :development
103
+ version_requirements: *id006
104
+ - !ruby/object:Gem::Dependency
105
+ name: thin
106
+ prerelease: false
107
+ requirement: &id007 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ type: :development
117
+ version_requirements: *id007
118
+ - !ruby/object:Gem::Dependency
119
+ name: factory_girl
120
+ prerelease: false
121
+ requirement: &id008 !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ hash: 3
127
+ segments:
128
+ - 0
129
+ version: "0"
130
+ type: :development
131
+ version_requirements: *id008
132
+ description:
133
+ email: james@songkick.com
134
+ executables: []
135
+
136
+ extensions: []
137
+
138
+ extra_rdoc_files:
139
+ - README.rdoc
140
+ files:
141
+ - README.rdoc
142
+ - spec/factories.rb
143
+ - spec/oauth2/model/authorization_spec.rb
144
+ - spec/oauth2/model/client_spec.rb
145
+ - spec/oauth2/model/resource_owner_spec.rb
146
+ - spec/oauth2/provider/access_token_spec.rb
147
+ - spec/oauth2/provider/authorization_spec.rb
148
+ - spec/oauth2/provider/exchange_spec.rb
149
+ - spec/oauth2/provider_spec.rb
150
+ - spec/request_helpers.rb
151
+ - spec/spec_helper.rb
152
+ - spec/test_app/helper.rb
153
+ - spec/test_app/provider/application.rb
154
+ - spec/test_app/provider/views/authorize.erb
155
+ - lib/oauth2/model/authorization.rb
156
+ - lib/oauth2/model/client.rb
157
+ - lib/oauth2/model/client_owner.rb
158
+ - lib/oauth2/model/hashing.rb
159
+ - lib/oauth2/model/resource_owner.rb
160
+ - lib/oauth2/model/schema.rb
161
+ - lib/oauth2/model.rb
162
+ - lib/oauth2/provider/access_token.rb
163
+ - lib/oauth2/provider/authorization.rb
164
+ - lib/oauth2/provider/error.rb
165
+ - lib/oauth2/provider/exchange.rb
166
+ - lib/oauth2/provider.rb
167
+ - lib/oauth2/router.rb
168
+ - example/application.rb
169
+ - example/config.ru
170
+ - example/environment.rb
171
+ - example/models/connection.rb
172
+ - example/models/note.rb
173
+ - example/models/user.rb
174
+ - example/public/style.css
175
+ - example/README.rdoc
176
+ - example/schema.rb
177
+ - example/views/authorize.erb
178
+ - example/views/create_user.erb
179
+ - example/views/home.erb
180
+ - example/views/layout.erb
181
+ - example/views/login.erb
182
+ - example/views/new_client.erb
183
+ - example/views/new_user.erb
184
+ - example/views/show_client.erb
185
+ homepage: http://www.songkick.com
186
+ licenses: []
187
+
188
+ post_install_message:
189
+ rdoc_options:
190
+ - --main
191
+ - README.rdoc
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ none: false
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ hash: 3
200
+ segments:
201
+ - 0
202
+ version: "0"
203
+ required_rubygems_version: !ruby/object:Gem::Requirement
204
+ none: false
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ hash: 3
209
+ segments:
210
+ - 0
211
+ version: "0"
212
+ requirements: []
213
+
214
+ rubyforge_project:
215
+ rubygems_version: 1.8.6
216
+ signing_key:
217
+ specification_version: 3
218
+ summary: Simple OAuth 2.0 provider toolkit
219
+ test_files: []
220
+