Class: Verifica::Authorizer
- Inherits:
-
Object
- Object
- Verifica::Authorizer
- Defined in:
- lib/verifica/authorizer.rb
Overview
Authorizer is the heart of Verifica. It’s an isolated container with no global state which has a list of resource types registered with their companion AclProviders.
Authorizer pairs great with Dependency Injection or can be configured and passed in a way that is compatible with your framework.
Instance Method Summary collapse
-
#allowed_actions(subject, resource, **context) ⇒ Array<Symbol>
Array of actions allowed for
subject
or empty array if none. -
#authorization_result(subject, resource, action, **context) ⇒ AuthorizationResult
The same as #authorize but returns a special result object instead of rising an exception.
-
#authorize(subject, resource, action, **context) ⇒ AuthorizationResult
Checks the authorization of a subject to perform an action on a resource.
-
#authorized?(subject, resource, action, **context) ⇒ Boolean
The same as #authorize but returns true/false instead of rising an exception.
-
#initialize(resource_configs) ⇒ Authorizer
constructor
A new instance of Authorizer.
-
#resource_acl(resource, **context) ⇒ Acl
Access Control List for
resource
. -
#resource_config(resource_type) ⇒ ResourceConfiguration
Configuration for
resource_type
. -
#resource_type?(resource_type) ⇒ Boolean
True if
resource_type
is registered inself
.
Constructor Details
#initialize(resource_configs) ⇒ Authorizer
Use Verifica.authorizer instead of this constructor directly
Returns a new instance of Authorizer.
34 35 36 37 |
# File 'lib/verifica/authorizer.rb', line 34 def initialize(resource_configs) @resources = index_resources(resource_configs).freeze freeze end |
Instance Method Details
#allowed_actions(subject, resource, **context) ⇒ Array<Symbol>
Returns array of actions allowed for subject
or empty array if none.
122 123 124 125 126 |
# File 'lib/verifica/authorizer.rb', line 122 def allowed_actions(subject, resource, **context) acl = resource_acl(resource, **context) sids = Verifica.subject_sids(subject) acl.allowed_actions(sids) end |
#authorization_result(subject, resource, action, **context) ⇒ AuthorizationResult
The same as #authorize but returns a special result object instead of rising an exception
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/verifica/authorizer.rb', line 100 def (subject, resource, action, **context) action = action.to_sym possible_actions = config_by_resource(resource).possible_actions unless possible_actions.include?(action) raise Error, "'#{action}' action is not registered as possible for '#{resource.resource_type}' resource" end acl = resource_acl(resource, **context) AuthorizationResult.new(subject, resource, action, acl, **context) end |
#authorize(subject, resource, action, **context) ⇒ AuthorizationResult
Checks the authorization of a subject to perform an action on a resource
-
The
subject
is asked for its Security Identifiers (SIDs) bysubject.subject_sids
-
The
resource
is asked for its type byresource.resource_type
-
ACL provider registered for this resource type is asked for Verifica::Acl by #call(resource, **context)
-
ACL is checked whether the
action
is allowed for the subject SIDs
67 68 69 70 71 72 |
# File 'lib/verifica/authorizer.rb', line 67 def (subject, resource, action, **context) result = (subject, resource, action, **context) raise AuthorizationError, result if result.failure? result end |
#authorized?(subject, resource, action, **context) ⇒ Boolean
The same as #authorize but returns true/false instead of rising an exception
85 86 87 |
# File 'lib/verifica/authorizer.rb', line 85 def (subject, resource, action, **context) (subject, resource, action, **context).success? end |
#resource_acl(resource, **context) ⇒ Acl
Returns Access Control List for resource
.
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/verifica/authorizer.rb', line 168 def resource_acl(resource, **context) config = config_by_resource(resource) acl = config.acl_provider.call(resource, **context) # trade-off flexibility to increase robustness here by requiring a specific type unless acl.is_a?(Verifica::Acl) type = resource.resource_type raise Error, "'#{type}' resource acl_provider should respond to #call with Acl object but got '#{acl.class}'" end acl end |
#resource_config(resource_type) ⇒ ResourceConfiguration
Returns configuration for resource_type
.
137 138 139 140 141 142 143 144 145 |
# File 'lib/verifica/authorizer.rb', line 137 def resource_config(resource_type) resource_type = resource_type.to_sym config = @resources[resource_type] if config.nil? raise Error, "Unknown resource '#{resource_type}'. Did you forget to register this resource type?" end config end |
#resource_type?(resource_type) ⇒ Boolean
Returns true if resource_type
is registered in self
.
154 155 156 |
# File 'lib/verifica/authorizer.rb', line 154 def resource_type?(resource_type) @resources.key?(resource_type.to_sym) end |