Class: VectorMCP::Security::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/vector_mcp/security/authorization.rb

Overview

Manages authorization policies for VectorMCP servers Provides fine-grained access control for tools and resources

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthorization

Returns a new instance of Authorization.



10
11
12
13
14
# File 'lib/vector_mcp/security/authorization.rb', line 10

def initialize
  @policies = {}
  @enabled = false
  @logger = VectorMCP.logger_for("authorization")
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



8
9
10
# File 'lib/vector_mcp/security/authorization.rb', line 8

def enabled
  @enabled
end

#policiesObject (readonly)

Returns the value of attribute policies.



8
9
10
# File 'lib/vector_mcp/security/authorization.rb', line 8

def policies
  @policies
end

Instance Method Details

#add_policy(resource_type, &block) ⇒ Object

Add an authorization policy for a resource type

Parameters:

  • resource_type (Symbol)

    the type of resource (e.g., :tool, :resource, :prompt)

  • block (Proc)

    the policy block that receives (user, action, resource)



29
30
31
# File 'lib/vector_mcp/security/authorization.rb', line 29

def add_policy(resource_type, &block)
  @policies[resource_type] = block
end

#authorize(user, action, resource) ⇒ Boolean

Check if a user is authorized to perform an action on a resource

Parameters:

  • user (Object)

    the authenticated user object

  • action (Symbol)

    the action being attempted (e.g., :call, :read, :list)

  • resource (Object)

    the resource being accessed

Returns:

  • (Boolean)

    true if authorized, false otherwise



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vector_mcp/security/authorization.rb', line 44

def authorize(user, action, resource)
  return true unless @enabled

  resource_type = determine_resource_type(resource)
  policy = @policies[resource_type]
  return true unless policy

  !!policy.call(user, action, resource)
rescue StandardError => e
  @logger.error("Authorization policy error for #{resource_type}: #{e.message}")
  false
end

#disable!Object

Disable authorization (return to pass-through mode)



22
23
24
# File 'lib/vector_mcp/security/authorization.rb', line 22

def disable!
  @enabled = false
end

#enable!Object

Enable authorization system



17
18
19
# File 'lib/vector_mcp/security/authorization.rb', line 17

def enable!
  @enabled = true
end

#policy_typesArray<Symbol>

Get list of resource types with policies

Returns:

  • (Array<Symbol>)

    array of resource types



65
66
67
# File 'lib/vector_mcp/security/authorization.rb', line 65

def policy_types
  @policies.keys
end

#remove_policy(resource_type) ⇒ Object

Remove an authorization policy

Parameters:

  • resource_type (Symbol)

    the resource type to remove policy for



35
36
37
# File 'lib/vector_mcp/security/authorization.rb', line 35

def remove_policy(resource_type)
  @policies.delete(resource_type)
end

#required?Boolean

Check if authorization is required

Returns:

  • (Boolean)

    true if authorization is enabled



59
60
61
# File 'lib/vector_mcp/security/authorization.rb', line 59

def required?
  @enabled
end