fridge 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/fridge.gemspec +1 -1
- data/lib/fridge/access_token.rb +4 -0
- data/lib/fridge/version.rb +1 -1
- data/spec/fixtures/app.rb +0 -4
- data/spec/fridge/access_token_spec.rb +11 -0
- data/spec/fridge/rails_helpers_spec.rb +160 -161
- data/spec/spec_helper.rb +11 -3
- metadata +5 -7
- data/spec/fixtures/controller.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 73170339e572589b38ca8c30c1bab638b80102caf10ea71572ebdb5168e28397
|
4
|
+
data.tar.gz: 89ed3cdee4f57bee03d128a155ea3ec8d9b29dfb3343d05b67fe343ac6fc12e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d1b78d332804fe1680c5df49b25fa83e47603281d950705f9c8775e37ad3821971f53670191bb493fc970d9d7d7cf08bbd6a54fd26f9c82402d1a545b49bc964
|
7
|
+
data.tar.gz: 9d98dead583203b74b41326d7b3b325fb7507ac3a1b2a9fbfbf5bb1120247327a4366f74544efa4f57061f8e340ba918cdd4719c61a5e98a0b992c7a144b8686
|
data/fridge.gemspec
CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.add_development_dependency 'aptible-tasks'
|
27
27
|
spec.add_development_dependency 'rake'
|
28
28
|
spec.add_development_dependency 'rails'
|
29
|
-
spec.add_development_dependency 'rspec', '~>
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
30
30
|
spec.add_development_dependency 'rspec-rails'
|
31
31
|
spec.add_development_dependency 'pry'
|
32
32
|
end
|
data/lib/fridge/access_token.rb
CHANGED
@@ -124,6 +124,8 @@ module Fridge
|
|
124
124
|
# mapping from Fridge to JWT and vice-versa.
|
125
125
|
|
126
126
|
def encode_for_jwt(hash)
|
127
|
+
hash = hash.dup
|
128
|
+
|
127
129
|
out = {
|
128
130
|
id: hash.delete(:id),
|
129
131
|
iss: hash.delete(:issuer),
|
@@ -145,6 +147,8 @@ module Fridge
|
|
145
147
|
end
|
146
148
|
|
147
149
|
def decode_from_jwt(hash)
|
150
|
+
hash = hash.dup
|
151
|
+
|
148
152
|
out = {
|
149
153
|
id: hash.delete('id'),
|
150
154
|
issuer: hash.delete('iss'),
|
data/lib/fridge/version.rb
CHANGED
data/spec/fixtures/app.rb
CHANGED
@@ -139,6 +139,17 @@ describe Fridge::AccessToken do
|
|
139
139
|
new = described_class.new(subject.serialize)
|
140
140
|
expect(new.actor).to eq(actor)
|
141
141
|
end
|
142
|
+
|
143
|
+
it 'should be idempotent' do
|
144
|
+
subject = described_class.new(options)
|
145
|
+
expect(subject.serialize).to eq(subject.serialize)
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'should be idempotent with an actor' do
|
149
|
+
actor = { subject: 'foo', username: 'test', actor: { subject: 'bar' } }
|
150
|
+
subject = described_class.new(options.merge(actor: actor))
|
151
|
+
expect(subject.serialize).to eq(subject.serialize)
|
152
|
+
end
|
142
153
|
end
|
143
154
|
|
144
155
|
describe '#expired?' do
|
@@ -1,218 +1,217 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'fixtures/app'
|
3
|
-
require 'fixtures/controller'
|
4
|
-
require 'rspec/rails'
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
context Fridge::RailsHelpers do
|
9
|
-
let(:organization_url) do
|
10
|
-
"https://auth.aptible.com/users/#{SecureRandom.uuid}"
|
11
|
-
end
|
12
|
-
let(:private_key) { OpenSSL::PKey::RSA.new(1024) }
|
13
|
-
let(:public_key) { OpenSSL::PKey::RSA.new(private_key.public_key) }
|
4
|
+
describe Fridge::RailsHelpers do
|
5
|
+
include RSpec::Rails::ControllerExampleGroup
|
14
6
|
|
15
|
-
|
16
|
-
{
|
17
|
-
subject: "https://auth.aptible.com/users/#{SecureRandom.uuid}",
|
18
|
-
expires_at: Time.now + 3600
|
19
|
-
}
|
20
|
-
end
|
21
|
-
let(:access_token) { Fridge::AccessToken.new(options) }
|
7
|
+
controller(ActionController::Base) { include Fridge::RailsHelpers }
|
22
8
|
|
23
|
-
|
9
|
+
let(:organization_url) do
|
10
|
+
"https://auth.aptible.com/users/#{SecureRandom.uuid}"
|
11
|
+
end
|
12
|
+
let(:private_key) { OpenSSL::PKey::RSA.new(1024) }
|
13
|
+
let(:public_key) { OpenSSL::PKey::RSA.new(private_key.public_key) }
|
14
|
+
|
15
|
+
let(:options) do
|
16
|
+
{
|
17
|
+
subject: "https://auth.aptible.com/users/#{SecureRandom.uuid}",
|
18
|
+
expires_at: Time.now + 3600
|
19
|
+
}
|
20
|
+
end
|
21
|
+
let(:access_token) { Fridge::AccessToken.new(options) }
|
24
22
|
|
25
|
-
|
26
|
-
before { Fridge.configuration.public_key = public_key.to_s }
|
23
|
+
let(:cookies) { controller.send(:cookies) }
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
request.env['HTTP_AUTHORIZATION'] = 'Bearer foobar'
|
31
|
-
expect(controller.bearer_token).to eq 'foobar'
|
32
|
-
end
|
25
|
+
before { Fridge.configuration.private_key = private_key.to_s }
|
26
|
+
before { Fridge.configuration.public_key = public_key.to_s }
|
33
27
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
28
|
+
describe '#bearer_token' do
|
29
|
+
it 'returns the bearer token from the Authorization: header' do
|
30
|
+
request.env['HTTP_AUTHORIZATION'] = 'Bearer foobar'
|
31
|
+
expect(controller.bearer_token).to eq 'foobar'
|
38
32
|
end
|
39
33
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
34
|
+
it 'returns nil in the absence of an Authorization: header' do
|
35
|
+
request.env['HTTP_AUTHORIZATION'] = nil
|
36
|
+
expect(controller.bearer_token).to be_nil
|
37
|
+
end
|
38
|
+
end
|
45
39
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
40
|
+
describe '#token_subject' do
|
41
|
+
it 'returns the subject encoded in the token' do
|
42
|
+
controller.stub(:current_token) { access_token }
|
43
|
+
expect(controller.token_subject).to eq access_token.subject
|
50
44
|
end
|
51
45
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
46
|
+
it 'returns nil if no token is present' do
|
47
|
+
controller.stub(:current_token) { nil }
|
48
|
+
expect(controller.token_subject).to be_nil
|
49
|
+
end
|
50
|
+
end
|
57
51
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
52
|
+
describe '#token_scope' do
|
53
|
+
it 'returns the scope encoded in the token' do
|
54
|
+
controller.stub(:current_token) { access_token }
|
55
|
+
expect(controller.token_scope).to eq access_token.scope
|
62
56
|
end
|
63
57
|
|
64
|
-
|
65
|
-
|
58
|
+
it 'returns nil if no token is present' do
|
59
|
+
controller.stub(:current_token) { nil }
|
60
|
+
expect(controller.token_scope).to be_nil
|
61
|
+
end
|
62
|
+
end
|
66
63
|
|
67
|
-
|
68
|
-
|
69
|
-
expect { controller.current_token }.to raise_error Fridge::InvalidToken
|
70
|
-
end
|
64
|
+
describe '#current_token' do
|
65
|
+
before { controller.stub(:bearer_token) { access_token.serialize } }
|
71
66
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
67
|
+
it 'should raise an error if the token is not a valid JWT' do
|
68
|
+
controller.stub(:bearer_token) { 'foobar' }
|
69
|
+
expect { controller.current_token }.to raise_error Fridge::InvalidToken
|
70
|
+
end
|
76
71
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
72
|
+
it 'should raise an error if the token has expired' do
|
73
|
+
access_token.expires_at = Time.now - 3600
|
74
|
+
expect { controller.current_token }.to raise_error Fridge::InvalidToken
|
75
|
+
end
|
81
76
|
|
82
|
-
|
83
|
-
|
84
|
-
|
77
|
+
it 'should raise an error if custom validation fails' do
|
78
|
+
Fridge.configuration.validator = ->(_) { false }
|
79
|
+
expect { controller.current_token }.to raise_error Fridge::InvalidToken
|
80
|
+
end
|
85
81
|
|
86
|
-
|
87
|
-
|
88
|
-
end
|
82
|
+
it 'should not raise an error if a valid token is passed' do
|
83
|
+
expect { controller.current_token }.not_to raise_error
|
89
84
|
end
|
90
85
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
86
|
+
it 'should return the token if a valid token is passed' do
|
87
|
+
expect(controller.current_token.id).to eq access_token.id
|
88
|
+
end
|
89
|
+
end
|
96
90
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
91
|
+
describe '#session_subject' do
|
92
|
+
it 'returns the subject encoded in the session' do
|
93
|
+
controller.stub(:session_token) { access_token }
|
94
|
+
expect(controller.session_subject).to eq access_token.subject
|
101
95
|
end
|
102
96
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
end
|
97
|
+
it 'returns nil if no session is present' do
|
98
|
+
controller.stub(:session_token) { nil }
|
99
|
+
expect(controller.session_subject).to be_nil
|
100
|
+
end
|
101
|
+
end
|
109
102
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
103
|
+
describe '#session_token' do
|
104
|
+
it 'should delete all cookies on error' do
|
105
|
+
cookies[:fridge_session] = 'foobar'
|
106
|
+
controller.session_token
|
107
|
+
expect(cookies.deleted?(:fridge_session, domain: :all)).to be true
|
108
|
+
end
|
114
109
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
110
|
+
it 'should return nil on error' do
|
111
|
+
cookies[:fridge_session] = 'foobar'
|
112
|
+
expect(controller.session_token).to be_nil
|
113
|
+
end
|
119
114
|
|
120
|
-
|
121
|
-
|
115
|
+
it 'should return the token stored in :fridge_session' do
|
116
|
+
cookies[:fridge_session] = access_token.serialize
|
117
|
+
expect(controller.session_token.id).to eq access_token.id
|
118
|
+
end
|
122
119
|
|
123
|
-
|
124
|
-
|
125
|
-
expect(controller.session_token.scope).to eq 'read'
|
126
|
-
end
|
120
|
+
context 'with a non-:read scope' do
|
121
|
+
before { options.merge!(scope: 'manage') }
|
127
122
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
end
|
123
|
+
it 'should downgrade the token' do
|
124
|
+
cookies[:fridge_session] = access_token.serialize
|
125
|
+
expect(controller.session_token.scope).to eq 'read'
|
132
126
|
end
|
133
|
-
end
|
134
127
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
expect(controller.validate_token(access_token)).to be false
|
128
|
+
it 'should not change the validity of a token' do
|
129
|
+
cookies[:fridge_session] = access_token.serialize
|
130
|
+
expect(controller.session_token).to be_valid
|
139
131
|
end
|
132
|
+
end
|
133
|
+
end
|
140
134
|
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
135
|
+
describe '#validate_token' do
|
136
|
+
it 'should return false if the token is invalid' do
|
137
|
+
Fridge.configuration.validator = ->(_) { false }
|
138
|
+
expect(controller.validate_token(access_token)).to be false
|
139
|
+
end
|
145
140
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
end
|
141
|
+
it 'should return false if the token validator fails' do
|
142
|
+
Fridge.configuration.validator = ->(_) { raise 'Foobar' }
|
143
|
+
expect(controller.validate_token(access_token)).to be false
|
150
144
|
end
|
151
145
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
146
|
+
it 'should return the token if valid' do
|
147
|
+
Fridge.configuration.validator = ->(_) { true }
|
148
|
+
expect(controller.validate_token(access_token)).to eq access_token
|
149
|
+
end
|
150
|
+
end
|
157
151
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
152
|
+
describe '#validate_token' do
|
153
|
+
it 'should raise an exception if the token is invalid' do
|
154
|
+
Fridge.configuration.validator = ->(_) { false }
|
155
|
+
expect { controller.validate_token!(access_token) }.to raise_error
|
162
156
|
end
|
163
157
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
controller.sessionize_token(access_token)
|
168
|
-
expect(cookies[:fridge_session]).to eq access_token.serialize
|
169
|
-
end
|
158
|
+
it 'should return the token if valid' do
|
159
|
+
Fridge.configuration.validator = ->(_) { true }
|
160
|
+
expect(controller.validate_token!(access_token)).to eq access_token
|
170
161
|
end
|
162
|
+
end
|
171
163
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
164
|
+
describe '#sessionize_token' do
|
165
|
+
it 'should set a session cookie' do
|
166
|
+
Rails.stub_chain(:env, :development?) { false }
|
167
|
+
controller.sessionize_token(access_token)
|
168
|
+
expect(cookies[:fridge_session]).to eq access_token.serialize
|
177
169
|
end
|
170
|
+
end
|
178
171
|
|
179
|
-
|
180
|
-
|
172
|
+
describe '#fridge_cookie_name' do
|
173
|
+
it 'is configurable' do
|
174
|
+
Fridge.configuration.cookie_name = 'foobar'
|
175
|
+
expect(controller.fridge_cookie_name).to eq 'foobar'
|
176
|
+
end
|
177
|
+
end
|
181
178
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
179
|
+
describe '#write_shared_cookie' do
|
180
|
+
before { Rails.stub_chain(:env, :development?) { false } }
|
181
|
+
|
182
|
+
it 'should save cookie' do
|
183
|
+
controller.write_shared_cookie(:organization_url, organization_url)
|
184
|
+
expect(cookies[:organization_url]).to eq organization_url
|
186
185
|
end
|
186
|
+
end
|
187
187
|
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
end
|
188
|
+
describe '#read_shared_cookie' do
|
189
|
+
it 'should read cookie' do
|
190
|
+
cookies[:organization_url] = { value: organization_url }
|
191
|
+
expect(controller.read_shared_cookie(:organization_url)).to(
|
192
|
+
eq organization_url
|
193
|
+
)
|
195
194
|
end
|
195
|
+
end
|
196
196
|
|
197
|
-
|
198
|
-
|
197
|
+
describe '#delete_shared_cookie' do
|
198
|
+
before { Rails.stub_chain(:env, :development?) { false } }
|
199
199
|
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
end
|
200
|
+
it 'should delete cookie' do
|
201
|
+
controller.write_shared_cookie(:organization_url, organization_url)
|
202
|
+
controller.delete_shared_cookie(:organization_url)
|
203
|
+
expect(cookies[:organization_url]).to be_nil
|
205
204
|
end
|
205
|
+
end
|
206
206
|
|
207
|
-
|
208
|
-
|
207
|
+
describe '#fridge_cookie_options' do
|
208
|
+
before { Rails.stub_chain(:env, :development?) { false } }
|
209
209
|
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
end
|
210
|
+
it 'are configurable' do
|
211
|
+
Fridge.configuration.cookie_options = { foobar: true }
|
212
|
+
options = controller.fridge_cookie_options
|
213
|
+
expect(options[:domain]).to eq :all
|
214
|
+
expect(options[:foobar]).to eq true
|
216
215
|
end
|
217
216
|
end
|
218
217
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,14 +1,22 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
2
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
3
|
|
4
|
+
require 'active_support/all'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_dispatch'
|
7
|
+
require 'action_view'
|
8
|
+
|
9
|
+
require 'fridge'
|
10
|
+
require 'fridge/rails_helpers'
|
11
|
+
|
12
|
+
require 'rspec'
|
13
|
+
require 'rspec/rails'
|
14
|
+
|
4
15
|
# Load shared spec files
|
5
16
|
Dir["#{File.dirname(__FILE__)}/shared/**/*.rb"].each do |file|
|
6
17
|
require file
|
7
18
|
end
|
8
19
|
|
9
|
-
# Require library up front
|
10
|
-
require 'fridge'
|
11
|
-
|
12
20
|
RSpec.configure do |config|
|
13
21
|
config.before { Fridge.configuration.reset }
|
14
22
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fridge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank Macreery
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gem_config
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '3.0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '3.0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rspec-rails
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -160,7 +160,6 @@ files:
|
|
160
160
|
- lib/fridge/serialization_error.rb
|
161
161
|
- lib/fridge/version.rb
|
162
162
|
- spec/fixtures/app.rb
|
163
|
-
- spec/fixtures/controller.rb
|
164
163
|
- spec/fridge/access_token_spec.rb
|
165
164
|
- spec/fridge/rails_helpers_spec.rb
|
166
165
|
- spec/spec_helper.rb
|
@@ -184,13 +183,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
183
|
version: '0'
|
185
184
|
requirements: []
|
186
185
|
rubyforge_project:
|
187
|
-
rubygems_version: 2.
|
186
|
+
rubygems_version: 2.7.6
|
188
187
|
signing_key:
|
189
188
|
specification_version: 4
|
190
189
|
summary: Token validation for distributed resource servers
|
191
190
|
test_files:
|
192
191
|
- spec/fixtures/app.rb
|
193
|
-
- spec/fixtures/controller.rb
|
194
192
|
- spec/fridge/access_token_spec.rb
|
195
193
|
- spec/fridge/rails_helpers_spec.rb
|
196
194
|
- spec/spec_helper.rb
|