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 +4 -4
- data/README.md +27 -20
- data/lib/prx_auth/resource_map.rb +19 -19
- data/lib/prx_auth/scope_list.rb +63 -59
- data/lib/prx_auth/version.rb +1 -1
- data/test/prx_auth/resource_map_test.rb +14 -0
- metadata +2 -3
- data/lib/rack/prx_auth/version.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6b1ad46cadbbeddae8e3bebd398582339573c0cd93a16e1ec32d81640c57132
|
4
|
+
data.tar.gz: e16653c6a43a3398d770dee31c1cc414a5a1eedb38434e8568c9d7fad3f74e78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef892e741fce211dd772a141dd0b80741b395cc8a926651322c79e9bcb91e141c556164c5b46a2f617a5297bc26ab04c0e6f1b954c6d7440ecd42d78831d1025
|
7
|
+
data.tar.gz: 50f52aa960307c3f2117cef31586c946338b032183c5d0f85d1098af448e0fe4e27b97365a782970b95738eec4e6be90f05f35dde1597841f70e0ede9a2a4aa4
|
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
#
|
1
|
+
# PrxAuth
|
2
2
|
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/
|
4
|
-
[![Dependency Status](https://gemnasium.com/PRX/
|
5
|
-
[![Build Status](https://travis-ci.org/PRX/
|
6
|
-
[![Code Climate](https://codeclimate.com/github/PRX/
|
7
|
-
[![Coverage Status](https://coveralls.io/repos/PRX/
|
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 '
|
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
|
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
|
-
###
|
37
|
+
### Utility Classes
|
37
38
|
|
38
|
-
|
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
|
-
|
49
|
+
#### Configuration
|
43
50
|
|
44
|
-
Rack
|
51
|
+
Rack::PrxAuth takes two optional parameters, `issuer` and `cert_location`. See Installation for how to specify them.
|
45
52
|
|
46
|
-
By default,
|
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,
|
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
|
-
|
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,
|
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,
|
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,
|
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
|
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/
|
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
|
-
|
9
|
-
[key
|
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 =
|
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[
|
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
|
-
|
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
|
-
|
110
|
+
keys
|
110
111
|
else
|
111
|
-
|
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
|
-
|
121
|
-
@map[resource.to_s]
|
121
|
+
self[resource.to_s]
|
122
122
|
end
|
123
123
|
end
|
124
124
|
end
|
data/lib/prx_auth/scope_list.rb
CHANGED
@@ -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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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 =
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
data/lib/prx_auth/version.rb
CHANGED
@@ -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.
|
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-
|
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
|