real_savvy 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1952776f35bc650b82d5f3686ebdf1f2d959b68
4
- data.tar.gz: 2fe7b245b44ddb8f93620fd3a098b02d76bfa592
3
+ metadata.gz: a089cce034187b8f9c6592b50b73e0ff2bcb4416
4
+ data.tar.gz: d98f257df54ca0c40f9565e9b8be9fa3958b0c4a
5
5
  SHA512:
6
- metadata.gz: 8e2bbbd513d79ecf979a5ac45f3c3d94ac2f4134eda5aee5d70e6336a4b2d227131f8f87b3b8acff1e6ded6717c69a124682b2408ddc5f7c1b47dc8d551b912a
7
- data.tar.gz: f66a2d8060897f3b7cb2ba7b7c7e81b337c6a02250890fc8ff5f96a1d418674e6cc1ad271d30874c3bab2fedb005824410a08fcb1a1ea7dc3b47368eda17c9ba
6
+ metadata.gz: 704bb7a5810c7e0b3f42f82c106c4f32a907f7cc2797cdc192382cf1d072c49ecd8ad6d8c9034ac34a9fd23eb2c2701395d79b8b08a73f091cfa72e0247bc1a9
7
+ data.tar.gz: 1a24892e521670cc0ba0727fbdb90b9614fd6caad1711b366140eb13bff864e0166647f22ac2f98b6f272c9ce9567ae29c6a97d4a26efddf0a0cfc0fbc4d71ee
@@ -4,21 +4,18 @@ module RealSavvy
4
4
  # In order of access level
5
5
  SCOPE_VERBS = %w{public read write admin}.freeze
6
6
 
7
- attr_reader :scopes, :user, :site, :token, :header, :claims
7
+ attr_reader :token
8
8
 
9
9
  def initialize(token)
10
10
  @token = token
11
11
  standardized_token
12
- retrieve_claims
13
- retrieve_scopes
14
- retrieve_audience
15
- retrieve_site
16
- retrieve_subject
17
- retrieve_user
18
12
  end
19
13
 
14
+ # New token, plus makes sure there isn't any errors with the token
20
15
  def self.decode(token)
21
- new(token)
16
+ new(token).tap do |new_token|
17
+ new_token.valid?
18
+ end
22
19
  end
23
20
 
24
21
  def scope_includes?(*scope_parts)
@@ -90,6 +87,7 @@ module RealSavvy
90
87
  end
91
88
 
92
89
  def imposter?
90
+ user
93
91
  @imposter ? true : false
94
92
  end
95
93
 
@@ -97,43 +95,53 @@ module RealSavvy
97
95
  @token.split('.')[1]
98
96
  end
99
97
 
100
- private
98
+ def claims
99
+ retrieve_claims unless @claims
100
+ @claims
101
+ end
101
102
 
102
- attr_reader :audience, :subject
103
+ def header
104
+ retrieve_claims unless @header
105
+ @header
106
+ end
103
107
 
104
- def retrieve_claims
105
- raise NotImplementedError, "subclass did not define #retrieve_claims"
108
+ def site
109
+ audience
106
110
  end
107
111
 
108
- def retrieve_audience
109
- @audience = ::RealSavvy::JWT::Config.retrieve_audience(self) if claims && claims['aud']
112
+ def user
113
+ @user ||= begin
114
+ if subject_is_user?
115
+ subject
116
+ elsif subject_is_imposter?
117
+ @imposter = true
118
+ subject.user
119
+ end
120
+ end
110
121
  end
111
122
 
112
- def retrieve_subject
113
- @subject = ::RealSavvy::JWT::Config.retrieve_subject(self) if claims && claims['sub']
123
+ def scopes
124
+ @scopes ||= raw_scopes.each_with_object({}) do |scope, result|
125
+ scope.split(':').inject(result) { |m, v| m[v] ||= {} }
126
+ end
114
127
  end
115
128
 
116
- def retrieve_site
117
- @site = audience
129
+ private
130
+
131
+ def retrieve_claims
132
+ raise NotImplementedError, "subclass did not define #retrieve_claims"
118
133
  end
119
134
 
120
- def retrieve_user
121
- if subject_is_user?
122
- @user = subject
123
- elsif subject_is_imposter?
124
- @user = subject.user
125
- @imposter = true
126
- end
135
+ def audience
136
+ @audience ||= ::RealSavvy::JWT::Config.retrieve_audience(self) if claims && claims['aud']
127
137
  end
128
138
 
129
- def raw_scopes
130
- claims&.fetch('scopes', nil).to_a
139
+ def subject
140
+ @subject ||= ::RealSavvy::JWT::Config.retrieve_subject(self) if claims && claims['sub']
131
141
  end
132
142
 
133
- def retrieve_scopes
134
- @scopes = raw_scopes.each_with_object({}) do |scope, result|
135
- scope.split(':').inject(result) { |m, v| m[v] ||= {} }
136
- end
143
+ def raw_scopes
144
+ claims&.fetch('scopes', nil).to_a
137
145
  end
138
146
 
139
147
  def standardized_token
@@ -5,6 +5,7 @@ module RealSavvy
5
5
  if block_given?
6
6
  @public_key = Proc.new
7
7
  else
8
+ raise NotImplementedError, "public_key not provided" unless @public_key
8
9
  result = @public_key.is_a?(Proc) ? @public_key.call : @public_key
9
10
  result.is_a?(OpenSSL::PKey::RSA) ? result : OpenSSL::PKey::RSA.new(result)
10
11
  end
@@ -14,11 +15,12 @@ module RealSavvy
14
15
  @public_key = value
15
16
  end
16
17
 
17
- def self.retrieve_audience claims = nil
18
+ def self.retrieve_audience token = nil
18
19
  if block_given?
19
20
  @retrieve_audience = Proc.new
20
21
  else
21
- @retrieve_audience.call(claims)
22
+ raise NotImplementedError, "retrieve_audience logic not implemeted" unless @retrieve_audience
23
+ @retrieve_audience.call(token)
22
24
  end
23
25
  end
24
26
 
@@ -26,11 +28,12 @@ module RealSavvy
26
28
  @retrieve_audience = value
27
29
  end
28
30
 
29
- def self.retrieve_subject claims = nil
31
+ def self.retrieve_subject token = nil
30
32
  if block_given?
31
33
  @retrieve_subject = Proc.new
32
34
  else
33
- @retrieve_subject.call(claims)
35
+ raise NotImplementedError, "retrieve_subject logic not implemeted" unless @retrieve_subject
36
+ @retrieve_subject.call(token)
34
37
  end
35
38
  end
36
39
 
@@ -42,6 +45,7 @@ module RealSavvy
42
45
  if block_given?
43
46
  @validate_token = Proc.new
44
47
  else
48
+ raise NotImplementedError, "validate_token logic not implemeted" unless @validate_token
45
49
  @validate_token.call(token)
46
50
  end
47
51
  end
@@ -1,3 +1,3 @@
1
1
  module RealSavvy
2
- VERSION = [0,0,9]
2
+ VERSION = [0,0,10]
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: real_savvy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Rauh