rapid-rack 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ module RapidRack
2
+ RSpec.describe Engine, type: :feature do
3
+ let(:opts) { YAML.load_file('spec/dummy/config/rapidconnect.yml') }
4
+ let(:issuer) { opts['issuer'] }
5
+ let(:audience) { opts['audience'] }
6
+ let(:url) { opts['url'] }
7
+ let(:secret) { opts['secret'] }
8
+ let(:receiver_class) { 'TestReceiver' }
9
+ let(:handler) { nil }
10
+ let(:app) { Rails.application }
11
+
12
+ # Unfortunately the neatest way to get access to a routed application in
13
+ # the engine.
14
+ let(:engine_app) { RapidRack::Engine.routes.routes.routes[0].app }
15
+
16
+ subject { last_response }
17
+
18
+ before do
19
+ error_handler = handler.try(:constantize).try(:new) || engine_app
20
+ engine_app.instance_variable_set(:@error_handler, error_handler)
21
+ engine_app.instance_variable_set(:@receiver, receiver_class.constantize)
22
+ end
23
+
24
+ it_behaves_like 'an authenticator'
25
+
26
+ context 'full integration' do
27
+ let(:receiver_class) do
28
+ build_class do
29
+ include DefaultReceiver
30
+ include RedisRegistry
31
+
32
+ def map_attributes(attrs)
33
+ {
34
+ targeted_id: attrs['edupersontargetedid'],
35
+ email: attrs['mail'],
36
+ name: attrs['displayname']
37
+ }
38
+ end
39
+
40
+ def subject(attrs)
41
+ identifier = attrs.slice(:targeted_id)
42
+ TestSubject.find_or_initialize_by(identifier).tap do |subject|
43
+ subject.update_attributes!(attrs)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ let(:attrs) do
50
+ {
51
+ cn: 'Test User', displayname: 'Test User X', surname: 'User',
52
+ givenname: 'Test', mail: 'testuser@example.com', o: 'Test Org',
53
+ edupersonscopedaffiliation: 'member@example.com',
54
+ edupersonprincipalname: 'testuser@example.com',
55
+ edupersontargetedid: "#{issuer}!#{audience}!abcd"
56
+ }
57
+ end
58
+
59
+ let(:valid_claims) do
60
+ {
61
+ aud: audience, iss: issuer, iat: Time.now, typ: 'authnresponse',
62
+ nbf: 1.minute.ago, exp: 2.minutes.from_now,
63
+ jti: 'accept', :'https://aaf.edu.au/attributes' => attrs
64
+ }
65
+ end
66
+
67
+ let(:receiver) { receiver_class.constantize.new }
68
+ let(:assertion) { JSON::JWT.new(claims).sign(secret).to_s }
69
+ let(:claims) { valid_claims }
70
+ let(:session) { {} }
71
+
72
+ def run
73
+ post '/auth/jwt', assertion: assertion
74
+ end
75
+
76
+ it 'creates the subject' do
77
+ expect { run }.to change(TestSubject, :count).by(1)
78
+ end
79
+
80
+ it 'redirects to /' do
81
+ run
82
+ expect(last_response).to be_redirect
83
+ expect(last_response['Location']).to eq('/')
84
+ end
85
+
86
+ it 'sets the session' do
87
+ run
88
+ expect(last_request.session[:subject_id]).to eq(TestSubject.last.id)
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,27 @@
1
+ require 'fakeredis'
2
+ require 'redis'
3
+
4
+ module RapidRack
5
+ RSpec.describe RedisRegistry do
6
+ let(:overrides) { Module.new }
7
+
8
+ subject do
9
+ klass = Class.new
10
+ klass.send(:extend, described_class)
11
+ klass.send(:extend, overrides)
12
+ end
13
+
14
+ context '#register_jti' do
15
+ let(:value) { 'abcd' }
16
+
17
+ it 'returns true for a new jti' do
18
+ expect(subject.register_jti(value)).to be_truthy
19
+ end
20
+
21
+ it 'returns false for a previously seen jti' do
22
+ subject.register_jti(value)
23
+ expect(subject.register_jti(value)).to be_falsey
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,45 @@
1
+ require 'simplecov'
2
+
3
+ ENV['RAILS_ENV'] ||= 'test'
4
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
5
+
6
+ require 'rspec/rails'
7
+ require 'capybara/rspec'
8
+ require 'fakeredis'
9
+
10
+ require 'rapid_rack'
11
+
12
+ Dir['./spec/support/*.rb'].each { |f| require f }
13
+
14
+ RSpec.configure do |config|
15
+ config.before(:suite) do
16
+ load Rails.root.join('db/schema.rb')
17
+ end
18
+
19
+ config.before { Redis::Connection::Memory.reset_all_databases }
20
+
21
+ config.around do |example|
22
+ ActiveRecord::Base.transaction do
23
+ example.run
24
+ fail ActiveRecord::Rollback
25
+ end
26
+ end
27
+
28
+ config.expect_with :rspec do |expectations|
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ config.mock_with :rspec do |mocks|
33
+ mocks.verify_partial_doubles = true
34
+ end
35
+
36
+ config.filter_run :focus
37
+ config.run_all_when_everything_filtered = true
38
+ config.disable_monkey_patching!
39
+
40
+ config.order = :random
41
+ Kernel.srand config.seed
42
+
43
+ config.include Rack::Test::Methods
44
+ config.include TemporaryTestClass
45
+ end
@@ -0,0 +1,216 @@
1
+ shared_examples 'an authenticator' do
2
+ let(:handler) { nil }
3
+ let(:receiver) do
4
+ build_class do
5
+ def receive(_, _)
6
+ [200, {}, ['Permitted']]
7
+ end
8
+
9
+ def logout(_)
10
+ [200, {}, ['Logged Out!']]
11
+ end
12
+
13
+ def register_jti(*)
14
+ true
15
+ end
16
+ end
17
+ end
18
+
19
+ context 'get /nonexistent' do
20
+ before { get '/auth/nonexistent' }
21
+ it { is_expected.to be_not_found }
22
+ end
23
+
24
+ context 'get /login' do
25
+ before { get '/auth/login' }
26
+
27
+ it 'redirects to the url' do
28
+ expect(last_response).to be_redirect
29
+ expect(last_response['Location']).to eq(url)
30
+ end
31
+ end
32
+
33
+ context 'post /login' do
34
+ before { post '/auth/login' }
35
+ it { is_expected.to be_method_not_allowed }
36
+ end
37
+
38
+ context 'get /logout' do
39
+ before { get '/auth/logout' }
40
+ it 'responds using the receiver' do
41
+ expect(last_response).to be_successful
42
+ expect(last_response.body).to have_content('Logged Out!')
43
+ end
44
+ end
45
+
46
+ context 'post /logout' do
47
+ before { post '/auth/logout' }
48
+ it { is_expected.to be_method_not_allowed }
49
+ end
50
+
51
+ context 'get /jwt' do
52
+ before { get '/auth/jwt' }
53
+ it { is_expected.to be_method_not_allowed }
54
+ end
55
+
56
+ context 'post /jwt' do
57
+ before { post '/auth/jwt', assertion: assertion }
58
+
59
+ let(:attrs) do
60
+ {
61
+ cn: 'Test User', displayname: 'Test User X', surname: 'User',
62
+ givenname: 'Test', mail: 'testuser@example.com', o: 'Test Org',
63
+ edupersonscopedaffiliation: 'member@example.com',
64
+ edupersonprincipalname: 'testuser@example.com',
65
+ edupersontargetedid: "#{issuer}!#{audience}!abcd"
66
+ }
67
+ end
68
+
69
+ let(:valid_claims) do
70
+ {
71
+ aud: audience, iss: issuer, iat: Time.now, typ: 'authnresponse',
72
+ nbf: 1.minute.ago, exp: 2.minutes.from_now,
73
+ jti: 'accept', :'https://aaf.edu.au/attributes' => attrs
74
+ }
75
+ end
76
+
77
+ let(:assertion) { JSON::JWT.new(claims).sign(secret).to_s }
78
+
79
+ context 'with an invalid assertion' do
80
+ let(:assertion) { 'x.y.z' }
81
+ it { is_expected.to be_bad_request }
82
+ end
83
+
84
+ context 'with a valid assertion' do
85
+ let(:claims) { valid_claims }
86
+
87
+ it 'responds using the receiver' do
88
+ expect(last_response).to be_successful
89
+ expect(last_response.body).to have_content('Permitted')
90
+ end
91
+ end
92
+
93
+ shared_examples 'an invalid claims set' do |field|
94
+ it { is_expected.to be_bad_request }
95
+
96
+ context 'with an error handler' do
97
+ let(:handler) do
98
+ build_class do
99
+ def handle(_env, exception)
100
+ [403, {}, ["Surprise!\n", exception.message]]
101
+ end
102
+ end
103
+ end
104
+
105
+ it 'uses the error handler to respond' do
106
+ expect(subject).to be_forbidden
107
+ expect(subject.body).to have_content('Surprise!')
108
+ end
109
+
110
+ it 'complains about the invalid field' do
111
+ val = claims[field]
112
+ expected = if val.nil?
113
+ "nil #{field}"
114
+ else
115
+ "bad #{field}: #{val}"
116
+ end
117
+
118
+ expect(subject.body).to have_content(expected)
119
+ end
120
+ end
121
+ end
122
+
123
+ context 'with a nil audience' do
124
+ let(:claims) { valid_claims.merge(aud: nil) }
125
+ it_behaves_like 'an invalid claims set', :aud
126
+ end
127
+
128
+ context 'with an invalid audience' do
129
+ let(:claims) { valid_claims.merge(aud: 'invalid') }
130
+ it_behaves_like 'an invalid claims set', :aud
131
+ end
132
+
133
+ context 'with a nil issuer' do
134
+ let(:claims) { valid_claims.merge(iss: nil) }
135
+ it_behaves_like 'an invalid claims set', :iss
136
+ end
137
+
138
+ context 'with an invalid issuer' do
139
+ let(:claims) { valid_claims.merge(iss: 'invalid') }
140
+ it_behaves_like 'an invalid claims set', :iss
141
+ end
142
+
143
+ context 'with a nil type' do
144
+ let(:claims) { valid_claims.merge(typ: nil) }
145
+ it_behaves_like 'an invalid claims set', :typ
146
+ end
147
+
148
+ context 'with an invalid type' do
149
+ let(:claims) { valid_claims.merge(typ: 'blarghn') }
150
+ it_behaves_like 'an invalid claims set', :typ
151
+ end
152
+
153
+ context 'with a nil jti' do
154
+ let(:claims) { valid_claims.merge(jti: nil) }
155
+ it_behaves_like 'an invalid claims set', :jti
156
+ end
157
+
158
+ context 'with a replayed jti' do
159
+ let(:receiver) do
160
+ build_class do
161
+ def register_jti(*)
162
+ false
163
+ end
164
+ end
165
+ end
166
+
167
+ let(:claims) { valid_claims.merge(jti: 'blarghn') }
168
+ it_behaves_like 'an invalid claims set', :jti
169
+ end
170
+
171
+ context 'with a nil nbf' do
172
+ let(:claims) { valid_claims.merge(nbf: nil) }
173
+ it_behaves_like 'an invalid claims set', :nbf
174
+ end
175
+
176
+ context 'with an invalid nbf' do
177
+ let(:claims) { valid_claims.merge(nbf: 2.minutes.from_now) }
178
+ it_behaves_like 'an invalid claims set', :nbf
179
+ end
180
+
181
+ context 'with a non-numeric nbf' do
182
+ let(:claims) { valid_claims.merge(nbf: 'a') }
183
+ it_behaves_like 'an invalid claims set', :nbf
184
+ end
185
+
186
+ context 'with a nil exp' do
187
+ let(:claims) { valid_claims.merge(exp: nil) }
188
+ it_behaves_like 'an invalid claims set', :exp
189
+ end
190
+
191
+ context 'with an invalid exp' do
192
+ let(:claims) { valid_claims.merge(exp: 1.minute.ago) }
193
+ it_behaves_like 'an invalid claims set', :exp
194
+ end
195
+
196
+ context 'with a non-numeric exp' do
197
+ let(:claims) { valid_claims.merge(exp: 'a') }
198
+ it_behaves_like 'an invalid claims set', :exp
199
+ end
200
+
201
+ context 'with a nil iat' do
202
+ let(:claims) { valid_claims.merge(iat: nil) }
203
+ it_behaves_like 'an invalid claims set', :iat
204
+ end
205
+
206
+ context 'with an invalid iat' do
207
+ let(:claims) { valid_claims.merge(iat: 10.minutes.ago) }
208
+ it_behaves_like 'an invalid claims set', :iat
209
+ end
210
+
211
+ context 'with a non-numeric iat' do
212
+ let(:claims) { valid_claims.merge(iat: 'a') }
213
+ it_behaves_like 'an invalid claims set', :iat
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,8 @@
1
+ module TemporaryTestClass
2
+ def build_class(&bl)
3
+ klass = Class.new(&bl)
4
+ name = "TestClass#{SecureRandom.hex}"
5
+ RapidRack.const_set(name, klass)
6
+ "RapidRack::#{name}"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,296 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rapid-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shaun Mangelsdorf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: capybara
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 4.1.7
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 4.1.7
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: fakeredis
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: redis
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: guard
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: guard-rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: guard-rubocop
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: guard-bundler
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ description:
210
+ email:
211
+ - s.mangelsdorf@gmail.com
212
+ executables: []
213
+ extensions: []
214
+ extra_rdoc_files: []
215
+ files:
216
+ - ".gitignore"
217
+ - ".rspec"
218
+ - ".rubocop.yml"
219
+ - ".simplecov"
220
+ - Gemfile
221
+ - Guardfile
222
+ - LICENSE
223
+ - README.md
224
+ - Rakefile
225
+ - config/routes.rb
226
+ - lib/rapid-rack.rb
227
+ - lib/rapid_rack.rb
228
+ - lib/rapid_rack/authenticator.rb
229
+ - lib/rapid_rack/default_receiver.rb
230
+ - lib/rapid_rack/engine.rb
231
+ - lib/rapid_rack/redis_registry.rb
232
+ - lib/rapid_rack/version.rb
233
+ - rapid-rack.gemspec
234
+ - spec/dummy/app/models/test_subject.rb
235
+ - spec/dummy/config.ru
236
+ - spec/dummy/config/application.rb
237
+ - spec/dummy/config/boot.rb
238
+ - spec/dummy/config/database.yml
239
+ - spec/dummy/config/environment.rb
240
+ - spec/dummy/config/rapidconnect.yml
241
+ - spec/dummy/config/routes.rb
242
+ - spec/dummy/config/secrets.yml
243
+ - spec/dummy/db/schema.rb
244
+ - spec/dummy/lib/test_error_handler.rb
245
+ - spec/dummy/lib/test_receiver.rb
246
+ - spec/lib/rapid_rack/authenticator_spec.rb
247
+ - spec/lib/rapid_rack/default_receiver_spec.rb
248
+ - spec/lib/rapid_rack/engine_spec.rb
249
+ - spec/lib/rapid_rack/redis_registry_spec.rb
250
+ - spec/spec_helper.rb
251
+ - spec/support/authenticator_examples.rb
252
+ - spec/support/temporary_test_class.rb
253
+ homepage: https://github.com/ausaccessfed/rapid-rack
254
+ licenses:
255
+ - Apache-2.0
256
+ metadata: {}
257
+ post_install_message:
258
+ rdoc_options: []
259
+ require_paths:
260
+ - lib
261
+ required_ruby_version: !ruby/object:Gem::Requirement
262
+ requirements:
263
+ - - ">="
264
+ - !ruby/object:Gem::Version
265
+ version: '0'
266
+ required_rubygems_version: !ruby/object:Gem::Requirement
267
+ requirements:
268
+ - - ">="
269
+ - !ruby/object:Gem::Version
270
+ version: '0'
271
+ requirements: []
272
+ rubyforge_project:
273
+ rubygems_version: 2.2.2
274
+ signing_key:
275
+ specification_version: 4
276
+ summary: Rack middleware for AAF Rapid Connect authentication.
277
+ test_files:
278
+ - spec/dummy/app/models/test_subject.rb
279
+ - spec/dummy/config.ru
280
+ - spec/dummy/config/application.rb
281
+ - spec/dummy/config/boot.rb
282
+ - spec/dummy/config/database.yml
283
+ - spec/dummy/config/environment.rb
284
+ - spec/dummy/config/rapidconnect.yml
285
+ - spec/dummy/config/routes.rb
286
+ - spec/dummy/config/secrets.yml
287
+ - spec/dummy/db/schema.rb
288
+ - spec/dummy/lib/test_error_handler.rb
289
+ - spec/dummy/lib/test_receiver.rb
290
+ - spec/lib/rapid_rack/authenticator_spec.rb
291
+ - spec/lib/rapid_rack/default_receiver_spec.rb
292
+ - spec/lib/rapid_rack/engine_spec.rb
293
+ - spec/lib/rapid_rack/redis_registry_spec.rb
294
+ - spec/spec_helper.rb
295
+ - spec/support/authenticator_examples.rb
296
+ - spec/support/temporary_test_class.rb