graphql-authorization 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f41e83bd1407914b99c2d161cab351389bc2662
4
+ data.tar.gz: 5327b6bcf0740340a54d0302c3411e37aba13d04
5
+ SHA512:
6
+ metadata.gz: d5c08d15ef43326785b8d84c132f5427aa68de7dcaf8a8bf55232958ab6afd12e6d8d0c22c968dcdf9cfc58139353bc3deac9fcdc974ad990d361946acce94d0
7
+ data.tar.gz: 432554dfd539e37283dfa0f120113895e188637de6ecc518406e4b8b46a527b1463f6749a50bc96b3dc6adb901db6b509c573fe86cc7b2d3200a3edb5d58b224
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Matthew Chang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'GraphqlAuthorization'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,11 @@
1
+ module GraphQL
2
+ module Authorization
3
+ end
4
+ end
5
+
6
+ require 'graphql/authorization/version'
7
+ require 'graphql/authorization/instrumentation'
8
+ require 'graphql/authorization/unauthorized'
9
+ require 'graphql/authorization/ability'
10
+ require 'graphql/authorization/all'
11
+ require 'graphql/authorization/ability_type'
@@ -0,0 +1,75 @@
1
+ class GraphQL::Authorization::Ability
2
+ def initialize(user)
3
+ @user = user
4
+ @ability = {}
5
+
6
+ #default white list builtin scalars
7
+ permit GraphQL::STRING_TYPE, execute: true, only: []
8
+ permit GraphQL::INT_TYPE, execute: true, only: []
9
+ permit GraphQL::FLOAT_TYPE, execute: true, only: []
10
+ permit GraphQL::ID_TYPE, execute: true, only: []
11
+ permit GraphQL::BOOLEAN_TYPE, execute: true, only: []
12
+
13
+ ability(user)
14
+ end
15
+
16
+ #permits execution, all access by default
17
+ def permit(type,options={})
18
+ raise NameError.new("duplicate ability definition") if @ability.key? type
19
+ ability_object = GraphQL::Authorization::AbilityType.new(type,nil,{})
20
+ if options.key?(:except) && options.key?(:only)
21
+ raise ArgumentError.new("you cannot specify white list and black list")
22
+ end
23
+ if options[:except]
24
+ ability_object.access(type.fields.keys.map(&:to_sym) - options[:except])
25
+ elsif options[:only]
26
+ ability_object.access(options[:only])
27
+ end
28
+ ability_object.execute options[:execute]
29
+ if block_given?
30
+ #note Proc.new creates a proc with the block given to the method
31
+ ability_object.instance_eval(&Proc.new)
32
+ end
33
+ @ability[type] = ability_object
34
+ end
35
+
36
+ #calls a proc-like object with args comensorate with it's arity
37
+ def callSetArgs(object,*args)
38
+ arity = object&.arity || object.method(:call).arity
39
+ if arity > 0
40
+ object.call(*args[0..arity-1])
41
+ elsif arity == 0
42
+ object.call()
43
+ else
44
+ object.call(*args)
45
+ end
46
+ end
47
+
48
+ #returns true if the user can execute queries of type, "type"
49
+ def canExecute(type,args={})
50
+ return false unless @ability[type]
51
+ execute = @ability[type].execute_permission
52
+ return callSetArgs(execute,args) if execute.respond_to? :call
53
+ execute
54
+ end
55
+
56
+ #returns true if the user can access "field" on "type"
57
+ def canAccess(type,field,object=nil,args={})
58
+ return false unless @ability[type]
59
+ access = @ability[type].access_permission[field]
60
+ return callSetArgs(access,object,args) if access.respond_to? :call
61
+ access
62
+ end
63
+
64
+ def allowed type
65
+ if type.class == GraphQL::UnionType
66
+ permit type, execute: true
67
+ else
68
+ permit type, execute: true, only: GraphQL::Authorization::All
69
+ end
70
+ end
71
+
72
+ def ability(user)
73
+ raise NotImplementedError.new("must implmenet ability funciton")
74
+ end
75
+ end
@@ -0,0 +1,19 @@
1
+ GraphQL::Authorization::AbilityType = Struct.new("AbilityType", :type, :execute_permission, :access_permission) do
2
+ def execute value
3
+ self.execute_permission = value
4
+ end
5
+ def access value, evaluator = true
6
+ if self.type.class == GraphQL::UnionType
7
+ raise ArgumentError.new "Specifying access on a union type which cannot be accessed"
8
+ end
9
+ if value == GraphQL::Authorization::All
10
+ self.access type.all_fields.map {|e| e.name.to_sym}, evaluator
11
+ elsif value.class == Array
12
+ self.access value.map {|e| [e,evaluator]}.to_h
13
+ elsif value.class != Hash
14
+ self.access({value => evaluator})
15
+ else
16
+ self.access_permission = self.access_permission.merge(value)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ #Simply definining a unique symbol to represent "all fields" for authorization
2
+ GraphQL::Authorization::All = Object.new
@@ -0,0 +1,40 @@
1
+ # Wrapps fields in authorization checks
2
+ module GraphQL
3
+ module Authorization
4
+ class Instrumentation
5
+ def initialize(always_allow_execute: false)
6
+ @always_allow_execute = always_allow_execute
7
+ end
8
+
9
+ # returns the essential type of a potentially wrapped type (i.e., list or non-null)
10
+ def baseTypeOf(type)
11
+ if type.class == GraphQL::NonNullType || type.class == GraphQL::ListType
12
+ baseTypeOf(type.of_type)
13
+ else
14
+ type
15
+ end
16
+ end
17
+
18
+ def toSymKeys(hash)
19
+ hash.map { |key, value| [key.to_sym, value] }.to_h
20
+ end
21
+
22
+ def instrument(type, field)
23
+ fieldType = baseTypeOf(field.type)
24
+ old_resolve_proc = field.resolve_proc
25
+ new_resolve_proc = lambda do |obj, args, ctx|
26
+ unless ctx[:ability] == :root
27
+ raise GraphQL::Authorization::Unauthorized, "not authorized to execute #{fieldType.name}" unless ctx[:ability].canExecute(fieldType, toSymKeys(args.to_h)) || @always_allow_execute
28
+ raise GraphQL::Authorization::Unauthorized, "not authorized to access #{field.name} on #{type.name}" unless ctx[:ability].canAccess(type, field.name.to_sym, obj, toSymKeys(args.to_h))
29
+ end
30
+ old_resolve_proc.call(obj, args, ctx)
31
+ end
32
+
33
+ # Return a copy of `field`, with a new resolve proc
34
+ field.redefine do
35
+ resolve(new_resolve_proc)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ module GraphQL
2
+ module Authorization
3
+ class Unauthorized < StandardError
4
+ def initialize(msg="Unauthorized")
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module GraphQL
2
+ module Authorization
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: graphql-authorization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Chang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: graphql
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.4.2
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3'
47
+ description:
48
+ email:
49
+ - matthew@callnine.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - Rakefile
56
+ - lib/graphql/authorization.rb
57
+ - lib/graphql/authorization/ability.rb
58
+ - lib/graphql/authorization/ability_type.rb
59
+ - lib/graphql/authorization/all.rb
60
+ - lib/graphql/authorization/instrumentation.rb
61
+ - lib/graphql/authorization/unauthorized.rb
62
+ - lib/graphql/authorization/version.rb
63
+ homepage: https://github.com/Call9/graphql-authorization
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.5.1
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: An authorization framework for graphql-ruby
87
+ test_files: []