rack-prx_auth 0.0.6 → 0.0.7

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: 65365d276d9668d2b774150b51cae69cc94e7d4a
4
- data.tar.gz: 2f9218bdcfe4b22ac97f45f6040b575e0fa9c665
3
+ metadata.gz: ac85eaa2032bbae554c23530f43c4e3d26c8545b
4
+ data.tar.gz: ab19c84a7ea04d795ffe526ee289692aa0a84f1d
5
5
  SHA512:
6
- metadata.gz: 253b726a570baa8db347649f171b89f7d955ebec32c65d860aa426a1dda1df0c1fe333d5518b28530f7a9047bc29febf82d5978d8aca71035f3463017ba63625
7
- data.tar.gz: bdd2a4f396fb85aefd68677becf5fedaf16a8b46482ff8bd6735455afa0de71cb367d07a13743405ec5f8911ea83523d9b69900d55593295c10dc61b2ce8fb62
6
+ metadata.gz: 0f6f1b767c855b51231059c5a702d46abbd5dd9411317c1372dda4f96bf3c4ebf210aa04e94abcb7871695ba7a7a6b7984da9301489d04b0e11c3158aa7e43a2
7
+ data.tar.gz: 65c6902e5c051f0e73092aa279bcec88c64e378f699eba8fdb36ddfe36c605382455170d3ecb51af54612e997eff72d965e502afe5bf0f10cf77ff8d1ce50d7f
@@ -0,0 +1,20 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ## [Unreleased][unreleased]
6
+
7
+ ## [0.0.7] - 2014-07-10
8
+ ### Added
9
+ - This Changelog
10
+ - TokenData#authorized? method
11
+
12
+ ## [0.0.6] - 2015-04-22
13
+ ### Removed
14
+ - Railtie
15
+ ### Fixed
16
+ - Endless loop while fetching afer certificate cache expires
17
+
18
+ [unreleased]: https://github.com/PRX/rack-prx_auth/compare/v0.0.7...HEAD
19
+ [0.0.7]: https://github.com/PRX/rack-prx_auth/compare/v0.0.6...v0.0.7
20
+ [0.0.6]: https://github.com/PRX/rack-prx_auth/compare/v0.0.5...v0.0.6
@@ -1,15 +1,45 @@
1
1
  module Rack
2
2
  class PrxAuth
3
3
  class TokenData
4
+ attr_reader :attributes, :authorized_resources, :scopes
5
+
4
6
  def initialize(attrs = {})
5
7
  @attributes = attrs
8
+ if attrs['aur']
9
+ @authorized_resources = unpack_aur(attrs['aur']).freeze
10
+ else
11
+ @authorized_resources = {}.freeze
12
+ end
13
+ if attrs['scope']
14
+ @scopes = attrs['scope'].split(' ').freeze
15
+ else
16
+ @scopes = [].freeze
17
+ end
6
18
  end
7
19
 
8
- attr_reader :attributes
9
-
10
20
  def user_id
11
21
  @attributes['sub']
12
22
  end
23
+
24
+ def authorized?(resource, scope=nil)
25
+ if auth = authorized_resources[resource.to_s]
26
+ scope.nil? || (scopes + auth.split(' ')).include?(scope.to_s)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def unpack_aur(aur)
33
+ aur.clone.tap do |result|
34
+ unless result['$'].nil?
35
+ result.delete('$').each do |role, resources|
36
+ resources.each do |res|
37
+ result[res.to_s] = role
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
13
43
  end
14
44
  end
15
45
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class PrxAuth
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ describe Rack::PrxAuth::TokenData do
4
+ it 'pulls user_id from sub' do
5
+ token = Rack::PrxAuth::TokenData.new('sub' => 123)
6
+ token.user_id.must_equal 123
7
+ end
8
+
9
+ it 'pulls authorized_resources from aur' do
10
+ token = Rack::PrxAuth::TokenData.new('aur' => {'123' => 'admin'})
11
+ token.authorized_resources['123'].must_equal 'admin'
12
+ end
13
+
14
+ it 'unpacks compressed aur into authorized_resources' do
15
+ token = Rack::PrxAuth::TokenData.new('aur' => {
16
+ '123' => 'member',
17
+ '$' => {
18
+ 'admin' => [456, 789, 1011]
19
+ }
20
+ })
21
+ token.authorized_resources['$'].must_be_nil
22
+ token.authorized_resources['789'].must_equal 'admin'
23
+ token.authorized_resources['123'].must_equal 'member'
24
+ end
25
+
26
+ describe '#authorized?' do
27
+ let(:token) { Rack::PrxAuth::TokenData.new('aur' => aur, 'scope' => scope) }
28
+ let(:scope) { 'read write purchase sell delete' }
29
+ let(:aur) { {'123' => 'admin', '456' => 'member' } }
30
+
31
+ it 'is authorized for scope in aur' do
32
+ assert token.authorized?(123, 'admin')
33
+ end
34
+
35
+ it 'is authorized for scope in scopes' do
36
+ assert token.authorized?(456, :delete)
37
+ end
38
+
39
+ it 'is not authorized across aur limits' do
40
+ assert !token.authorized?(123, :member)
41
+ end
42
+
43
+ it 'does not require a scope' do
44
+ assert token.authorized?(123)
45
+ end
46
+
47
+ it 'is unauthorized if it hasnt seen the resource' do
48
+ assert !token.authorized?(789)
49
+ end
50
+
51
+ end
52
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-prx_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
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: 2015-04-22 00:00:00.000000000 Z
12
+ date: 2015-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -123,6 +123,7 @@ extra_rdoc_files: []
123
123
  files:
124
124
  - .gitignore
125
125
  - .travis.yml
126
+ - CHANGELOG.md
126
127
  - Gemfile
127
128
  - Guardfile
128
129
  - LICENSE
@@ -134,6 +135,7 @@ files:
134
135
  - lib/rack/prx_auth/version.rb
135
136
  - rack-prx_auth.gemspec
136
137
  - test/rack/prx_auth/certificate_test.rb
138
+ - test/rack/prx_auth/token_data_test.rb
137
139
  - test/rack/prx_auth_test.rb
138
140
  - test/test_helper.rb
139
141
  homepage: https://github.com/PRX/rack-prx_auth
@@ -163,5 +165,6 @@ summary: Rack middleware that verifies and decodes a JWT token and attaches the
163
165
  claims to env.
164
166
  test_files:
165
167
  - test/rack/prx_auth/certificate_test.rb
168
+ - test/rack/prx_auth/token_data_test.rb
166
169
  - test/rack/prx_auth_test.rb
167
170
  - test/test_helper.rb