prx_auth 1.1.0 → 1.2.0

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
  SHA256:
3
- metadata.gz: 3e7866c4a21b61c3e43328c528301e6b53aa350efc5caec23f94b2c4acebea30
4
- data.tar.gz: 4d39f067376023bfa72cdaa2059ea976d5011535e5b766fb5928478508f48e23
3
+ metadata.gz: b6b1ad46cadbbeddae8e3bebd398582339573c0cd93a16e1ec32d81640c57132
4
+ data.tar.gz: e16653c6a43a3398d770dee31c1cc414a5a1eedb38434e8568c9d7fad3f74e78
5
5
  SHA512:
6
- metadata.gz: 849fe18e035fe9406412cf48da28578d60ecdd5e5ea632b0577d17dcb0b2566e187452313f86d6186529b963a5771275a4968b5ed8bd342a7fea5bfafeb6d34e
7
- data.tar.gz: 916cc3c3e08829b1bd07f638b853bc4a19b9bf68ce08ec5580df999fda8c712f50046a9e11b77ef4063df7e8bfd1ebf15457439ccc67e2c5ff5089ae9c2c2350
6
+ metadata.gz: ef892e741fce211dd772a141dd0b80741b395cc8a926651322c79e9bcb91e141c556164c5b46a2f617a5297bc26ab04c0e6f1b954c6d7440ecd42d78831d1025
7
+ data.tar.gz: 50f52aa960307c3f2117cef31586c946338b032183c5d0f85d1098af448e0fe4e27b97365a782970b95738eec4e6be90f05f35dde1597841f70e0ede9a2a4aa4
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # Rack::PrxAuth
1
+ # PrxAuth
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/rack-prx_auth.svg)](http://badge.fury.io/rb/rack-prx_auth)
4
- [![Dependency Status](https://gemnasium.com/PRX/rack-prx_auth.svg)](https://gemnasium.com/PRX/rack-prx_auth)
5
- [![Build Status](https://travis-ci.org/PRX/rack-prx_auth.svg?branch=master)](https://travis-ci.org/PRX/rack-prx_auth)
6
- [![Code Climate](https://codeclimate.com/github/PRX/rack-prx_auth/badges/gpa.svg)](https://codeclimate.com/github/PRX/rack-prx_auth)
7
- [![Coverage Status](https://coveralls.io/repos/PRX/rack-prx_auth/badge.svg)](https://coveralls.io/r/PRX/rack-prx_auth)
3
+ [![Gem Version](https://badge.fury.io/rb/prx_auth.svg)](http://badge.fury.io/rb/prx_auth)
4
+ [![Dependency Status](https://gemnasium.com/PRX/prx_auth.svg)](https://gemnasium.com/PRX/prx_auth)
5
+ [![Build Status](https://travis-ci.org/PRX/prx_auth.svg?branch=master)](https://travis-ci.org/PRX/prx_auth)
6
+ [![Code Climate](https://codeclimate.com/github/PRX/prx_auth/badges/gpa.svg)](https://codeclimate.com/github/PRX/prx_auth)
7
+ [![Coverage Status](https://coveralls.io/repos/PRX/prx_auth/badge.svg)](https://coveralls.io/r/PRX/prx_auth)
8
8
 
9
9
  This gem adds middleware to a Rack application that decodes and verified a JSON Web Token (JWT) issued by PRX.org. If the JWT is invalid, the middleware will respond with a 401 Unauthorized. If the JWT was not issued by PRX (or the specified issuer), the request will continue through the middleware stack.
10
10
 
@@ -13,7 +13,7 @@ This gem adds middleware to a Rack application that decodes and verified a JSON
13
13
  Add this line to your application's Gemfile:
14
14
 
15
15
  ```ruby
16
- gem 'rack-prx_auth'
16
+ gem 'prx_auth'
17
17
  ```
18
18
 
19
19
  And then execute:
@@ -22,44 +22,51 @@ And then execute:
22
22
 
23
23
  Or install it yourself as:
24
24
 
25
- $ gem install rack-prx_auth
25
+ $ gem install prx_auth
26
26
 
27
27
  In a non-Rails app, add the following to the application's config.ru file:
28
28
 
29
29
  ```ruby
30
+ require 'rack/prx_auth'
30
31
  use Rack::PrxAuth, cert_location: [CERT LOCATION], issuer: [ISSUER]
31
32
  ```
32
33
  The `cert_location` and `issuer` parameters are optional. See below.
33
34
 
34
35
  ## Usage
35
36
 
36
- ### The Request
37
+ ### Utility Classes
37
38
 
38
- Rack-prx_auth looks for a token in the request's HTTP_AUTHORIZATION header. It expects that the header's content will take the form of 'Bearer <your token>'. If no HTTP_AUTHORIZATION header is present, rack-prx_auth passes the request to the next middleware.
39
+ Right now, we just have ResourceMap and ScopeList which allow you to work with scopes the way they are handled in the id.prx.org tokens. Resource wildcards and namespaces are supported. By using ResourceMap and ScopeList, you can also perform set arithmetic while handling wildcard semantics.
40
+
41
+ ### Rack::PrxAuth
42
+
43
+ #### The Request
44
+
45
+ Rack::PrxAuth looks for a token in the request's HTTP_AUTHORIZATION header. It expects that the header's content will take the form of 'Bearer <your token>'. If no HTTP_AUTHORIZATION header is present, the middlware passes the request to the next middleware.
39
46
 
40
47
  We have another application that's in charge of making the token. It's called id.prx.org. Its job is to show a form for a user to enter credentials, validate those credentials, and then generate a JWT using PRX's private key. See http://openid.net/specs/openid-connect-implicit-1_0.html to find out what information is encoded in a JWT. Basically it's a hash containing, among other things, the user's ID, the issuer of the token, and when the token expires.
41
48
 
42
- ### Configuration
49
+ #### Configuration
43
50
 
44
- Rack-prx_auth takes two optional parameters, `issuer` and `cert_location`. See Installation for how to specify them.
51
+ Rack::PrxAuth takes two optional parameters, `issuer` and `cert_location`. See Installation for how to specify them.
45
52
 
46
- By default, rack-prx_auth will assume that you want to make sure the JWT was issued by PRX. After decoding the JWT, rack-prx_auth checks the `issuer` field to make sure it's id.prx.org. If you want it to check for a different issuer, pass `issuer: <your issuer>` as a parameter.
53
+ By default, Rack::PrxAuth will assume that you want to make sure the JWT was issued by PRX. After decoding the JWT, Rack::PrxAuth checks the `issuer` field to make sure it's id.prx.org. If you want it to check for a different issuer, pass `issuer: <your issuer>` as a parameter.
47
54
 
48
- Since the JWT was created using PRX's private key, rack-prx_auth needs to fetch PRX's public key to decode it. It does this by accessing the `cert_location` (default is https://id.prx.org/api/v1/certs), generating an OpenSSL::X509::Certificate based on its contents, and determining the public key from the certificate object. Should you wish to get your public key from a different certificate, you may specify a different endpoint by passing `cert_location: <your cert location>` as a parameter. Keep in mind that unless the certificate matches the private key used to make the JWT, rack-prx_auth will return 401.
55
+ Since the JWT was created using PRX's private key, rack-prx_auth needs to fetch PRX's public key to decode it. It does this by accessing the `cert_location` (default is https://id.prx.org/api/v1/certs), generating an OpenSSL::X509::Certificate based on its contents, and determining the public key from the certificate object. Should you wish to get your public key from a different certificate, you may specify a different endpoint by passing `cert_location: <your cert location>` as a parameter. Keep in mind that unless the certificate matches the private key used to make the JWT, Rack::PrxAuth will return 401.
49
56
 
50
- ### The Response
57
+ #### The Response
51
58
 
52
- If the token isn't valid, meaning it's expired or it wasn't created using our private key, rack-prx_auth will return 401 Unauthorized.
59
+ If the token isn't valid, meaning it's expired or it wasn't created using our private key, Rack::PrxAuth will return 401 Unauthorized.
53
60
 
54
- If there's nothing in the HTTP_AUTHORIZATION heading, there's something but JSON::JWT can't decode it, or the issuer field doesn't specify the correct issuer, rack-prx_auth just punts to the next piece of middleware.
61
+ If there's nothing in the HTTP_AUTHORIZATION heading, there's something but JSON::JWT can't decode it, or the issuer field doesn't specify the correct issuer, Rack::PrxAuth just punts to the next piece of middleware.
55
62
 
56
- If all goes well, rack-prx_auth takes the decoded JWT and makes a TokenData object. Then it adds this object to the `env` with the key 'prx.auth'.
63
+ If all goes well, Rack::PrxAuth takes the decoded JWT and makes a TokenData object. Then it adds this object to the `env` with the key 'prx.auth'.
57
64
 
58
- If you are using rack-prx_auth in a Rails app, you'll have a few handy controller methods available to you. Calling `prx_auth_token` within a controller returns the TokenData object, and `prx_authenticated?` tells you whether a TokenData object is available. Also, if you call `user_id` on the TokenData object, you get the user's ID so you can ask id.prx.org for information about them.
65
+ If you are using Rack::PrxAuth in a Rails app, probably use [prx_auth-rails](https://github.com/PRX/prx_auth-rails) which will automatically install the middleware and add some helpful controller methods.
59
66
 
60
67
  ## Contributing
61
68
 
62
- 1. Fork it ( https://github.com/PRX/rack-prx_auth/fork )
69
+ 1. Fork it ( https://github.com/PRX/prx_auth/fork )
63
70
  2. Create your feature branch (`git checkout -b my-new-feature`)
64
71
  3. Commit your changes (`git commit -am 'Add some feature'`)
65
72
  4. Push to the branch (`git push origin my-new-feature`)
@@ -1,13 +1,20 @@
1
1
  module PrxAuth
2
- class ResourceMap
2
+ class ResourceMap < Hash
3
3
  WILDCARD_KEY = '*'
4
4
 
5
5
  def initialize(mapped_values)
6
+ super() do |hash, key|
7
+ if key == WILDCARD_KEY
8
+ @wildcard
9
+ else
10
+ nil
11
+ end
12
+ end
6
13
  input = mapped_values.clone
7
14
  @wildcard = ScopeList.new(input.delete(WILDCARD_KEY)||'')
8
- @map = Hash[input.map do |(key, values)|
9
- [key, ScopeList.new(values)]
10
- end]
15
+ input.each do |(key, values)|
16
+ self[key.to_s] = ScopeList.new(values)
17
+ end
11
18
  end
12
19
 
13
20
  def contains?(resource, namespace=nil, scope=nil)
@@ -18,7 +25,7 @@ module PrxAuth
18
25
 
19
26
  @wildcard.contains?(namespace, scope)
20
27
  else
21
- mapped_resource = @map[resource]
28
+ mapped_resource = self[resource]
22
29
 
23
30
  if mapped_resource && !namespace.nil?
24
31
  mapped_resource.contains?(namespace, scope) || @wildcard.contains?(namespace, scope)
@@ -32,7 +39,7 @@ module PrxAuth
32
39
 
33
40
  def condense
34
41
  condensed_wildcard = @wildcard.condense
35
- condensed_map = Hash[@map.map do |resource, list|
42
+ condensed_map = Hash[map do |resource, list|
36
43
  [resource, (list - condensed_wildcard).condense]
37
44
  end]
38
45
  ResourceMap.new(condensed_map.merge(WILDCARD_KEY => condensed_wildcard))
@@ -63,7 +70,7 @@ module PrxAuth
63
70
  result[resource] = list_for_resource(resource) - (other_wildcard + other_map.list_for_resource(resource))
64
71
  end
65
72
 
66
- if @wildcard
73
+ if @wildcard.length
67
74
  result[WILDCARD_KEY] = @wildcard - other_wildcard
68
75
  end
69
76
 
@@ -87,7 +94,7 @@ module PrxAuth
87
94
  end
88
95
  end
89
96
 
90
- if @wildcard
97
+ if @wildcard.length > 0
91
98
  result[WILDCARD_KEY] = @wildcard - (@wildcard - other_wildcard)
92
99
  end
93
100
 
@@ -95,20 +102,14 @@ module PrxAuth
95
102
  end
96
103
 
97
104
  def as_json(opts={})
98
- @map.merge(WILDCARD_KEY => @wildcard).as_json(opts)
99
- end
100
-
101
- def freeze
102
- @map.freeze
103
- @wildcard.freeze
104
- self
105
+ super(opts).merge(@wildcard.length > 0 ? {WILDCARD_KEY => @wildcard}.as_json(opts) : {})
105
106
  end
106
107
 
107
108
  def resources(namespace=nil, scope=nil)
108
109
  if namespace.nil?
109
- @map.keys
110
+ keys
110
111
  else
111
- @map.select do |name, list|
112
+ select do |name, list|
112
113
  list.contains?(namespace, scope) || @wildcard.contains?(namespace, scope)
113
114
  end.map(&:first)
114
115
  end
@@ -117,8 +118,7 @@ module PrxAuth
117
118
  protected
118
119
 
119
120
  def list_for_resource(resource)
120
- return @wildcard if resource.to_s == WILDCARD_KEY
121
- @map[resource.to_s]
121
+ self[resource.to_s]
122
122
  end
123
123
  end
124
124
  end
@@ -1,52 +1,87 @@
1
1
  module PrxAuth
2
- class ScopeList
2
+ class ScopeList < Array
3
3
  SCOPE_SEPARATOR = ' '
4
4
  NAMESPACE_SEPARATOR = ':'
5
5
  NO_NAMESPACE = :_
6
6
 
7
+ Entry = Struct.new(:namespace, :scope)
8
+
9
+ class Entry
10
+ def equal?(other_entry)
11
+ namespace == other_entry.namespace && scope == other_entry.scope
12
+ end
13
+
14
+ def to_s
15
+ if namespaced?
16
+ "#{namespace}:#{scope}"
17
+ else
18
+ scope.to_s
19
+ end
20
+ end
21
+
22
+ def namespaced?
23
+ !(namespace.nil? || namespace == NO_NAMESPACE)
24
+ end
25
+
26
+ def unnamespaced
27
+ if namespaced?
28
+ Entry.new(NO_NAMESPACE, scope)
29
+ else
30
+ self
31
+ end
32
+ end
33
+ end
34
+
7
35
  def self.new(list)
8
36
  case list
9
37
  when PrxAuth::ScopeList then list
38
+ when Array then super(list.join(' '))
10
39
  else super(list)
11
40
  end
12
41
  end
13
42
 
14
43
  def initialize(list)
15
44
  @string = list
45
+ @string.split(SCOPE_SEPARATOR).each do |value|
46
+ next if value.length < 1
47
+
48
+ parts = value.split(NAMESPACE_SEPARATOR, 2)
49
+ if parts.length == 2
50
+ push Entry.new(symbolize(parts[0]), symbolize(parts[1]))
51
+ else
52
+ push Entry.new(NO_NAMESPACE, symbolize(parts[0]))
53
+ end
54
+ end
16
55
  end
17
56
 
18
57
  def contains?(namespace, scope=nil)
19
- scope, namespace = namespace, NO_NAMESPACE if scope.nil?
20
-
21
- if namespace == NO_NAMESPACE
22
- map[namespace].include?(symbolize(scope))
23
- else
24
- symbolized_scope = symbolize(scope)
25
- map[symbolize(namespace)].include?(symbolized_scope) || map[NO_NAMESPACE].include?(symbolized_scope)
58
+ entries = if scope.nil?
59
+ scope, namespace = namespace, NO_NAMESPACE
60
+ [Entry.new(namespace, symbolize(scope))]
61
+ else
62
+ scope = symbolize(scope)
63
+ namespace = symbolize(namespace)
64
+ [Entry.new(namespace, scope), Entry.new(NO_NAMESPACE, scope)]
65
+ end
66
+
67
+ entries.any? do |possible_match|
68
+ include?(possible_match)
26
69
  end
27
70
  end
28
71
 
29
- def freeze
30
- @string.freeze
31
- self
32
- end
33
-
34
72
  def to_s
35
73
  @string
36
74
  end
37
75
 
38
76
  def condense
39
77
  tripped = false
40
- result = map[NO_NAMESPACE].clone
41
- namespaces = map.keys - [NO_NAMESPACE]
42
-
43
- namespaces.each do |ns|
44
- map[ns].each do |scope|
45
- if !contains?(NO_NAMESPACE, scope)
46
- result << scope_string(ns, scope)
47
- else
48
- tripped = true
49
- end
78
+ result = []
79
+
80
+ each do |entry|
81
+ if entry.namespaced? && include?(entry.unnamespaced)
82
+ tripped = true
83
+ else
84
+ result << entry
50
85
  end
51
86
  end
52
87
 
@@ -67,13 +102,11 @@ module PrxAuth
67
102
  tripped = false
68
103
  result = []
69
104
 
70
- map.each do |namespace, scopes|
71
- scopes.each do |scope|
72
- if other_scope_list.contains?(namespace, scope)
73
- tripped = true
74
- else
75
- result << scope_string(namespace, scope)
76
- end
105
+ each do |entry|
106
+ if other_scope_list.include?(entry) || other_scope_list.include?(entry.unnamespaced)
107
+ tripped = true
108
+ else
109
+ result << entry
77
110
  end
78
111
  end
79
112
 
@@ -98,35 +131,6 @@ module PrxAuth
98
131
 
99
132
  private
100
133
 
101
- def map
102
- @parsed_map ||= empty_map.tap do |map|
103
- @string.split(SCOPE_SEPARATOR).each do |value|
104
- next if value.length < 1
105
-
106
- parts = value.split(NAMESPACE_SEPARATOR, 2)
107
- if parts.length == 2
108
- map[symbolize(parts[0])] << symbolize(parts[1])
109
- else
110
- map[NO_NAMESPACE] << symbolize(parts[0])
111
- end
112
- end
113
- end
114
- end
115
-
116
- def scope_string(ns, scope)
117
- if ns == NO_NAMESPACE
118
- scope.to_s
119
- else
120
- [ns, scope].join(NAMESPACE_SEPARATOR)
121
- end
122
- end
123
-
124
- def empty_map
125
- @empty_map ||= Hash.new do |hash, key|
126
- hash[key] = []
127
- end
128
- end
129
-
130
134
  def symbolize(value)
131
135
  case value
132
136
  when Symbol then value
@@ -1,3 +1,3 @@
1
1
  module PrxAuth
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -155,4 +155,18 @@ describe PrxAuth::ResourceMap do
155
155
  assert map.contains?("*", :wild)
156
156
  end
157
157
  end
158
+
159
+ describe '#as_json' do
160
+ it 'does not include wildcard key if list is empty' do
161
+ map = new_map("foo" => "asdf")
162
+ refute map.as_json.has_key?('*')
163
+ map2 = new_map("foo" => "asdf", "*" => "")
164
+ refute map2.as_json.has_key?('*')
165
+ end
166
+
167
+ it 'includes the wildcard key if the list is not empty' do
168
+ map = new_map("*" => "asdf")
169
+ assert map.as_json.has_key?('*')
170
+ end
171
+ end
158
172
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prx_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eve Asher
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-04-23 00:00:00.000000000 Z
12
+ date: 2020-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -146,7 +146,6 @@ files:
146
146
  - lib/rack/prx_auth.rb
147
147
  - lib/rack/prx_auth/certificate.rb
148
148
  - lib/rack/prx_auth/token_data.rb
149
- - lib/rack/prx_auth/version.rb
150
149
  - prx_auth.gemspec
151
150
  - test/prx_auth/resource_map_test.rb
152
151
  - test/prx_auth/scope_list_test.rb
@@ -1,7 +0,0 @@
1
- require 'prx_auth/version'
2
-
3
- module Rack
4
- class PrxAuth
5
- VERSION = PrxAuth::VERSION
6
- end
7
- end