warden-github 1.2.0 → 1.3.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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -7
- data/lib/warden/github/membership_cache.rb +8 -4
- data/lib/warden/github/user.rb +7 -6
- data/lib/warden/github/version.rb +1 -1
- data/spec/unit/membership_cache_spec.rb +36 -15
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ea7be457e43d5d73595605d0f6107797007b36f0
|
|
4
|
+
data.tar.gz: 39e3ccc8f12771f33e456a7d2609bf9b810d0ddb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 223429bf4759f89e7eedccea6724c7a9f50526517302386cf63f11d2fcdfcbebd73a96eaab044ac388867cd3f7bfca9cbf241377aeb98eabd6bf6192e5d2e3c2
|
|
7
|
+
data.tar.gz: 30bc47e8d9762afe87fdf58567005a605a8381cc71bfc9904a04d5d258f27f44a64696dd7db71ded800672b8f1ea3715dcf92c943094a9b3d5e4783dc4cf9dec
|
data/.travis.yml
CHANGED
|
@@ -3,9 +3,13 @@ module Warden
|
|
|
3
3
|
# A hash subclass that acts as a cache for organization and team
|
|
4
4
|
# membership states. Only membership states that are true are cached. These
|
|
5
5
|
# are invalidated after a certain time.
|
|
6
|
-
class MembershipCache
|
|
6
|
+
class MembershipCache
|
|
7
7
|
CACHE_TIMEOUT = 60 * 5
|
|
8
8
|
|
|
9
|
+
def initialize(data)
|
|
10
|
+
@data = data
|
|
11
|
+
end
|
|
12
|
+
|
|
9
13
|
# Fetches a membership status by type and id (e.g. 'org', 'my_company')
|
|
10
14
|
# from cache. If no cached value is present or if the cached value
|
|
11
15
|
# expired, the block will be invoked and the return value, if true,
|
|
@@ -27,10 +31,10 @@ module Warden
|
|
|
27
31
|
private
|
|
28
32
|
|
|
29
33
|
def cached_membership_valid?(type, id)
|
|
30
|
-
timestamp = fetch(type).fetch(id)
|
|
34
|
+
timestamp = @data.fetch(type).fetch(id)
|
|
31
35
|
|
|
32
36
|
if Time.now.to_i > timestamp + CACHE_TIMEOUT
|
|
33
|
-
fetch(type).delete(id)
|
|
37
|
+
@data.fetch(type).delete(id)
|
|
34
38
|
false
|
|
35
39
|
else
|
|
36
40
|
true
|
|
@@ -40,7 +44,7 @@ module Warden
|
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
def cache_membership(type, id)
|
|
43
|
-
hash =
|
|
47
|
+
hash = @data[type] ||= {}
|
|
44
48
|
hash[id] = Time.now.to_i
|
|
45
49
|
end
|
|
46
50
|
end
|
data/lib/warden/github/user.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
module Warden
|
|
2
2
|
module GitHub
|
|
3
|
-
class User < Struct.new(:attribs, :token, :browser_session_id)
|
|
3
|
+
class User < Struct.new(:attribs, :token, :browser_session_id, :memberships)
|
|
4
4
|
ATTRIBUTES = %w[id login name gravatar_id avatar_url email company site_admin].freeze
|
|
5
5
|
|
|
6
6
|
def self.load(access_token, browser_session_id = nil)
|
|
@@ -32,7 +32,7 @@ module Warden
|
|
|
32
32
|
#
|
|
33
33
|
# Returns: true if the user is publicized as an org member
|
|
34
34
|
def organization_public_member?(org_name)
|
|
35
|
-
|
|
35
|
+
membership_cache.fetch_membership(:org_pub, org_name) do
|
|
36
36
|
api.organization_public_member?(org_name, login)
|
|
37
37
|
end
|
|
38
38
|
end
|
|
@@ -46,7 +46,7 @@ module Warden
|
|
|
46
46
|
#
|
|
47
47
|
# Returns: true if the user has access, false otherwise
|
|
48
48
|
def organization_member?(org_name)
|
|
49
|
-
|
|
49
|
+
membership_cache.fetch_membership(:org, org_name) do
|
|
50
50
|
api.organization_member?(org_name, login)
|
|
51
51
|
end
|
|
52
52
|
end
|
|
@@ -57,7 +57,7 @@ module Warden
|
|
|
57
57
|
#
|
|
58
58
|
# Returns: true if the user has access, false otherwise
|
|
59
59
|
def team_member?(team_id)
|
|
60
|
-
|
|
60
|
+
membership_cache.fetch_membership(:team, team_id) do
|
|
61
61
|
api.team_member?(team_id, login)
|
|
62
62
|
end
|
|
63
63
|
end
|
|
@@ -105,8 +105,9 @@ module Warden
|
|
|
105
105
|
|
|
106
106
|
private
|
|
107
107
|
|
|
108
|
-
def
|
|
109
|
-
|
|
108
|
+
def membership_cache
|
|
109
|
+
self.memberships ||= {}
|
|
110
|
+
@membership_cache ||= MembershipCache.new(memberships)
|
|
110
111
|
end
|
|
111
112
|
end
|
|
112
113
|
end
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe Warden::GitHub::MembershipCache do
|
|
4
|
-
subject(:cache) do
|
|
5
|
-
described_class.new
|
|
6
|
-
end
|
|
7
|
-
|
|
8
4
|
describe '#fetch_membership' do
|
|
9
5
|
it 'returns false by default' do
|
|
6
|
+
cache = described_class.new({})
|
|
10
7
|
cache.fetch_membership('foo', 'bar').should be_falsey
|
|
11
8
|
end
|
|
12
9
|
|
|
13
10
|
context 'when cache valid' do
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
let(:cache) do
|
|
12
|
+
described_class.new('foo' => {
|
|
13
|
+
'bar' => Time.now.to_i - described_class::CACHE_TIMEOUT + 5
|
|
14
|
+
})
|
|
17
15
|
end
|
|
18
16
|
|
|
19
17
|
it 'returns true' do
|
|
@@ -27,9 +25,10 @@ describe Warden::GitHub::MembershipCache do
|
|
|
27
25
|
end
|
|
28
26
|
|
|
29
27
|
context 'when cache expired' do
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
let(:cache) do
|
|
29
|
+
described_class.new('foo' => {
|
|
30
|
+
'bar' => Time.now.to_i - described_class::CACHE_TIMEOUT - 5
|
|
31
|
+
})
|
|
33
32
|
end
|
|
34
33
|
|
|
35
34
|
context 'when no block given' do
|
|
@@ -38,11 +37,6 @@ describe Warden::GitHub::MembershipCache do
|
|
|
38
37
|
end
|
|
39
38
|
end
|
|
40
39
|
|
|
41
|
-
it 'deletes the cached value' do
|
|
42
|
-
cache.fetch_membership('foo', 'bar')
|
|
43
|
-
cache['foo'].should_not have_key('bar')
|
|
44
|
-
end
|
|
45
|
-
|
|
46
40
|
it 'invokes the block' do
|
|
47
41
|
expect { |b| cache.fetch_membership('foo', 'bar', &b) }.
|
|
48
42
|
to yield_control
|
|
@@ -50,13 +44,40 @@ describe Warden::GitHub::MembershipCache do
|
|
|
50
44
|
end
|
|
51
45
|
|
|
52
46
|
it 'caches the value when block returns true' do
|
|
47
|
+
cache = described_class.new({})
|
|
53
48
|
cache.fetch_membership('foo', 'bar') { true }
|
|
54
49
|
cache.fetch_membership('foo', 'bar').should be_truthy
|
|
55
50
|
end
|
|
56
51
|
|
|
57
52
|
it 'does not cache the value when block returns false' do
|
|
53
|
+
cache = described_class.new({})
|
|
58
54
|
cache.fetch_membership('foo', 'bar') { false }
|
|
59
55
|
cache.fetch_membership('foo', 'bar').should be_falsey
|
|
60
56
|
end
|
|
61
57
|
end
|
|
58
|
+
|
|
59
|
+
it 'uses the hash that is passed to the initializer as storage' do
|
|
60
|
+
time = Time.now.to_i
|
|
61
|
+
hash = {
|
|
62
|
+
'foo' => {
|
|
63
|
+
'valid' => time,
|
|
64
|
+
'timedout' => time - described_class::CACHE_TIMEOUT - 5
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
cache = described_class.new(hash)
|
|
68
|
+
|
|
69
|
+
# Verify that existing data in the hash is used:
|
|
70
|
+
expect(cache.fetch_membership('foo', 'valid')).to be_true
|
|
71
|
+
expect(cache.fetch_membership('foo', 'timedout')).to be_false
|
|
72
|
+
|
|
73
|
+
# Add new data to the hash:
|
|
74
|
+
cache.fetch_membership('foo', 'new') { true }
|
|
75
|
+
cache.fetch_membership('foo', 'false') { false }
|
|
76
|
+
|
|
77
|
+
# Verify the final hash state:
|
|
78
|
+
expect(hash).to eq('foo' => {
|
|
79
|
+
'valid' => time,
|
|
80
|
+
'new' => time
|
|
81
|
+
})
|
|
82
|
+
end
|
|
62
83
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: warden-github
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Corey Donohoe
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-04-20 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: warden
|
|
@@ -250,7 +250,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
250
250
|
version: '0'
|
|
251
251
|
requirements: []
|
|
252
252
|
rubyforge_project: warden-github
|
|
253
|
-
rubygems_version: 2.
|
|
253
|
+
rubygems_version: 2.5.1
|
|
254
254
|
signing_key:
|
|
255
255
|
specification_version: 4
|
|
256
256
|
summary: A warden strategy for easy oauth integration with github
|